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.
parent
82e1b54819
commit
4c4cf00499
|
@ -100,8 +100,8 @@ xcopy /s /e /d /y "..\..\data" "$(OutDir)data\"</Command>
|
|||
<ClCompile Include="..\..\src\render\Texture.cpp" />
|
||||
<ClCompile Include="..\..\src\render\Vec.cpp" />
|
||||
<ClCompile Include="..\..\src\states\GameState.cpp" />
|
||||
<ClCompile Include="..\..\src\Thing.cpp" />
|
||||
<ClCompile Include="..\..\src\things\Denizen.cpp" />
|
||||
<ClCompile Include="..\..\src\things\Thing.cpp" />
|
||||
<ClCompile Include="..\..\src\Thought.cpp" />
|
||||
<ClCompile Include="..\..\src\Tile.cpp" />
|
||||
</ItemGroup>
|
||||
|
@ -128,8 +128,8 @@ xcopy /s /e /d /y "..\..\data" "$(OutDir)data\"</Command>
|
|||
<ClInclude Include="..\..\src\render\Vec.hpp" />
|
||||
<ClInclude Include="..\..\src\states\GameState.hpp" />
|
||||
<ClInclude Include="..\..\src\states\State.hpp" />
|
||||
<ClInclude Include="..\..\src\Thing.hpp" />
|
||||
<ClInclude Include="..\..\src\things\Denizen.hpp" />
|
||||
<ClInclude Include="..\..\src\things\Thing.hpp" />
|
||||
<ClInclude Include="..\..\src\Thought.hpp" />
|
||||
<ClInclude Include="..\..\src\Tile.hpp" />
|
||||
</ItemGroup>
|
||||
|
|
|
@ -39,9 +39,6 @@
|
|||
<ClCompile Include="..\..\src\QMap.cpp">
|
||||
<Filter>Classes</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\src\Thing.cpp">
|
||||
<Filter>Classes</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\src\Thought.cpp">
|
||||
<Filter>Classes</Filter>
|
||||
</ClCompile>
|
||||
|
@ -102,6 +99,9 @@
|
|||
<ClCompile Include="..\..\src\main.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\src\things\Thing.cpp">
|
||||
<Filter>Classes\Things</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="..\..\src\Controller.hpp">
|
||||
|
@ -116,9 +116,6 @@
|
|||
<ClInclude Include="..\..\src\QMap.hpp">
|
||||
<Filter>Classes</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\Thing.hpp">
|
||||
<Filter>Classes</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\Thought.hpp">
|
||||
<Filter>Classes</Filter>
|
||||
</ClInclude>
|
||||
|
@ -182,5 +179,8 @@
|
|||
<ClInclude Include="..\..\src\Log.hpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\things\Thing.hpp">
|
||||
<Filter>Classes\Things</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
|
@ -33,11 +33,11 @@ int Core::initSystem() {
|
|||
// get our displayyyy
|
||||
SDL_DisplayMode dmode;
|
||||
SDL_GetDesktopDisplayMode(0, &dmode);
|
||||
v_width = dmode.w;
|
||||
v_height = dmode.h;
|
||||
v_width = dmode.w/2; // TEMP: just for a smaller window per-default
|
||||
v_height = dmode.h/2;
|
||||
v_flags = SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE;
|
||||
// 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();
|
||||
return 1;
|
||||
}
|
||||
|
|
|
@ -14,28 +14,28 @@ int PlayerController::onEvent(SDL_Event event) {
|
|||
case SDLK_h:
|
||||
case SDLK_KP_4:
|
||||
thought.type = Thought::MOVE;
|
||||
thought.loc.x = thing->position.x-1;
|
||||
thought.loc.x = -1;
|
||||
thing->pushThought(thought);
|
||||
break;
|
||||
case SDLK_RIGHT:
|
||||
case SDLK_l:
|
||||
case SDLK_KP_6:
|
||||
thought.type = Thought::MOVE;
|
||||
thought.loc.x = thing->position.x+1;
|
||||
thought.loc.x = 1;
|
||||
thing->pushThought(thought);
|
||||
break;
|
||||
case SDLK_UP:
|
||||
case SDLK_k:
|
||||
case SDLK_KP_8:
|
||||
thought.type = Thought::MOVE;
|
||||
thought.loc.y = thing->position.y-1;
|
||||
thought.loc.y = -1;
|
||||
thing->pushThought(thought);
|
||||
break;
|
||||
case SDLK_DOWN:
|
||||
case SDLK_j:
|
||||
case SDLK_2:
|
||||
thought.type = Thought::MOVE;
|
||||
thought.loc.y = thing->position.y+1;
|
||||
thought.loc.y = 1;
|
||||
thing->pushThought(thought);
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#include "Controller.hpp"
|
||||
#include "SDL.h"
|
||||
class PlayerController : Controller {
|
||||
friend class GameState;
|
||||
public:
|
||||
PlayerController();
|
||||
~PlayerController();
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -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
|
|
@ -1,5 +1,6 @@
|
|||
#include "Thought.hpp"
|
||||
#include <stdlib.h>
|
||||
#include <sstream>
|
||||
|
||||
Thought::Thought() {
|
||||
type = 0;
|
||||
|
@ -7,3 +8,9 @@ Thought::Thought() {
|
|||
}
|
||||
Thought::~Thought() {
|
||||
}
|
||||
|
||||
std::string Thought::dumpInfo() {
|
||||
std::ostringstream oss;
|
||||
oss << type << ", " << loc.x << "x" << loc.y << "x" << loc.z;
|
||||
return oss.str();
|
||||
}
|
|
@ -8,9 +8,10 @@ Put more plainly, a thought is an action in a Thing's event/action queue.
|
|||
#ifndef THOUGHT_HPP
|
||||
#define THOUGHT_HPP
|
||||
#include "Vec.hpp"
|
||||
#include "Thing.hpp"
|
||||
#include <string>
|
||||
//#include "Thing.hpp"
|
||||
|
||||
class Thing; // fwd declaration
|
||||
//class Thing; // fwd declaration
|
||||
|
||||
class Thought {
|
||||
friend class Thing;
|
||||
|
@ -19,12 +20,13 @@ class Thought {
|
|||
public:
|
||||
Thought();
|
||||
~Thought();
|
||||
std::string dumpInfo();
|
||||
enum Act {
|
||||
MOVE = (1 << 1),
|
||||
ABILITY = (1 << 2),
|
||||
WAIT = (1 << 3)
|
||||
};
|
||||
protected:
|
||||
//protected:
|
||||
int type;
|
||||
union {
|
||||
Thing *target;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include "GameState.hpp"
|
||||
#include "Core.hpp"
|
||||
#include "Log.hpp"
|
||||
#include "Denizen.hpp"
|
||||
#include <algorithm> // remove_if
|
||||
|
||||
/* ======== Constructor & Destructor ======== */
|
||||
|
@ -15,13 +16,18 @@ GameState::GameState() {
|
|||
current_map = new QMap(16, 16);
|
||||
current_map->setTile(4, 4, new Tile());
|
||||
|
||||
pc.thing = new Denizen();
|
||||
current_map->thing.push_back(pc.thing);
|
||||
|
||||
LOG(LOG_INFO);
|
||||
}
|
||||
GameState::~GameState() {
|
||||
}
|
||||
/* ======== Base Methods ======== */
|
||||
int GameState::onEvent(SDL_Event event) {
|
||||
LOG(LOG_INFO);
|
||||
if (pc.thing != NULL) {
|
||||
pc.onEvent(event);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int GameState::doProcess(unsigned int ticks) {
|
||||
|
@ -30,13 +36,26 @@ int GameState::doProcess(unsigned int ticks) {
|
|||
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) {
|
||||
Thing *thing = *thing_it;
|
||||
thing->doThink();
|
||||
thing->doThink(ticks);
|
||||
}
|
||||
// remove things desiring to be removed
|
||||
thing_it = std::remove_if(current_map->thing.begin(), current_map->thing.end(), destroyThing);
|
||||
current_map->thing.erase(thing_it, current_map->thing.end());
|
||||
|
||||
// 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;
|
||||
}
|
||||
int GameState::doRender() {
|
||||
|
|
|
@ -1,10 +1,37 @@
|
|||
#include "Denizen.hpp"
|
||||
Denizen::Denizen() {
|
||||
name = "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;
|
||||
}
|
||||
int Denizen::onTouch(Thing *thing) {
|
||||
|
|
|
@ -2,12 +2,12 @@
|
|||
#define DENIZEN_HPP
|
||||
#include "Thing.hpp"
|
||||
|
||||
class Denizen : Thing {
|
||||
class Denizen : public Thing {
|
||||
public:
|
||||
Denizen();
|
||||
~Denizen();
|
||||
// interface
|
||||
virtual int doThink();
|
||||
virtual int doThink(unsigned int ticks);
|
||||
virtual int onTouch(Thing *thing);
|
||||
};
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -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
|
Loading…
Reference in New Issue