Skip to content

Commit

Permalink
Add event slots,-parsing, remove Behaviour, close #31 #22
Browse files Browse the repository at this point in the history
  • Loading branch information
VectorWolf committed Jul 6, 2019
1 parent a4950e7 commit c24a034
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 99 deletions.
3 changes: 2 additions & 1 deletion data/events.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
</tile>
<tile id="1" type="AeMoveDirection">
<properties>
<property name="NAME" value="Moveup"/>
<property name="DIRECTION" value="DOWN"/>
<property name="NAME" value="Movedown"/>
</properties>
</tile>
</tileset>
6 changes: 4 additions & 2 deletions data/example.tmx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<tile id="0" type="ACTOR_TEMPLATE">
<properties>
<property name="ACTOR_NAME" value="DEF_BLOCK"/>
<property name="DIRECTION" value="RIGHT"/>
</properties>
<objectgroup draworder="index">
<object id="1" x="0" y="48" width="80" height="80"/>
Expand Down Expand Up @@ -178,9 +179,10 @@
</properties>
</imagelayer>
<objectgroup name="Objektebene 1">
<object id="1" name="Actor_1" gid="63" x="122.667" y="128.667" width="128" height="128">
<object id="1" name="PLAYER" gid="63" x="122.667" y="128.667" width="128" height="128">
<properties>
<property name="BEHAVIOUR" value="PLAYER"/>
<property name="DIRECTION" value="DOWN"/>
<property name="ON_IDLE" value="Movedown"/>
</properties>
</object>
<object id="2" name="Billy" gid="63" x="451" y="249" width="128" height="128"/>
Expand Down
8 changes: 4 additions & 4 deletions src/actor/actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ Actor::Actor(const ActorTemplate& templ, MapData* map) :
m_map {map},
m_type {templ.template_name},
m_base_speed {templ.speed},
m_AI {templ.AI},
m_direction {templ.direction},
m_hitbox {templ.hitbox},
m_animations {templ.animations}
Expand Down Expand Up @@ -78,7 +77,7 @@ tinyxml2::XMLError Actor::init_actor(tinyxml2::XMLElement* source) {
XMLElement* p_tile_properties = source->FirstChildElement("properties");
if(p_tile_properties != nullptr) {
XMLElement* p_property = p_tile_properties->FirstChildElement("property");
eResult = parse_actor_properties(p_property, m_base_speed, m_AI, m_direction);
eResult = m_map->parse_actor_properties(p_property, m_base_speed, m_direction, m_response);
if(eResult != XML_SUCCESS) {
std::cerr << "Failed at parsing actor properties for actor: " << m_name << "\n";
return eResult;
Expand Down Expand Up @@ -181,8 +180,9 @@ bool Actor::process_events() {
else if(signal == EventSignal::die) {return false;}
}
}
else {
animate(AnimationType::idle, m_direction);
if(m_event_pipeline.empty()) {
respond(Response::on_idle);
//animate(AnimationType::idle, m_direction);
// AI and Player behaviour stuff
}
return true;
Expand Down
5 changes: 3 additions & 2 deletions src/actor/actor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class Actor{
bool collide(const SDL_Rect* rect, int& x_depth, int& y_depth) const;
bool respond(Response r, Actor* cause = nullptr, SDL_Keysym key = SDL_Keysym());

Behaviour get_behaviour() const {return m_AI;}
AnimationType get_animation() const {return m_anim_state;}
Direction get_direction() const {return m_direction;}
std::string get_name() const {return m_name;}
Expand All @@ -69,6 +68,8 @@ class Actor{
bool is_blocked(std::string event_type) const;
bool in_cooldown(std::string event_type) const;



private:
MapData* m_map;

Expand All @@ -80,12 +81,12 @@ class Actor{
std::string m_type;
float m_base_speed;

Behaviour m_AI;
AnimationType m_anim_state = AnimationType::idle; ///< Currently active animation
Direction m_direction; ///< Current direction facing
SDL_Rect m_hitbox;
std::map<AnimationType, std::map<Direction, Tile>> m_animations; ///< 2D Map which stores all animation tiles
std::map<Response, ActorEvent*> m_response; ///< Map which yields events for response values

std::map<std::string, Uint32> m_timestamp; ///< Map holding timestamps for use as cooldown functionality
std::map<std::string, bool> m_block; ///< Map determinig if the event pipeline is blocked for a specific event type
std::vector<ActorEvent*> m_event_pipeline; ///< Vector of current events to be processed
Expand Down
7 changes: 3 additions & 4 deletions src/core/gameinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,13 @@ bool GameInfo::load_map(std::string mapfile) {
}

bool GameInfo::fetch_player() {
Behaviour B = Behaviour::player;
std::vector<Actor*> actor_list = m_map.get_actors(std::string(""),B);
std::vector<Actor*> actor_list = m_map.get_actors(std::string("PLAYER"));
if(actor_list.size() > 1) {
std::cerr << "Error: More than one actor with player behaviour!\n";
std::cerr << "Error: More than one actor called PLAYER!\n";
return false;
}
else if(actor_list.size() == 0) {
std::cerr << "Error: No actor with player behaviour found!\n";
std::cerr << "Error: No actor called PLAYER found!\n";
return false;
}
else {
Expand Down
3 changes: 1 addition & 2 deletions src/map/layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,14 +353,13 @@ void Layer::update() {
* @return Vector of conforming actors
* @note "invalid" value indicates that a parameter is ignored
*/
std::vector<Actor*> Layer::get_actors(std::string name, Behaviour behaviour, Direction direction, AnimationType animation) {
std::vector<Actor*> Layer::get_actors(std::string name, Direction direction, AnimationType animation) {
std::vector<Actor*> actor_list;
if(m_type == LayerType::object) {
for(Actor& actor : m_obj_grid) {
bool match = true;

if(name != "" && actor.get_name() != name) {match = false;}
if(behaviour != Behaviour::invalid && actor.get_behaviour() != behaviour) {match = false;}
if(direction != Direction::invalid && actor.get_direction() != direction) {match = false;}
if(animation != AnimationType::invalid && actor.get_animation() != animation) {match = false;}

Expand Down
2 changes: 1 addition & 1 deletion src/map/layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class Layer{

bool render(SDL_Rect* camera, const MapData& base_map) const;
void update();
std::vector<Actor*> get_actors(std::string name = "", Behaviour behaviour = Behaviour::invalid, Direction direction = Direction::invalid,
std::vector<Actor*> get_actors(std::string name = "", Direction direction = Direction::invalid,
AnimationType animation = AnimationType::invalid);
bool collide(const SDL_Rect* rect, int& x_max, int& y_max, const MapData& base_map, std::vector<Actor*>& collided);

Expand Down
71 changes: 68 additions & 3 deletions src/map/mapdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,10 @@ void MapData::update() {
* @return Vector of conforming actors
* @note "invalid" value indicates that a parameter is ignored
*/
std::vector<Actor*> MapData::get_actors(std::string name, Behaviour behaviour, Direction direction, AnimationType animation) {
std::vector<Actor*> MapData::get_actors(std::string name, Direction direction, AnimationType animation) {
std::vector<Actor*> actor_list;
for(Layer& layer : m_layers) {
std::vector<Actor*> sublist = layer.get_actors(name, behaviour, direction, animation);
std::vector<Actor*> sublist = layer.get_actors(name, direction, animation);
actor_list.insert(actor_list.end(),sublist.begin(),sublist.end());
}
return actor_list;
Expand Down Expand Up @@ -457,7 +457,7 @@ tinyxml2::XMLError MapData::add_actor_template(tinyxml2::XMLElement* source, Til

// Parse user specified properties of the ActorTemplate
ActorTemplate& a = m_templates[actor_name];
eResult = parse_actor_properties(p_property, a.speed, a.AI, a.direction);
eResult = parse_actor_properties(p_property, a.speed, a.direction, a.response);
if(eResult != XML_SUCCESS) {
std::cerr << "Failed at parsing actor properties for actor template of actor: " << actor_name << "\n";
return eResult;
Expand Down Expand Up @@ -515,3 +515,68 @@ bool MapData::collide(const SDL_Rect* rect, int& x_max, int& y_max, std::vector<
}
return collide;
}

tinyxml2::XMLError MapData::parse_actor_properties(tinyxml2::XMLElement* source, float& speed, Direction& dir, std::map<Response, ActorEvent*>& resp) {
using namespace tinyxml2;
XMLError eResult;
while(source != nullptr) {
const char* p_name;
p_name = source->Attribute("name");
std::string name(p_name);
if(p_name == nullptr) return XML_ERROR_PARSING_ATTRIBUTE;

// Parse base speed
else if(name == "BASE_SPEED") {
eResult = source->QueryFloatAttribute("value", &speed);
if(eResult != XML_SUCCESS) {
std::cerr << "Failed at loading speed value\n";
return eResult;
}
}

// Parse current direction facing
else if(name == "DIRECTION") {
const char* p_direction = source->Attribute("value");
if(p_direction != nullptr) {
dir = str_to_direction(std::string(p_direction));
if(dir == Direction::invalid) {
std::cerr << "Invalid direction type \"" << p_direction << "\"specified\n";
return XML_WRONG_ATTRIBUTE_TYPE;
}
}
else {
std::cerr << "Empty direction value specified\n";
return XML_NO_ATTRIBUTE;
}

}

// Parse response values
else if(str_to_response(name) != Response::invalid) {
const char* p_event = source->Attribute("value");
if(p_event != nullptr) {
std::string event(p_event);
if(check_event(event)) {
resp[str_to_response(name)] = get_event(event);
}
else {
std::cerr << "An event called: " << event << " does not exist/ never got parsed!";
return XML_ERROR_PARSING_ATTRIBUTE;
}
}
else {
std::cerr << "Empty response event specified\n";
return XML_NO_ATTRIBUTE;
}
}

else {
std::cerr << "Unknown actor property \"" << p_name << "\" specified\n";
return XML_ERROR_PARSING_ATTRIBUTE;
}
// Move to next property
source = source->NextSiblingElement("property");
}
return XML_SUCCESS;
}

5 changes: 4 additions & 1 deletion src/map/mapdata.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ class MapData {
unsigned get_tile_w() const {return m_tile_w;}
void register_event(std::pair<std::string, ActorEvent*> event) {m_events.insert(event);}
ActorEvent* get_event(std::string name) const {return m_events.at(name)->copy();} ///< Returns copy of named event
bool check_event(std::string name) const {if(m_events.find(name) != m_events.end()) {return true;} else {return false;}}

const ActorTemplate& get_actor_template(Uint16 gid) const {return m_templates.at(m_gid_to_temp_name.at(gid));}
std::vector<Actor*> get_actors(std::string name = "", Behaviour behaviour = Behaviour::invalid, Direction direction = Direction::invalid,
std::vector<Actor*> get_actors(std::string name = "", Direction direction = Direction::invalid,
AnimationType animation = AnimationType::invalid);
bool collide(const SDL_Rect* rect, int& x_max, int& y_max, std::vector<Actor*>& collided);

Expand All @@ -73,6 +74,8 @@ class MapData {
bool render(Uint16 tile_id, int x, int y) const;
bool render(Uint16 tile_id, SDL_Rect& dest) const;

tinyxml2::XMLError parse_actor_properties(tinyxml2::XMLElement* source, float& speed, Direction& dir, std::map<Response, ActorEvent*>& resp);

private:
tinyxml2::XMLDocument m_mapfile{true, tinyxml2::COLLAPSE_WHITESPACE}; ///< Contains the .tmx map file
std::string m_base_path = "../data/"; ///< Path to folder where .tmx map files are
Expand Down
70 changes: 1 addition & 69 deletions src/util/game_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,13 @@ EventSignal str_to_event_signal(const std::string& name) {
else return EventSignal::invalid;
}

/// Converts a @c string to an @c enum of @c Behaviour
Behaviour str_to_behaviour(const std::string& name) {
if(name == "PLAYER") return Behaviour::player;
if(name == "IDLE") return Behaviour::idle;
if(name == "WALK_AROUND") return Behaviour::walk_around;
else return Behaviour::invalid;
}

/// Converts a @c string to an @c enum of @c Response
Response str_to_response(const std::string& name) {
if(name == "ON_HIT") return Response::on_hit;
if(name == "ON_COLLISION") return Response::on_collision;
if(name == "ON_ACTIVATION") return Response::on_activation;
if(name == "ON_DEATH") return Response::on_death;
if(name == "ON_IDLE") return Response::on_idle;
else return Response::invalid;
}

Expand Down Expand Up @@ -137,64 +130,3 @@ tinyxml2::XMLError parse_blendmode(tinyxml2::XMLElement* source, Texture& img) {
}
return XML_SUCCESS;
}

tinyxml2::XMLError parse_actor_properties(tinyxml2::XMLElement* source, float& speed, Behaviour& beh, Direction& dir) {
using namespace tinyxml2;
XMLError eResult;
while(source != nullptr) {
const char* p_name;
p_name = source->Attribute("name");
std::string name(p_name);
if(p_name == nullptr) return XML_ERROR_PARSING_ATTRIBUTE;

// Parse base speed
else if(name == "BASE_SPEED") {
eResult = source->QueryFloatAttribute("value", &speed);
if(eResult != XML_SUCCESS) {
std::cerr << "Failed at loading speed value\n";
return eResult;
}
}

//Parse current direction facing
else if(name == "DIRECTION") {
const char* p_direction = source->Attribute("value");
if(p_direction != nullptr) {
dir = str_to_direction(std::string(p_direction));
if(dir == Direction::invalid) {
std::cerr << "Invalid direction type \"" << p_direction << "\"specified\n";
return XML_WRONG_ATTRIBUTE_TYPE;
}
}
else {
std::cerr << "Empty direction value specified\n";
return XML_NO_ATTRIBUTE;
}

}

// Parse the AI type
else if(name == "BEHAVIOUR") {
const char* p_behaviour = source->Attribute("value");
if(p_behaviour != nullptr) {
beh = str_to_behaviour(std::string(p_behaviour));
if(beh == Behaviour::invalid) {
std::cerr << "Invalid behaviour type \"" << p_behaviour << "\"specified\n";
return XML_WRONG_ATTRIBUTE_TYPE;
}
}
else {
std::cerr << "Empty behaviour value specified\n";
return XML_NO_ATTRIBUTE;
}
}

else {
std::cerr << "Unknown actor property \"" << p_name << "\" specified\n";
return XML_ERROR_PARSING_ATTRIBUTE;
}
// Move to next property
source = source->NextSiblingElement("property");
}
return XML_SUCCESS;
}
13 changes: 3 additions & 10 deletions src/util/game_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,12 @@
#include "map/tile.hpp"
#include "util/tinyxml2.h"

class ActorEvent;

/**
* @brief A collection of various enums
*/

enum class Behaviour {
player,
idle,
walk_around,
invalid,
};

enum class Direction {
up,
down,
Expand Down Expand Up @@ -74,27 +68,26 @@ enum class Response{
on_collision,
on_activation,
on_death,
on_idle,
invalid,
};

struct ActorTemplate {
std::string template_name = "_";
float speed = 250.0f; // Pixel per second
Behaviour AI = Behaviour::idle;
Direction direction = Direction::down;
SDL_Rect hitbox = {0,0,0,0};
std::map<AnimationType, std::map<Direction, Tile>> animations;
std::map<Response, ActorEvent*> response; ///< Map which yields events for response values
};

AnimationType str_to_anim_type(const std::string& name);
Direction str_to_direction(const std::string& name);
Behaviour str_to_behaviour(const std::string& name);
Priority str_to_priority(const std::string& name);
EventSignal str_to_event_signal(const std::string& name);
Response str_to_response(const std::string& name);
tinyxml2::XMLError parse_hitbox(tinyxml2::XMLElement* source, SDL_Rect& rect);
tinyxml2::XMLError parse_blendmode(tinyxml2::XMLElement* source, Texture& img);
tinyxml2::XMLError parse_actor_properties(tinyxml2::XMLElement* source, float& speed, Behaviour& beh, Direction& dir);

std::vector<float> dir_to_mov(const Direction dir);

Expand Down

0 comments on commit c24a034

Please sign in to comment.