diff --git a/.gitignore b/.gitignore index a529989..6fdae60 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,4 @@ cmake_install.cmake Makefile saland !src/saland -nbproject +nbproject \ No newline at end of file diff --git a/src/saland/Game.cpp b/src/saland/Game.cpp index b5080d2..73ec216 100644 --- a/src/saland/Game.cpp +++ b/src/saland/Game.cpp @@ -23,6 +23,7 @@ https://github.com/sago007/saland #include +#include "GameUpdates.hpp" #include "GameRegion.hpp" #include "Game.hpp" #include "model/World.hpp" @@ -40,15 +41,6 @@ https://github.com/sago007/saland int32 velocityIterations = 6; int32 positionIterations = 2; -class Projectile : public Placeable { -public: - bool active = true; - float directionX = 1; - float directionY = 1; - float velocity = 1.0f; - float timeToLive = 2000.0; - std::shared_ptr fired_by; -}; /** * This sort method sorts the elements from furthest to screen to closest to screen, so that elemnets closer to the screen will be drawn last @@ -352,82 +344,7 @@ void Game::ProcessInput(const SDL_Event& event, bool& processed) { } } -static void SetDesiredVelocity(b2Body* body, float x, float y) { - b2Vec2 vel = body->GetLinearVelocity(); - float velChangeX = x - vel.x; - float velChangeY = y - vel.y; - float impulseX = body->GetMass() * velChangeX; //disregard time factor - float impulseY = body->GetMass() * velChangeY; //disregard time factor - //std::cout << body->GetMass() << " " << impulseX << "\n"; - body->ApplyLinearImpulse(b2Vec2(impulseX, impulseY), body->GetWorldCenter(), true); -} - -static void SetCreatureMovementEntity(Creature *entity, float directionX, float directionY) { - float deltaX = directionX; - float deltaY = directionY; - - if (deltaX == 0.0f && deltaY == 0.0f) { - entity->moving = false; - SetDesiredVelocity(entity->body, 0, 0); - return; - } - entity->moving = true; - if (deltaX * deltaX + deltaY * deltaY > 1.5f) { - deltaX *= 0.7071067811865476f; //sqrt(0.5) - deltaY *= 0.7071067811865476f; //sqrt(0.5) - } - if (deltaY > 0.0f) { - entity->direction = 'S'; - } - if (deltaY < 0.0f) { - entity->direction = 'N'; - } - if (deltaX < 0.0f) { - entity->direction = 'W'; - } - if (deltaX > 0.0f) { - entity->direction = 'E'; - } - float speed = 500.0f; - SetDesiredVelocity(entity->body, deltaX*speed, deltaY * speed); -} - -static void UpdateHuman(Human *entity, float fDeltaTime) { - if (entity->castTimeRemaining > 0) { - entity->castTimeRemaining -= fDeltaTime; - } - if (entity->castTimeRemaining < 0) { - entity->castTimeRemaining = 0; - } - if (entity->castTimeRemaining != 0) { - entity->moveX = 0.0f; - entity->moveY = 0.0f; - } - SetCreatureMovementEntity(entity, entity->moveX, entity->moveY); - b2Vec2 place = entity->body->GetPosition(); - entity->X = place.x*pixel2unit; - entity->Y = place.y*pixel2unit; -} - -static void UpdateMonster(Monster *entity) { - SetCreatureMovementEntity(entity, entity->moveX, entity->moveY); - b2Vec2 place = entity->body->GetPosition(); - entity->X = place.x*pixel2unit; - entity->Y = place.y*pixel2unit; - if (entity->health <= 0.0) { - entity->removeMe = true; - } -} -static void UpdateProjectile(Projectile *entity, float fDeltaTime) { - entity->timeToLive -= fDeltaTime; - if (entity->timeToLive < 0.0f) { - entity->removeMe = true; - return; - } - entity->X -= entity->directionX*fDeltaTime*entity->velocity; - entity->Y -= entity->directionY*fDeltaTime*entity->velocity; -} static bool Intersect(const Placeable& p1, const Placeable& p2) { double distance = std::sqrt( std::pow(p1.X-p2.X, 2) + std::pow(p1.Y-p2.Y,2)); @@ -437,13 +354,7 @@ static bool Intersect(const Placeable& p1, const Placeable& p2) { return false; } -static void ProjectileHit(Projectile* p, Placeable* target) { - Monster* monster = dynamic_cast (target); - if (monster) { - monster->health -= 10.0f; - p->removeMe = true; - } -} + void Game::Update() { Uint32 nowTime = SDL_GetTicks(); diff --git a/src/saland/GameUpdates.cpp b/src/saland/GameUpdates.cpp new file mode 100644 index 0000000..f469c28 --- /dev/null +++ b/src/saland/GameUpdates.cpp @@ -0,0 +1,109 @@ +/* +=========================================================================== + * Saland Adventures +Copyright (C) 2014-2017 Poul Sander + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see http://www.gnu.org/licenses/ + +Source information and contacts persons can be found at +https://github.com/sago007/saland +=========================================================================== +*/ + +#include "GameUpdates.hpp" + +static void SetDesiredVelocity(b2Body* body, float x, float y) { + b2Vec2 vel = body->GetLinearVelocity(); + float velChangeX = x - vel.x; + float velChangeY = y - vel.y; + float impulseX = body->GetMass() * velChangeX; //disregard time factor + float impulseY = body->GetMass() * velChangeY; //disregard time factor + //std::cout << body->GetMass() << " " << impulseX << "\n"; + body->ApplyLinearImpulse(b2Vec2(impulseX, impulseY), body->GetWorldCenter(), true); +} + +static void SetCreatureMovementEntity(Creature *entity, float directionX, float directionY) { + float deltaX = directionX; + float deltaY = directionY; + + if (deltaX == 0.0f && deltaY == 0.0f) { + entity->moving = false; + SetDesiredVelocity(entity->body, 0, 0); + return; + } + entity->moving = true; + if (deltaX * deltaX + deltaY * deltaY > 1.5f) { + deltaX *= 0.7071067811865476f; //sqrt(0.5) + deltaY *= 0.7071067811865476f; //sqrt(0.5) + } + if (deltaY > 0.0f) { + entity->direction = 'S'; + } + if (deltaY < 0.0f) { + entity->direction = 'N'; + } + if (deltaX < 0.0f) { + entity->direction = 'W'; + } + if (deltaX > 0.0f) { + entity->direction = 'E'; + } + float speed = 500.0f; + SetDesiredVelocity(entity->body, deltaX*speed, deltaY * speed); +} + +void UpdateHuman(Human *entity, float fDeltaTime) { + if (entity->castTimeRemaining > 0) { + entity->castTimeRemaining -= fDeltaTime; + } + if (entity->castTimeRemaining < 0) { + entity->castTimeRemaining = 0; + } + if (entity->castTimeRemaining != 0) { + entity->moveX = 0.0f; + entity->moveY = 0.0f; + } + SetCreatureMovementEntity(entity, entity->moveX, entity->moveY); + b2Vec2 place = entity->body->GetPosition(); + entity->X = place.x*pixel2unit; + entity->Y = place.y*pixel2unit; +} + +void UpdateMonster(Monster *entity) { + SetCreatureMovementEntity(entity, entity->moveX, entity->moveY); + b2Vec2 place = entity->body->GetPosition(); + entity->X = place.x*pixel2unit; + entity->Y = place.y*pixel2unit; + if (entity->health <= 0.0) { + entity->removeMe = true; + } +} + +void UpdateProjectile(Projectile *entity, float fDeltaTime) { + entity->timeToLive -= fDeltaTime; + if (entity->timeToLive < 0.0f) { + entity->removeMe = true; + return; + } + entity->X -= entity->directionX*fDeltaTime*entity->velocity; + entity->Y -= entity->directionY*fDeltaTime*entity->velocity; +} + +void ProjectileHit(Projectile* p, Placeable* target) { + Monster* monster = dynamic_cast (target); + if (monster) { + monster->health -= 10.0f; + p->removeMe = true; + } +} \ No newline at end of file diff --git a/src/saland/GameUpdates.hpp b/src/saland/GameUpdates.hpp new file mode 100644 index 0000000..007eb87 --- /dev/null +++ b/src/saland/GameUpdates.hpp @@ -0,0 +1,38 @@ +/* +=========================================================================== + * Saland Adventures +Copyright (C) 2014-2017 Poul Sander + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see http://www.gnu.org/licenses/ + +Source information and contacts persons can be found at +https://github.com/sago007/saland +=========================================================================== +*/ + +#ifndef GAMEUPDATES_HPP +#define GAMEUPDATES_HPP + +#include "globals.hpp" +#include "model/placeables.hpp" + + + +void ProjectileHit(Projectile* p, Placeable* target); +void UpdateHuman(Human *entity, float fDeltaTime); +void UpdateMonster(Monster *entity); +void UpdateProjectile(Projectile *entity, float fDeltaTime); + +#endif /* GAMEUPDATES_HPP */ + diff --git a/src/saland/model/placeables.hpp b/src/saland/model/placeables.hpp index 3264c82..364aa77 100644 --- a/src/saland/model/placeables.hpp +++ b/src/saland/model/placeables.hpp @@ -69,5 +69,15 @@ public: std::string race = "bat"; }; +class Projectile : public Placeable { +public: + bool active = true; + float directionX = 1; + float directionY = 1; + float velocity = 1.0f; + float timeToLive = 2000.0; + std::shared_ptr fired_by; +}; + #endif /* PLACEABLES_HPP */