|
|
@@ -1,4 +1,14 @@
|
|
|
-#include "Scripts/Controls.as"
|
|
|
+const int CTRL_UP = 1;
|
|
|
+const int CTRL_DOWN = 2;
|
|
|
+const int CTRL_LEFT = 4;
|
|
|
+const int CTRL_RIGHT = 8;
|
|
|
+const int CTRL_FIRE = 16;
|
|
|
+const int CTRL_JUMP = 32;
|
|
|
+const int CTRL_ALL = 63;
|
|
|
+
|
|
|
+const int SIDE_NEUTRAL = 0;
|
|
|
+const int SIDE_PLAYER = 1;
|
|
|
+const int SIDE_ENEMY = 2;
|
|
|
|
|
|
class GameObject : ScriptObject
|
|
|
{
|
|
|
@@ -7,12 +17,20 @@ class GameObject : ScriptObject
|
|
|
bool onGround;
|
|
|
bool isSliding;
|
|
|
float duration;
|
|
|
+ int health;
|
|
|
+ int maxHealth;
|
|
|
+ int side;
|
|
|
+ int lastDamageSide;
|
|
|
|
|
|
GameObject()
|
|
|
{
|
|
|
onGround = false;
|
|
|
isSliding = false;
|
|
|
- duration = 0;
|
|
|
+ duration = -1; // Infinite
|
|
|
+ health = 0;
|
|
|
+ maxHealth = 0;
|
|
|
+ side = SIDE_NEUTRAL;
|
|
|
+ lastDamageSide = SIDE_NEUTRAL;
|
|
|
}
|
|
|
|
|
|
void create(const Vector3&in position, const Quaternion&in rotation)
|
|
|
@@ -27,7 +45,7 @@ class GameObject : ScriptObject
|
|
|
void updateFixed(float timeStep)
|
|
|
{
|
|
|
// Disappear when duration expired
|
|
|
- if (duration > 0)
|
|
|
+ if (duration >= 0)
|
|
|
{
|
|
|
duration -= timeStep;
|
|
|
if (duration <= 0)
|
|
|
@@ -35,17 +53,110 @@ class GameObject : ScriptObject
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ bool damage(GameObject@ origin, int amount)
|
|
|
+ {
|
|
|
+ if ((origin.side == side) || (health == 0))
|
|
|
+ return false;
|
|
|
+
|
|
|
+ lastDamageSide = origin.side;
|
|
|
+ health -= amount;
|
|
|
+ if (health < 0)
|
|
|
+ health = 0;
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ bool heal(int amount)
|
|
|
+ {
|
|
|
+ // By default do not heal
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ void playSound(const string&in soundName)
|
|
|
+ {
|
|
|
+ RigidBody@ body = entity.getComponent("RigidBody");
|
|
|
+ if (@body == null)
|
|
|
+ return;
|
|
|
+
|
|
|
+ // Create the sound channel
|
|
|
+ PositionalChannel@ channel = entity.createComponent("PositionalChannel", entity.getUniqueComponentName());
|
|
|
+ body.addChild(channel);
|
|
|
+ channel.setAutoRemove(true);
|
|
|
+ channel.setDistanceAttenuation(200, 5000, 1);
|
|
|
+ Sound@ sound = cache.getResource("Sound", soundName);
|
|
|
+ channel.play(sound, sound.getFrequency());
|
|
|
+ }
|
|
|
+
|
|
|
+ Entity@ spawnObject(const Vector3&in position, const Quaternion&in rotation, const string&in className)
|
|
|
+ {
|
|
|
+ Entity@ newEntity = scene.createEntity();
|
|
|
+
|
|
|
+ // Create the ScriptInstance with specified class
|
|
|
+ ScriptInstance@ instance = newEntity.createComponent("ScriptInstance");
|
|
|
+ instance.setScriptClass(cache.getResource("ScriptFile", "Scripts/NinjaSnowWar.as"), className);
|
|
|
+ GameObject@ object = cast<GameObject>(instance.getScriptObject());
|
|
|
+ if (@object != null)
|
|
|
+ object.create(position, rotation);
|
|
|
+
|
|
|
+ return newEntity;
|
|
|
+ }
|
|
|
+
|
|
|
+ Entity@ spawnParticleEffect(const Vector3&in position, const string&in effectName, float duration)
|
|
|
+ {
|
|
|
+ Entity@ newEntity = scene.createEntity();
|
|
|
+
|
|
|
+ // Create the particle emitter
|
|
|
+ ParticleEmitter@ emitter = newEntity.createComponent("ParticleEmitter");
|
|
|
+ emitter.loadParameters(cache.getResource("XMLFile", effectName));
|
|
|
+ emitter.setPosition(position);
|
|
|
+
|
|
|
+ // Create a GameObject for managing the effect lifetime
|
|
|
+ ScriptInstance@ instance = newEntity.createComponent("ScriptInstance");
|
|
|
+ instance.setScriptClass(cache.getResource("ScriptFile", "Scripts/NinjaSnowWar.as"), "GameObject");
|
|
|
+ GameObject@ object = cast<GameObject>(instance.getScriptObject());
|
|
|
+ if (@object != null)
|
|
|
+ object.duration = duration;
|
|
|
+
|
|
|
+ return newEntity;
|
|
|
+ }
|
|
|
+
|
|
|
+ Entity@ spawnSound(const Vector3&in position, const string&in soundName, float duration)
|
|
|
+ {
|
|
|
+ Entity@ newEntity = scene.createEntity();
|
|
|
+
|
|
|
+ // Create a GameObject for managing the effect lifetime
|
|
|
+ ScriptInstance@ instance = newEntity.createComponent("ScriptInstance");
|
|
|
+ instance.setScriptClass(cache.getResource("ScriptFile", "Scripts/NinjaSnowWar.as"), "GameObject");
|
|
|
+ GameObject@ object = cast<GameObject>(instance.getScriptObject());
|
|
|
+ if (@object != null)
|
|
|
+ object.duration = duration;
|
|
|
+
|
|
|
+ // Create the sound channel
|
|
|
+ PositionalChannel@ channel = newEntity.createComponent("PositionalChannel", entity.getUniqueComponentName());
|
|
|
+ channel.setPosition(position);
|
|
|
+ channel.setAutoRemove(true);
|
|
|
+ channel.setDistanceAttenuation(200, 5000, 1);
|
|
|
+ Sound@ sound = cache.getResource("Sound", soundName);
|
|
|
+ channel.play(sound, sound.getFrequency());
|
|
|
+
|
|
|
+ return newEntity;
|
|
|
+ }
|
|
|
+
|
|
|
void handleEntityCollision(StringHash eventType, VariantMap& eventData)
|
|
|
{
|
|
|
Entity@ otherEntity = eventData["OtherEntity"].getEntity();
|
|
|
- // If the other entity does not have a ScriptInstance component, it's static world geometry
|
|
|
- if (!otherEntity.hasComponent("ScriptInstance"))
|
|
|
- handleWorldCollision(eventData);
|
|
|
- else
|
|
|
- handleObjectCollision(otherEntity, eventData);
|
|
|
+ RigidBody@ otherBody = eventData["OtherBody"].getRigidBody();
|
|
|
+
|
|
|
+ // If the other rigid body belongs to static geometry, perform world collision
|
|
|
+ if (otherBody.getCollisionGroup() == 2)
|
|
|
+ worldCollision(eventData);
|
|
|
+
|
|
|
+ // If the other entity is scripted, perform object-to-object collision
|
|
|
+ GameObject@ otherObject = cast<GameObject>(otherEntity.getScriptObject());
|
|
|
+ if (@otherObject != null)
|
|
|
+ objectCollision(otherEntity, otherObject, eventData);
|
|
|
}
|
|
|
|
|
|
- void handleWorldCollision(VariantMap& eventData)
|
|
|
+ void worldCollision(VariantMap& eventData)
|
|
|
{
|
|
|
RigidBody@ body = entity.getComponent("RigidBody");
|
|
|
|
|
|
@@ -76,8 +187,8 @@ class GameObject : ScriptObject
|
|
|
if (onGround == true)
|
|
|
isSliding = false;
|
|
|
}
|
|
|
-
|
|
|
- void handleObjectCollision(Entity@ otherEntity, VariantMap& eventData)
|
|
|
+
|
|
|
+ void objectCollision(Entity@ otherEntity, GameObject@ otherObject, VariantMap& eventData)
|
|
|
{
|
|
|
}
|
|
|
|
|
|
@@ -96,51 +207,4 @@ class GameObject : ScriptObject
|
|
|
isSliding = false;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- Entity@ spawnObject(const string&in className, const Vector3&in position, const Quaternion&in rotation)
|
|
|
- {
|
|
|
- Entity@ newEntity = scene.createEntity();
|
|
|
-
|
|
|
- // Create the ScriptInstance with specified class
|
|
|
- ScriptInstance@ instance = newEntity.createComponent("ScriptInstance");
|
|
|
- instance.setScriptClass(cache.getResource("ScriptFile", "Scripts/NinjaSnowWar.as"), className);
|
|
|
- GameObject@ object = cast<GameObject>(instance.getScriptObject());
|
|
|
- if (@object != null)
|
|
|
- object.create(position, rotation);
|
|
|
-
|
|
|
- return newEntity;
|
|
|
- }
|
|
|
-
|
|
|
- Entity@ spawnParticleEffect(const string&in effectName, float duration, const Vector3&in position, const Quaternion&in rotation)
|
|
|
- {
|
|
|
- Entity@ newEntity = scene.createEntity();
|
|
|
-
|
|
|
- // Create the particle emitter
|
|
|
- ParticleEmitter@ emitter = newEntity.createComponent("ParticleEmitter");
|
|
|
- emitter.loadParameters(cache.getResource("XMLFile", effectName));
|
|
|
- emitter.setPosition(position);
|
|
|
- emitter.setRotation(rotation);
|
|
|
-
|
|
|
- // Create a GameObject for managing the effect lifetime
|
|
|
- ScriptInstance@ instance = newEntity.createComponent("ScriptInstance");
|
|
|
- instance.setScriptClass(cache.getResource("ScriptFile", "Scripts/NinjaSnowWar.as"), "GameObject");
|
|
|
- GameObject@ object = cast<GameObject>(instance.getScriptObject());
|
|
|
- if (@object != null)
|
|
|
- object.duration = duration;
|
|
|
-
|
|
|
- return newEntity;
|
|
|
- }
|
|
|
-
|
|
|
- void playSound(const string&in soundName)
|
|
|
- {
|
|
|
- RigidBody@ body = entity.getComponent("RigidBody");
|
|
|
- if (@body == null)
|
|
|
- return;
|
|
|
- PositionalChannel@ channel = entity.createComponent("PositionalChannel", entity.getUniqueComponentName());
|
|
|
- body.addChild(channel);
|
|
|
- channel.setAutoRemove(true);
|
|
|
- channel.setDistanceAttenuation(200, 5000, 1);
|
|
|
- Sound@ sound = cache.getResource("Sound", soundName);
|
|
|
- channel.play(sound, sound.getFrequency());
|
|
|
- }
|
|
|
}
|