Move Thing to things/Thing. Added some basic doThink operations. Basically a Thing has a deque of Thoughts that are added by the Thing's Controller. A Thought takes some time to be realized (the default of which is 100ms), upon which the action takes place. At the moment, the Thought just does the action, but it may be worthwhile to create an Action deque for actions that could take some time to execute.

master
kts 2015-03-14 03:28:58 -07:00
parent 82e1b54819
commit 4c4cf00499
14 changed files with 156 additions and 79 deletions

View File

@ -100,8 +100,8 @@ xcopy /s /e /d /y "..\..\data" "$(OutDir)data\"</Command>
<ClCompile Include="..\..\src\render\Texture.cpp" /> <ClCompile Include="..\..\src\render\Texture.cpp" />
<ClCompile Include="..\..\src\render\Vec.cpp" /> <ClCompile Include="..\..\src\render\Vec.cpp" />
<ClCompile Include="..\..\src\states\GameState.cpp" /> <ClCompile Include="..\..\src\states\GameState.cpp" />
<ClCompile Include="..\..\src\Thing.cpp" />
<ClCompile Include="..\..\src\things\Denizen.cpp" /> <ClCompile Include="..\..\src\things\Denizen.cpp" />
<ClCompile Include="..\..\src\things\Thing.cpp" />
<ClCompile Include="..\..\src\Thought.cpp" /> <ClCompile Include="..\..\src\Thought.cpp" />
<ClCompile Include="..\..\src\Tile.cpp" /> <ClCompile Include="..\..\src\Tile.cpp" />
</ItemGroup> </ItemGroup>
@ -128,8 +128,8 @@ xcopy /s /e /d /y "..\..\data" "$(OutDir)data\"</Command>
<ClInclude Include="..\..\src\render\Vec.hpp" /> <ClInclude Include="..\..\src\render\Vec.hpp" />
<ClInclude Include="..\..\src\states\GameState.hpp" /> <ClInclude Include="..\..\src\states\GameState.hpp" />
<ClInclude Include="..\..\src\states\State.hpp" /> <ClInclude Include="..\..\src\states\State.hpp" />
<ClInclude Include="..\..\src\Thing.hpp" />
<ClInclude Include="..\..\src\things\Denizen.hpp" /> <ClInclude Include="..\..\src\things\Denizen.hpp" />
<ClInclude Include="..\..\src\things\Thing.hpp" />
<ClInclude Include="..\..\src\Thought.hpp" /> <ClInclude Include="..\..\src\Thought.hpp" />
<ClInclude Include="..\..\src\Tile.hpp" /> <ClInclude Include="..\..\src\Tile.hpp" />
</ItemGroup> </ItemGroup>

View File

@ -39,9 +39,6 @@
<ClCompile Include="..\..\src\QMap.cpp"> <ClCompile Include="..\..\src\QMap.cpp">
<Filter>Classes</Filter> <Filter>Classes</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\src\Thing.cpp">
<Filter>Classes</Filter>
</ClCompile>
<ClCompile Include="..\..\src\Thought.cpp"> <ClCompile Include="..\..\src\Thought.cpp">
<Filter>Classes</Filter> <Filter>Classes</Filter>
</ClCompile> </ClCompile>
@ -102,6 +99,9 @@
<ClCompile Include="..\..\src\main.cpp"> <ClCompile Include="..\..\src\main.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\src\things\Thing.cpp">
<Filter>Classes\Things</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\src\Controller.hpp"> <ClInclude Include="..\..\src\Controller.hpp">
@ -116,9 +116,6 @@
<ClInclude Include="..\..\src\QMap.hpp"> <ClInclude Include="..\..\src\QMap.hpp">
<Filter>Classes</Filter> <Filter>Classes</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\src\Thing.hpp">
<Filter>Classes</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Thought.hpp"> <ClInclude Include="..\..\src\Thought.hpp">
<Filter>Classes</Filter> <Filter>Classes</Filter>
</ClInclude> </ClInclude>
@ -182,5 +179,8 @@
<ClInclude Include="..\..\src\Log.hpp"> <ClInclude Include="..\..\src\Log.hpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\src\things\Thing.hpp">
<Filter>Classes\Things</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -33,11 +33,11 @@ int Core::initSystem() {
// get our displayyyy // get our displayyyy
SDL_DisplayMode dmode; SDL_DisplayMode dmode;
SDL_GetDesktopDisplayMode(0, &dmode); SDL_GetDesktopDisplayMode(0, &dmode);
v_width = dmode.w; v_width = dmode.w/2; // TEMP: just for a smaller window per-default
v_height = dmode.h; v_height = dmode.h/2;
v_flags = SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE; v_flags = SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE;
// create our windowww // create our windowww
if ((v_window = SDL_CreateWindow("GodEater", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, v_width, v_height, v_flags)) == NULL) { if ((v_window = SDL_CreateWindow("GodEater", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, v_width, v_height, v_flags)) == NULL) {
LOG(LOG_ERROR) << SDL_GetError(); LOG(LOG_ERROR) << SDL_GetError();
return 1; return 1;
} }

View File

@ -14,28 +14,28 @@ int PlayerController::onEvent(SDL_Event event) {
case SDLK_h: case SDLK_h:
case SDLK_KP_4: case SDLK_KP_4:
thought.type = Thought::MOVE; thought.type = Thought::MOVE;
thought.loc.x = thing->position.x-1; thought.loc.x = -1;
thing->pushThought(thought); thing->pushThought(thought);
break; break;
case SDLK_RIGHT: case SDLK_RIGHT:
case SDLK_l: case SDLK_l:
case SDLK_KP_6: case SDLK_KP_6:
thought.type = Thought::MOVE; thought.type = Thought::MOVE;
thought.loc.x = thing->position.x+1; thought.loc.x = 1;
thing->pushThought(thought); thing->pushThought(thought);
break; break;
case SDLK_UP: case SDLK_UP:
case SDLK_k: case SDLK_k:
case SDLK_KP_8: case SDLK_KP_8:
thought.type = Thought::MOVE; thought.type = Thought::MOVE;
thought.loc.y = thing->position.y-1; thought.loc.y = -1;
thing->pushThought(thought); thing->pushThought(thought);
break; break;
case SDLK_DOWN: case SDLK_DOWN:
case SDLK_j: case SDLK_j:
case SDLK_2: case SDLK_2:
thought.type = Thought::MOVE; thought.type = Thought::MOVE;
thought.loc.y = thing->position.y+1; thought.loc.y = 1;
thing->pushThought(thought); thing->pushThought(thought);
break; break;
} }

View File

@ -3,6 +3,7 @@
#include "Controller.hpp" #include "Controller.hpp"
#include "SDL.h" #include "SDL.h"
class PlayerController : Controller { class PlayerController : Controller {
friend class GameState;
public: public:
PlayerController(); PlayerController();
~PlayerController(); ~PlayerController();

View File

@ -1,21 +0,0 @@
#include "Thing.hpp"
/* ======== Constructor/Destructor ======== */
Thing::Thing() { flags = 0; sprite = NULL; }
Thing::~Thing() {}
/* ======== Interfaces ======== */
int Thing::doThink() {
return 0;
}
int Thing::onTouch(Thing *toucher) {
return 0;
}
/* ======== Adding/Access ======== */
int Thing::pushThought(Thought thought) {
thoughts.push_back(thought);
return thoughts.size();
}
/* ======== Etc. ======== */
bool destroyThing(Thing *thing) {
if (thing->flags & Thing::THING_DESTROY) return true;
return false;
}

View File

@ -1,35 +0,0 @@
#ifndef THING_HPP
#define THING_HPP
#include "Thought.hpp"
#include "RenderObject.hpp"
#include "Sprite.hpp"
#include <vector>
class Thought; // fwd declaration
class Thing {
friend class PlayerController;
friend class AiController;
public:
Thing();
~Thing();
// interface
virtual int doThink();
virtual int onTouch(Thing *thing);
//
int pushThought(Thought thought);
//
enum Flags {
THING_DESTROY = (1 << 1)
};
int flags;
protected:
Vec3 position;
Vec2 scale;
std::vector<Thought> thoughts;
//
Sprite *sprite;
};
bool destroyThing(Thing* thing);
#endif

View File

@ -1,5 +1,6 @@
#include "Thought.hpp" #include "Thought.hpp"
#include <stdlib.h> #include <stdlib.h>
#include <sstream>
Thought::Thought() { Thought::Thought() {
type = 0; type = 0;
@ -7,3 +8,9 @@ Thought::Thought() {
} }
Thought::~Thought() { Thought::~Thought() {
} }
std::string Thought::dumpInfo() {
std::ostringstream oss;
oss << type << ", " << loc.x << "x" << loc.y << "x" << loc.z;
return oss.str();
}

View File

@ -8,9 +8,10 @@ Put more plainly, a thought is an action in a Thing's event/action queue.
#ifndef THOUGHT_HPP #ifndef THOUGHT_HPP
#define THOUGHT_HPP #define THOUGHT_HPP
#include "Vec.hpp" #include "Vec.hpp"
#include "Thing.hpp" #include <string>
//#include "Thing.hpp"
class Thing; // fwd declaration //class Thing; // fwd declaration
class Thought { class Thought {
friend class Thing; friend class Thing;
@ -19,12 +20,13 @@ class Thought {
public: public:
Thought(); Thought();
~Thought(); ~Thought();
std::string dumpInfo();
enum Act { enum Act {
MOVE = (1 << 1), MOVE = (1 << 1),
ABILITY = (1 << 2), ABILITY = (1 << 2),
WAIT = (1 << 3) WAIT = (1 << 3)
}; };
protected: //protected:
int type; int type;
union { union {
Thing *target; Thing *target;

View File

@ -1,6 +1,7 @@
#include "GameState.hpp" #include "GameState.hpp"
#include "Core.hpp" #include "Core.hpp"
#include "Log.hpp" #include "Log.hpp"
#include "Denizen.hpp"
#include <algorithm> // remove_if #include <algorithm> // remove_if
/* ======== Constructor & Destructor ======== */ /* ======== Constructor & Destructor ======== */
@ -15,13 +16,18 @@ GameState::GameState() {
current_map = new QMap(16, 16); current_map = new QMap(16, 16);
current_map->setTile(4, 4, new Tile()); current_map->setTile(4, 4, new Tile());
pc.thing = new Denizen();
current_map->thing.push_back(pc.thing);
LOG(LOG_INFO); LOG(LOG_INFO);
} }
GameState::~GameState() { GameState::~GameState() {
} }
/* ======== Base Methods ======== */ /* ======== Base Methods ======== */
int GameState::onEvent(SDL_Event event) { int GameState::onEvent(SDL_Event event) {
LOG(LOG_INFO); if (pc.thing != NULL) {
pc.onEvent(event);
}
return 0; return 0;
} }
int GameState::doProcess(unsigned int ticks) { int GameState::doProcess(unsigned int ticks) {
@ -30,13 +36,26 @@ int GameState::doProcess(unsigned int ticks) {
std::vector<Thing*>::iterator thing_it, thing_end; std::vector<Thing*>::iterator thing_it, thing_end;
for (thing_it = current_map->thing.begin(), thing_end = current_map->thing.end(); thing_it != thing_end; ++thing_it) { for (thing_it = current_map->thing.begin(), thing_end = current_map->thing.end(); thing_it != thing_end; ++thing_it) {
Thing *thing = *thing_it; Thing *thing = *thing_it;
thing->doThink(); thing->doThink(ticks);
} }
// remove things desiring to be removed // remove things desiring to be removed
thing_it = std::remove_if(current_map->thing.begin(), current_map->thing.end(), destroyThing); thing_it = std::remove_if(current_map->thing.begin(), current_map->thing.end(), destroyThing);
current_map->thing.erase(thing_it, current_map->thing.end()); current_map->thing.erase(thing_it, current_map->thing.end());
// 3. for each thing, do their physics movement // 3. for each thing, do their physics movement
for (thing_it = current_map->thing.begin(), thing_end = current_map->thing.end(); thing_it != thing_end; ++thing_it) {
Thing *thing = *thing_it;
float x = thing->velocity.x;
float y = thing->velocity.y;
if (x != 0) thing->velocity.x += -x;
if (y != 0) thing->velocity.y += -y;
if (x != 0 && y != 0) {
thing->position.x += x;
thing->position.y += y;
LOG(LOG_INFO) << thing->position.x << "x" << thing->position.y;
}
//thing->doThink(ticks);
}
return 0; return 0;
} }
int GameState::doRender() { int GameState::doRender() {

View File

@ -1,10 +1,37 @@
#include "Denizen.hpp" #include "Denizen.hpp"
Denizen::Denizen() { Denizen::Denizen() {
name = "Denizen";
} }
Denizen::~Denizen() { Denizen::~Denizen() {
} }
int Denizen::doThink() { int Denizen::doThink(unsigned int ticks) {
// get new thought if we have nothing to think about
if (!thought_current.type) {
if (thoughts.size() > 0) {
thought_current = thoughts.front();
thought_delay = 100; // assume 100 ms
thoughts.pop_front();
}
}
// process current thought
if (thought_current.type) {
thought_elapsed += ticks;
if (thought_elapsed >= thought_delay) {
// attempt to do the associated action
switch(thought_current.type) {
case Thought::MOVE:
addVelocity(thought_current.loc);
break;
case Thought::ABILITY:
break;
case Thought::WAIT:
break;
}
thought_current.type = 0;
thought_elapsed = 0;
}
}
return 0; return 0;
} }
int Denizen::onTouch(Thing *thing) { int Denizen::onTouch(Thing *thing) {

View File

@ -2,12 +2,12 @@
#define DENIZEN_HPP #define DENIZEN_HPP
#include "Thing.hpp" #include "Thing.hpp"
class Denizen : Thing { class Denizen : public Thing {
public: public:
Denizen(); Denizen();
~Denizen(); ~Denizen();
// interface // interface
virtual int doThink(); virtual int doThink(unsigned int ticks);
virtual int onTouch(Thing *thing); virtual int onTouch(Thing *thing);
}; };

View File

@ -0,0 +1,31 @@
#include "Thing.hpp"
#include "Log.hpp"
/* ======== Constructor/Destructor ======== */
Thing::Thing() { flags = 0; sprite = NULL; name = "Thing"; thought_current.type = 0; thought_delay = thought_elapsed = 0; }
Thing::~Thing() {}
/* ======== Interfaces ======== */
int Thing::doThink(unsigned int ticks) {
return 0;
}
int Thing::onTouch(Thing *toucher) {
return 0;
}
/* ======== Adding/Access ======== */
int Thing::pushThought(Thought thought) {
if (thoughts.size() > thoughts_max) return 0;
LOG(LOG_INFO) << name << " has a new thought( " << thoughts.size()+1 << "): " << thought.dumpInfo();
thoughts.push_back(thought);
return thoughts.size();
}
/* ======== Velocity/Position ======== */
void Thing::addVelocity(Vec3 vel) {
velocity += vel;
}
void Thing::setVelocity(Vec3 vel) {
velocity = vel;
}
/* ======== Etc. ======== */
bool destroyThing(Thing *thing) {
if (thing->flags & Thing::THING_DESTROY) return true;
return false;
}

View File

@ -0,0 +1,46 @@
#ifndef THING_HPP
#define THING_HPP
#include "Thought.hpp"
#include "RenderObject.hpp"
#include "Sprite.hpp"
#include <deque>
#include <string>
//class Thought; // fwd declaration
class Thing {
friend class GameState;
friend class PlayerController;
friend class AiController;
public:
Thing();
~Thing();
// interface
virtual int doThink(unsigned int ticks);
virtual int onTouch(Thing *thing);
//
int pushThought(Thought thought);
//
enum Flags {
THING_DESTROY = (1 << 1)
};
int flags;
// position and velocity
void addVelocity(Vec3 vel);
void setVelocity(Vec3 vel);
protected:
std::string name;
Vec3 velocity; // although this is a rug-like, velocity makes life easier
Vec3 position;
Vec2 scale;
unsigned int thoughts_max; // our maximum # of thoughts
std::deque<Thought> thoughts; // our queue of thoughts
Thought thought_current; // current thought, popped from thoughts
unsigned int thought_delay; // time required for current thought
unsigned int thought_elapsed; // elapsed time spent on current thought
//
Sprite *sprite;
};
bool destroyThing(Thing* thing);
#endif