Skip to content

Commit

Permalink
Add collision ref for collision handling
Browse files Browse the repository at this point in the history
- Add actor id for clear identity check
- Implement add and remove actor interface
  • Loading branch information
VectorWolf committed Nov 20, 2019
1 parent c260aa0 commit 372022d
Show file tree
Hide file tree
Showing 13 changed files with 180 additions and 4 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ endif()

set(LIB_SOURCES
src/include_impl/audio_manager_ref.cpp
src/include_impl/collision_ref.cpp
src/include_impl/sound_ref.cpp
src/include_impl/music_ref.cpp
src/include_impl/gameinfo.cpp
Expand Down
10 changes: 10 additions & 0 deletions include/actor_ref.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,44 @@
#define ACTOR_REF_HPP_INCLUDED

#include <string>
#include <vector>

#include "./types.hpp"
#include "./collision_ref.hpp"
#include "./data_block_ref.hpp"

class Actor;

namespace salmon {
class ActorRef {
friend class CameraRef;
friend class MapRef;
public:
ActorRef(Actor& impl);
ActorRef(Actor* impl);
virtual ~ActorRef() = default;

bool good() const {return (m_impl == nullptr) ? false : true ;}

bool animate(salmon::AnimationType anim = salmon::AnimationType::current, salmon::Direction dir = salmon::Direction::current, float speed = 1.0);
bool set_animation(salmon::AnimationType anim = salmon::AnimationType::current, salmon::Direction dir = salmon::Direction::current, int frame = 0);
salmon::AnimSignal animate_trigger(salmon::AnimationType anim = salmon::AnimationType::current, salmon::Direction dir = salmon::Direction::current, float speed = 1.0);
salmon::AnimationType get_animation() const;
salmon::Direction get_direction() const;
std::string get_name() const;
std::string get_type() const;
unsigned get_id() const;

bool move(float x_factor, float y_factor, bool absolute = false);
bool on_ground(salmon::Direction dir = salmon::Direction::down, int tolerance = 0) const;

double get_angle() const;
void set_angle(double angle);

std::vector<CollisionRef> get_collisions();
void clear_collisions();
void register_collisions(bool r);

float get_x() const;
float get_y() const;
unsigned get_w() const;
Expand Down
30 changes: 30 additions & 0 deletions include/collision_ref.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef COLLISION_REF_HPP_INCLUDED
#define COLLISION_REF_HPP_INCLUDED

#include <string>

class Collision;

namespace salmon {
class CollisionRef {
public:
CollisionRef(Collision& impl);

// Checks against tile types
bool tile() const;
bool actor() const;
bool mouse() const;
bool none() const;

std::string my_hitbox() const;
std::string other_hitbox() const;

unsigned get_actor_id() const;
std::string get_tile_type() const;

private:
Collision* m_impl;
};
}

#endif // COLLISION_REF_HPP_INCLUDED
5 changes: 5 additions & 0 deletions include/map_ref.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#ifndef MAP_REF_HPP_INCLUDED
#define MAP_REF_HPP_INCLUDED

#include <string>
#include <vector>

#include "./actor_ref.hpp"
Expand All @@ -40,6 +41,10 @@ namespace salmon {
std::vector<salmon::ActorRef> get_actors();
CameraRef get_camera();

salmon::ActorRef add_actor(std::string actor_template_name, std::string layer_name);
salmon::ActorRef add_actor(salmon::ActorRef actor, std::string layer_name);
bool remove_actor(salmon::ActorRef actor);

unsigned get_w() const;
unsigned get_h() const;
float get_delta_time() const;
Expand Down
37 changes: 37 additions & 0 deletions src/actor/actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,43 @@ using namespace salmon;

Actor::Actor(MapData* map) : m_map{map} {}

/*
Actor::Actor(MapData* map) : m_map{map}, m_id{current_id++} {}
Actor::Actor(const Actor& other) {*this = other;}
Actor& Actor::operator=(const Actor& other) {
m_map = other.m_map;
m_x = other.m_x;
m_y = other.m_y;
m_width = other.m_width;
m_height = other.m_height;
m_name = other.m_name;
m_type = other.m_type;
m_angle = other.m_angle;
m_anim_state = other.m_anim_state;
m_direction = other.m_direction;
m_animations = other.m_animations;
m_base_tile = other.m_base_tile;
#ifndef LIB_BUILD
m_response = other.m_response;
m_events = other.m_events;
#endif // LIB_BUILD
m_data = other.m_data;
m_collisions = other.m_collisions;
m_register_collisions = other.m_register_collisions;
m_id = current_id++;
m_late_polling = other.m_late_polling;
return *this;
}
*/
/**
* @brief Initialize actor dimensions and name from XML info
* @param source The @c XMLElement which contains the information
Expand Down
17 changes: 15 additions & 2 deletions src/actor/actor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ class Actor{
// Actor() = default;
Actor(MapData* map);

/*
Actor(const Actor& other);
Actor& operator=(const Actor& other);
Actor(Actor&& other) = default;
Actor& operator=(Actor&& other) = default;
*/

// Core functions
tinyxml2::XMLError parse_base(tinyxml2::XMLElement* source);
tinyxml2::XMLError parse_properties(tinyxml2::XMLElement* source);
Expand Down Expand Up @@ -85,13 +92,16 @@ class Actor{
bool valid_anim_state(salmon::AnimationType anim, salmon::Direction dir) const;
bool valid_anim_state() const {return valid_anim_state(m_anim_state, m_direction);}
bool is_valid() const {return m_base_tile.is_valid();}
unsigned get_id() const {return m_id;}
void set_id(unsigned id) {m_id = id;}

SDL_Rect get_hitbox(std::string type = "COLLIDE") const;
const std::map<std::string, SDL_Rect> get_hitboxes() const;

void add_collision(Collision c) {m_collisions.push_back(c);}
std::vector<Collision> get_collisions() {return m_collisions;}
void add_collision(Collision c) {if(m_register_collisions) {m_collisions.push_back(c);}}
std::vector<Collision>& get_collisions() {return m_collisions;}
void clear_collisions() {m_collisions.clear();}
void register_collisions(bool r) {if(!r) {clear_collisions();} m_register_collisions = r;}

#ifndef LIB_BUILD
void update();
Expand Down Expand Up @@ -124,6 +134,9 @@ class Actor{
DataBlock m_data; ///< This holds custom user values by string

std::vector<Collision> m_collisions;
bool m_register_collisions = true;

unsigned m_id = 0;

bool m_late_polling = false;
};
Expand Down
2 changes: 2 additions & 0 deletions src/actor/collision.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,5 @@ std::string Collision::get_type() const {
}
}

unsigned Collision::get_actor_id() const {return get_actor()->get_id();}

1 change: 1 addition & 0 deletions src/actor/collision.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class Collision{

// Return cause objects
Actor* get_actor() const {return type == CollisionType::actor ? data.actor : nullptr;}
unsigned get_actor_id() const;
Tile* get_tile() const {return type == CollisionType::tile ? data.tile : nullptr;}

std::string get_type() const;
Expand Down
12 changes: 12 additions & 0 deletions src/include_impl/actor_ref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,25 @@ salmon::AnimationType ActorRef::get_animation() const {return m_impl->get_animat
salmon::Direction ActorRef::get_direction() const {return m_impl->get_direction();}
std::string ActorRef::get_name() const {return m_impl->get_name();}
std::string ActorRef::get_type() const {return m_impl->get_type();}
unsigned ActorRef::get_id() const {return m_impl->get_id();}

bool ActorRef::move(float x_factor, float y_factor, bool absolute) {return m_impl->move(x_factor, y_factor, absolute);}
bool ActorRef::on_ground(salmon::Direction dir, int tolerance) const {return m_impl->on_ground(dir, tolerance);}

double ActorRef::get_angle() const {return m_impl->get_angle();}
void ActorRef::set_angle(double angle) {return m_impl->set_angle(angle);}

std::vector<salmon::CollisionRef> ActorRef::get_collisions() {
std::vector<Collision>& temp = m_impl->get_collisions();
std::vector<salmon::CollisionRef> out;
for(auto& c : temp) {
out.emplace_back(c);
}
return out;
}
void ActorRef::clear_collisions() {m_impl->clear_collisions();}
void ActorRef::register_collisions(bool r) {m_impl->register_collisions(r);}

float ActorRef::get_x() const {return m_impl->get_x();}
float ActorRef::get_y() const {return m_impl->get_y();}
unsigned ActorRef::get_w() const {return m_impl->get_w();}
Expand Down
17 changes: 17 additions & 0 deletions src/include_impl/collision_ref.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "collision_ref.hpp"

#include "actor/collision.hpp"

using salmon::CollisionRef;

CollisionRef::CollisionRef(Collision& impl) : m_impl(&impl) {}

bool CollisionRef::tile() const {return m_impl->tile();}
bool CollisionRef::actor() const {return m_impl->actor();}
bool CollisionRef::mouse() const {return m_impl->mouse();}
bool CollisionRef::none() const {return m_impl->none();}

std::string CollisionRef::my_hitbox() const {return m_impl->my_hitbox();}
std::string CollisionRef::other_hitbox() const {return m_impl->other_hitbox();}
std::string CollisionRef::get_tile_type() const {return m_impl->get_type();}
unsigned CollisionRef::get_actor_id() const {return m_impl->get_actor_id();}
42 changes: 42 additions & 0 deletions src/include_impl/map_ref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
*/
#include "map_ref.hpp"

#include <iostream>

#include "actor/actor.hpp"
#include "map/mapdata.hpp"
#include "map/layer_collection.hpp"
#include "map/object_layer.hpp"

using salmon::MapRef;

Expand All @@ -38,6 +43,43 @@ std::vector<salmon::ActorRef> MapRef::get_actors() {
}
salmon::CameraRef MapRef::get_camera() {return m_impl->get_camera();}

salmon::ActorRef MapRef::add_actor(std::string actor_template_name, std::string layer_name) {
if(m_impl->is_actor(actor_template_name)) {
Actor a = m_impl->get_actor(actor_template_name);
return add_actor(&a, layer_name);
}
else {
std::cerr << "There is no actor template called: \"" << actor_template_name << "\"\n";
return salmon::ActorRef(nullptr);
}
}
salmon::ActorRef MapRef::add_actor(salmon::ActorRef actor, std::string layer_name) {
Layer* dest_layer = m_impl->get_layer_collection().get_layer(layer_name);
if(dest_layer == nullptr) {
std::cerr << "There is no layer called: \"" << layer_name << "\"\n";
return salmon::ActorRef(nullptr);
}
else if(dest_layer->get_type() != Layer::object) {
std::cerr << "The layer: \"" << layer_name << "\" is no object layer!\n";
return salmon::ActorRef(nullptr);
}
else {
ObjectLayer* layer = static_cast<ObjectLayer*>(dest_layer);
Actor* added = layer->add_actor(*actor.m_impl);
return salmon::ActorRef(added);
}
}

bool MapRef::remove_actor(salmon::ActorRef actor) {
std::vector<ObjectLayer*> obj_layers = m_impl->get_layer_collection().get_object_layers();
bool success = false;
for(ObjectLayer* o : obj_layers) {
success = success || o->erase_actor(actor.m_impl);
}
return success;
}


unsigned MapRef::get_w() const {return m_impl->get_w();}
unsigned MapRef::get_h() const {return m_impl->get_h();}
float MapRef::get_delta_time() const {return m_impl->get_delta_time();}
Expand Down
6 changes: 5 additions & 1 deletion src/map/object_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include "map/camera.hpp"
#include "util/logger.hpp"

unsigned ObjectLayer::next_object_id = 1;

/// Factory function which retrieves a pointer owning the object layer
ObjectLayer* ObjectLayer::parse(tinyxml2::XMLElement* source, std::string name, LayerCollection* layer_collection, tinyxml2::XMLError& eresult) {
return new ObjectLayer(source, name, layer_collection, eresult);
Expand Down Expand Up @@ -244,11 +246,13 @@ std::vector<const Actor*> ObjectLayer::get_clip(const SDL_Rect& rect) const {
return actor_list;
}

void ObjectLayer::add_actor(Actor a) {
Actor* ObjectLayer::add_actor(Actor a) {
#ifndef LIB_BUILD
a.respond(Response::on_spawn);
#endif // LIB_BUILD
m_obj_grid.push_back(a);
m_obj_grid.back().set_id(next_object_id++);
return &m_obj_grid.back();
}

/// Remove actor with given name from layer
Expand Down
4 changes: 3 additions & 1 deletion src/map/object_layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ObjectLayer : public Layer{

LayerType get_type() override {return LayerType::object;}

void add_actor(Actor a);
Actor* add_actor(Actor a);
std::vector<Actor*> get_actors();
std::vector<Actor*> get_actors(std::string name);
Actor* get_actor(std::string name);
Expand Down Expand Up @@ -79,6 +79,8 @@ class ObjectLayer : public Layer{
std::list<Actor> m_obj_grid;
std::list<Smart<Primitive>> m_primitives;
bool m_suspended = false;

static unsigned next_object_id;
};


Expand Down

0 comments on commit 372022d

Please sign in to comment.