diff --git a/Resources/scenes/debugScene.xml b/Resources/scenes/debugScene.xml
index 456c249..62da31c 100644
--- a/Resources/scenes/debugScene.xml
+++ b/Resources/scenes/debugScene.xml
@@ -39,15 +39,15 @@
-
+
-
+
-
+
diff --git a/Resources/scripts/.grunt_behaviour.lua.swp b/Resources/scripts/.grunt_behaviour.lua.swp
deleted file mode 100644
index df49588..0000000
Binary files a/Resources/scripts/.grunt_behaviour.lua.swp and /dev/null differ
diff --git a/Resources/scripts/grunt_behaviour.lua b/Resources/scripts/ai/grunt_behaviour.lua
similarity index 100%
rename from Resources/scripts/grunt_behaviour.lua
rename to Resources/scripts/ai/grunt_behaviour.lua
diff --git a/Resources/scripts/scared_behaviour.lua b/Resources/scripts/ai/scared_behaviour.lua
similarity index 100%
rename from Resources/scripts/scared_behaviour.lua
rename to Resources/scripts/ai/scared_behaviour.lua
diff --git a/Resources/scripts/shotgun_script.lua b/Resources/scripts/weapons/shotgun_script.lua
similarity index 52%
rename from Resources/scripts/shotgun_script.lua
rename to Resources/scripts/weapons/shotgun_script.lua
index 4c8e4b6..f2f45aa 100644
--- a/Resources/scripts/shotgun_script.lua
+++ b/Resources/scripts/weapons/shotgun_script.lua
@@ -1,17 +1,21 @@
+
+function length(vec)
+ return math.sqrt(vec.x^2 + vec.y^2)
+end
+
-- How bullets are produced make sure to generate bullet data, then pass it through createBullet
function onShoot()
rot = weapon.wielder.rotation
data = weapon:genBulletData()
- for i=1, 10 do
- spread = math.random(-20, 20)
- print("direction x: "..data.direction.x.." y: "..data.direction.y);
+ for i=-20, 20, 10 do
+ spread = i
data.direction.x = math.cos(math.rad(rot + spread))
data.direction.y = math.sin(math.rad(rot + spread))
weapon:createBullet(data)
end
end
--- How the bulllet, target or wielder respond when the bullet hits a target.
-function onHit(target)
-
+-- How the bullet, target or wielder respond when the bullet hits a target.
+function onHit(target, bullet, normal)
+ target.physics.rigidBody:applyForce(vec3.new(normal.x, normal.y, 0.0), 5000.0);
end
diff --git a/Resources/weapons/bubblegun.xml b/Resources/weapons/bubblegun.xml
index 96ab369..ff0536d 100644
--- a/Resources/weapons/bubblegun.xml
+++ b/Resources/weapons/bubblegun.xml
@@ -12,7 +12,7 @@
-
+
diff --git a/YuppleMayham/include/gameplay/weapons/weapon.h b/YuppleMayham/include/gameplay/weapons/weapon.h
index ec4befd..800690a 100644
--- a/YuppleMayham/include/gameplay/weapons/weapon.h
+++ b/YuppleMayham/include/gameplay/weapons/weapon.h
@@ -21,6 +21,7 @@ class EventManager;
class ResourceManager;
class GameActor;
class WeaponScript;
+struct PhysicsComponent;
struct WeaponData;
class Weapon : public Entity, public std::enable_shared_from_this
@@ -45,7 +46,7 @@ public:
float speedMod;
float dropMod;
};
- void onHitCallback(std::shared_ptr target);
+ void onHitCallback(std::shared_ptr target, std::shared_ptr bullet, const glm::vec2& normal);
glm::vec2 getWeaponSize() const { return weaponSize; }
// This is the offset from the center right point of the weapon to the barrel of the gun defined in
diff --git a/YuppleMayham/include/utility/events.h b/YuppleMayham/include/utility/events.h
index d36431a..c33811c 100644
--- a/YuppleMayham/include/utility/events.h
+++ b/YuppleMayham/include/utility/events.h
@@ -33,10 +33,13 @@ public:
class BulletCollideEvent : public Event {
public:
- BulletCollideEvent(const unsigned int ownerID, const unsigned int victimID) : ownerID(ownerID), victimID(victimID) {}
+ BulletCollideEvent(const unsigned int ownerID, const unsigned int victimID, const std::shared_ptr& bullet, const glm::vec2& normal)
+ : ownerID(ownerID), victimID(victimID), bullet(bullet), normal(normal){}
std::string getType() const override { return "OnBulletCollide"; }
unsigned int ownerID;
unsigned int victimID;
+ std::shared_ptr bullet;
+ glm::vec2 normal;
};
class DirectionChangeEvent : public Event {
diff --git a/YuppleMayham/src/gameplay/physics.cpp b/YuppleMayham/src/gameplay/physics.cpp
index 11a44eb..61c4898 100644
--- a/YuppleMayham/src/gameplay/physics.cpp
+++ b/YuppleMayham/src/gameplay/physics.cpp
@@ -101,16 +101,18 @@ void PhysicsEngine::resolvePossibleCollisions()
if (glm::length(distance) < sumOfRadius)
{
// We got impact!
+ glm::vec3 normal = distance / glm::length(distance);
// That impact is a bullet hitting a gameactor!
- if (objs.first->isBullet || objs.second->isBullet && !(objs.first->isBullet && objs.second->isBullet))
+ if ((objs.first->isBullet || objs.second->isBullet) && !(objs.first->isBullet && objs.second->isBullet))
{
eventManager->notify(std::make_shared(
- (objs.first->isBullet) ? objs.first->ID : objs.second->ID,
- (!objs.first->isBullet) ? objs.first->ID : objs.second->ID
+ ( objs.first->isBullet) ? objs.first->ID : objs.second->ID,
+ (!objs.first->isBullet) ? objs.first->ID : objs.second->ID,
+ std::make_shared(( objs.first->isBullet) ? objs.first : objs.second),
+ normal
));
}
// Apply impulse force
- glm::vec3 normal = distance / glm::length(distance);
float penetrationDepth = sumOfRadius - glm::length(distance);
glm::vec3 correctionVector = normal * (penetrationDepth / ((1 / objs.first->rigidBody.mass) + (1 / objs.second->rigidBody.mass)));
glm::vec3 vrel = objs.first->rigidBody.velocity - objs.second->rigidBody.velocity;
diff --git a/YuppleMayham/src/gameplay/scene.cpp b/YuppleMayham/src/gameplay/scene.cpp
index deaa18f..8cf63d1 100644
--- a/YuppleMayham/src/gameplay/scene.cpp
+++ b/YuppleMayham/src/gameplay/scene.cpp
@@ -31,6 +31,7 @@ Scene::Scene(SceneType sceneType, std::shared_ptr resources) :
physicsEngine = std::make_shared();
eventManager = std::make_shared();
physicsEngine->hookEventManager(eventManager);
+ hookSceneEvents();
if (sceneType == SCENE_SHOOTER)
loadDebugShooterScene();
}
@@ -150,7 +151,7 @@ void Scene::hookSceneEvents()
std::shared_ptr shooter = getGameActorByID(collideEvent->ownerID);
std::shared_ptr target = getGameActorByID(collideEvent->victimID);
if (shooter && target)
- shooter->getHeldWeapon()->onHitCallback(target);
+ shooter->getHeldWeapon()->onHitCallback(target, collideEvent->bullet, collideEvent->normal);
});
}
diff --git a/YuppleMayham/src/gameplay/weapons/weapon.cpp b/YuppleMayham/src/gameplay/weapons/weapon.cpp
index 96d6f9c..e69d741 100644
--- a/YuppleMayham/src/gameplay/weapons/weapon.cpp
+++ b/YuppleMayham/src/gameplay/weapons/weapon.cpp
@@ -86,11 +86,11 @@ void Weapon::attachScript(const std::shared_ptr& script)
std::cout << "weapon state bound" << std::endl;
}
-void Weapon::onHitCallback(std::shared_ptr target)
+void Weapon::onHitCallback(std::shared_ptr target, std::shared_ptr bullet, const glm::vec2& normal)
{
if (weaponScript && weaponScript->lua["onHit"].valid())
{
- auto result = weaponScript->lua["onHit"](target);
+ auto result = weaponScript->lua["onHit"](target, bullet, normal);
if (!result.valid())
{
sol::error err = result;
diff --git a/YuppleMayham/src/utility/script.cpp b/YuppleMayham/src/utility/script.cpp
index 0975d53..a162a7f 100644
--- a/YuppleMayham/src/utility/script.cpp
+++ b/YuppleMayham/src/utility/script.cpp
@@ -7,31 +7,60 @@
void Script::registerGlobalUserTypes()
{
+ auto vec3_mult_overloads = sol::overload(
+ [](const glm::vec3& v1, const glm::vec3& v2) -> glm::vec3 {return v1 * v2; },
+ [](const glm::vec3& v1, const float f) -> glm::vec3 { return v1 * f; },
+ [](const float f, const glm::vec3& v1) -> glm::vec3 { return f * v1; }
+ );
+
+ auto vec2_mult_overloads = sol::overload(
+ [](const glm::vec2& v1, const glm::vec2& v2) -> glm::vec2 { return v1 * v2; },
+ [](const glm::vec2& v1, const float f) -> glm::vec2 { return v1 * f; },
+ [](const float f, const glm::vec3& v1) -> glm::vec2 { return f * v1; }
+ );
+
+ auto vec3_div_overloads = sol::overload(
+ [](const glm::vec3& v1, const glm::vec3& v2) -> glm::vec3 {return v1 / v2; },
+ [](const glm::vec3& v1, const float f) -> glm::vec3 { return v1 / f; },
+ [](const float f, const glm::vec3& v1) -> glm::vec3 { return f / v1; }
+ );
+
+ auto vec2_div_overloads = sol::overload(
+ [](const glm::vec2& v1, const glm::vec2& v2) -> glm::vec2 { return v1 / v2; },
+ [](const glm::vec2& v1, const float f) -> glm::vec2 { return v1 / f; },
+ [](const float f, const glm::vec3& v1) -> glm::vec2 { return f / v1; }
+ );
+
// These usertypes are constant for every script
lua.new_usertype("vec3",
sol::constructors(),
+ sol::meta_function::multiplication, vec3_mult_overloads,
+ sol::meta_function::division, vec3_div_overloads,
"x", &glm::vec3::x,
"y", &glm::vec3::y,
"z", &glm::vec3::z);
lua.new_usertype("vec2",
sol::constructors(),
+ sol::meta_function::multiplication, vec2_mult_overloads,
+ sol::meta_function::division, vec2_div_overloads,
"x", &glm::vec2::x,
"y", &glm::vec2::y);
lua.new_usertype("GameActor",
// properties
"position", sol::property(&GameActor::getCenter, &GameActor::setPosition),
"rotation", sol::property(&GameActor::getRotation, &GameActor::setRotation),
+ "physics", sol::property(&GameActor::getPhysicsComponent),
// methods
"moveUp", &GameActor::moveUp,
"moveDown", &GameActor::moveDown,
"moveLeft", &GameActor::moveLeft,
"moveRight", &GameActor::moveRight,
"moveForward", &GameActor::moveForward,
- "shoot", &GameActor::fireWeapon,
- "physics", &GameActor::getPhysicsComponent);
+ "shoot", &GameActor::fireWeapon);
lua.new_usertype("PhysicsComponent",
"rigidBody", &PhysicsComponent::rigidBody);
lua.new_usertype("RigidBody",
+ "position", &PhysicsComponent::RigidBody::position,
"mass", &PhysicsComponent::RigidBody::mass,
"veloctiy", &PhysicsComponent::RigidBody::velocity,
"applyForce", &PhysicsComponent::RigidBody::applyForce);