Skip to content

Commit

Permalink
Add harmless mob
Browse files Browse the repository at this point in the history
  • Loading branch information
CBenoit committed May 9, 2020
1 parent 3c3eaec commit 2a20cb6
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 31 deletions.
2 changes: 2 additions & 0 deletions cmake/config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ set(SFML_MINIMUM_SYSTEM_VERSION 2.5)
# mobs
set(MOBS_PLAYERID 0)
add_compile_definitions(MOBS_PLAYERID=0)
set(MOBS_SCIENTISTID 1)
add_compile_definitions(MOBS_SCIENTISTID=1)

# tiles
set(TILES_CHASMID 0)
Expand Down
1 change: 1 addition & 0 deletions include/utils/resource_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class resource_manager {

enum class mob_id {
player = MOBS_PLAYERID,
scientist = MOBS_SCIENTISTID,
};

enum class object_id {
Expand Down
47 changes: 24 additions & 23 deletions include/view/overmap_collection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "adapter/adapter.hpp"

#include <set>
#include <vector>
#include <list>

namespace utils {
class resource_manager;
Expand All @@ -18,47 +18,48 @@ namespace view {
class viewer;

class overmap_collection {
using pair_type = std::pair<const overmap_displayable_interface*, adapter::view_handle>;
using pair_type = std::pair<const overmap_displayable_interface*, adapter::view_handle>;

struct less {
bool operator()(const pair_type& rhs, const pair_type& lhs) const noexcept {
return *rhs.first < *lhs.first;
}
};
struct less {
bool operator()(const pair_type& rhs, const pair_type& lhs) const noexcept {
return *rhs.first < *lhs.first;
}
};

public:
void reload_sprites(const utils::resource_manager& res) {

void reload_sprites(const utils::resource_manager& res) {
for (mob& mob : m_mobs) {
mob.reload_sprites(res);
}
for (object& object : m_objects) {
object.reload_sprites(res);
}
}
}

void print_all(view::viewer &) const noexcept;
void print_all(view::viewer &) const noexcept;

void print_all(view::viewer &, adapter::adapter &, utils::resource_manager &) const noexcept;
void print_all(view::viewer &, adapter::adapter &, utils::resource_manager &) const noexcept;

adapter::view_handle add_object(object&&) noexcept;
adapter::view_handle add_mob(mob&&) noexcept;
adapter::view_handle add_object(object&&) noexcept;
adapter::view_handle add_mob(mob&&) noexcept;

void move_entity(adapter::view_handle handle, float newx, float newy);
void move_entity(adapter::view_handle handle, float newx, float newy);

void rotate_entity(adapter::view_handle handle, view::facing_direction::type new_direction) noexcept;
void rotate_entity(adapter::view_handle handle, view::facing_direction::type new_direction) noexcept;


void clear() {
m_ordered_displayable.clear();
m_mobs.clear();
m_objects.clear();
}
void clear() {
m_ordered_displayable.clear();
m_mobs.clear();
m_objects.clear();
}

private:
std::set<pair_type, less> m_ordered_displayable;
std::set<pair_type, less> m_ordered_displayable;

std::vector<mob> m_mobs;
std::vector<object> m_objects;
std::list<mob> m_mobs;
std::list<object> m_objects;
};
} // namespace view

Expand Down
36 changes: 35 additions & 1 deletion resources/configured_resources/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

[graphics]
[graphics.mobs]
list = ["player"]
list = ["player", "scientist"]

[graphics.mobs.player]
id = @MOBS_PLAYERID@
Expand Down Expand Up @@ -41,6 +41,40 @@
width = 29
height = 56

[graphics.mobs.scientist]
id = @MOBS_SCIENTISTID@

[graphics.mobs.scientist.downwards]
frame-count = 1
pos.x = 0
pos.y = 64
shift.y = -58
width = 42
height = 56

[graphics.mobs.scientist.upwards]
frame-count = 1
pos.x = 73
pos.y = 64
shift.y = -58
width = 41
height = 56

[graphics.mobs.scientist.rightwards]
frame-count = 1
pos.x = 116
pos.y = 64
shift.y = -58
width = 29
height = 56

[graphics.mobs.scientist.leftwards]
frame-count = 1
pos.x = 43
pos.y = 64
shift.y = -58
width = 29
height = 56

[graphics.tiles]
list = ["chasm", "iron-tile", "concrete-tile", "tile-frame"]
Expand Down
2 changes: 1 addition & 1 deletion resources/raw_resources/map_test.map
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# D # b #
# # # @ #
# # #
## ### #
##S### #
# ### #
# ###### #
# #### #
Expand Down
23 changes: 23 additions & 0 deletions src/adapter/adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,29 @@ bool adapter::adapter::load_map(const std::filesystem::path &path) noexcept {
cell.type = model::cell_type::GROUND;
break;
}
case 'S': {
const float scientist_hitbox_height = 1.f;
const float scientist_hitbox_width = 1.f;

world.components.health[next_entity_handle] = {1};
world.components.hitbox[next_entity_handle]
= {static_cast<float>(column), static_cast<float>(row), scientist_hitbox_width / 2.f, scientist_hitbox_height / 2.f};

view::mob m{};
m.set_mob_id(utils::resource_manager::mob_id::scientist, m_state.resources);
m.set_direction(view::facing_direction::S);
m.set_pos(static_cast<float>(column) + scientist_hitbox_width / 2.f, static_cast<float>(row) + scientist_hitbox_height);

view_handle view_handle = view.acquire_overmap()->add_mob(std::move(m));
model_handle model_handle{next_entity_handle};
m_model2view[model_handle] = view_handle;
m_view2model[view_handle] = model_handle;
cell.type = model::cell_type::GROUND;

++next_entity_handle;

break;
}
case 'D':
case ' ':
cell.type = model::cell_type::GROUND;
Expand Down
11 changes: 6 additions & 5 deletions src/view/overmap_collection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,16 @@ void view::overmap_collection::move_entity(adapter::view_handle handle, float ne
}

m_ordered_displayable.erase(it);
auto target = std::next(m_mobs.begin(), handle.handle);
if (handle.is_mob) {
spdlog::trace("Moving view mob {} to ({} ; {})", handle.handle, newx, newy);
m_mobs[handle.handle].set_pos(newx, newy);
m_ordered_displayable.emplace(std::pair{&m_mobs[handle.handle], handle});
target->set_pos(newx, newy);
m_ordered_displayable.emplace(std::pair{&*target, handle});
}
else {
spdlog::trace("Moving view object {} to ({} ; {})", handle.handle, newx, newy);
m_objects[handle.handle].set_pos(newx, newy);
m_ordered_displayable.emplace(std::pair{&m_mobs[handle.handle], handle});
target->set_pos(newx, newy);
m_ordered_displayable.emplace(std::pair{&*target, handle});
}
}

Expand All @@ -83,5 +84,5 @@ void view::overmap_collection::rotate_entity(adapter::view_handle handle, view::
return;
}

m_mobs[handle.handle].set_direction(new_direction);
std::next(m_mobs.begin(), handle.handle)->set_direction(new_direction);
}
3 changes: 2 additions & 1 deletion src/view/viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ namespace cst {
} // namespace

view::viewer::viewer(state::holder *state_holder) noexcept
: m_state_holder{*state_holder} { }
: m_state_holder{*state_holder} {
}

void view::viewer::run() {
m_running = true;
Expand Down

0 comments on commit 2a20cb6

Please sign in to comment.