From 7783cb8e5ec90ba31ca9762050499862dc5dc811 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Mon, 31 Jul 2017 09:56:49 +0100 Subject: [PATCH] Refactor moving logic out from Character into a new class. --- src/gamelib/CMakeLists.txt | 1 + src/gamelib/character.cpp | 38 +++++++---------------- src/gamelib/character.hpp | 11 ++----- src/gamelib/world_moveable.cpp | 57 ++++++++++++++++++++++++++++++++++ src/gamelib/world_moveable.hpp | 47 ++++++++++++++++++++++++++++ 5 files changed, 119 insertions(+), 35 deletions(-) create mode 100644 src/gamelib/world_moveable.cpp create mode 100644 src/gamelib/world_moveable.hpp diff --git a/src/gamelib/CMakeLists.txt b/src/gamelib/CMakeLists.txt index 7bead15..a004f95 100644 --- a/src/gamelib/CMakeLists.txt +++ b/src/gamelib/CMakeLists.txt @@ -21,6 +21,7 @@ add_library(${PROJECT_NAME} grid_raytrace.cpp drawable.cpp drawing_queue.cpp + world_moveable.cpp ) set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 14) set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD_REQUIRED ON) diff --git a/src/gamelib/character.cpp b/src/gamelib/character.cpp index 66fa277..b42426c 100644 --- a/src/gamelib/character.cpp +++ b/src/gamelib/character.cpp @@ -32,28 +32,17 @@ namespace curry { Character::Character (cloonel::InputBag* parInputBag, const WorldSizeNotifiable::DeferredRegister& parDeferredRegister) : - WorldSizeNotifiable(parDeferredRegister), - m_position(vec2f(0.0f), vec2f(0.0f), vec2f(0.0f)), + WorldMoveable(parDeferredRegister), m_input_bag(parInputBag), - m_world(parDeferredRegister.world()), m_speed(1.0f) { assert(m_input_bag); - assert(m_world); - } - - void Character::set_position (const vec2f& parPos) { - m_position = parPos; - } - - const vec2f& Character::position() const { - return m_position.get(); } Rect Character::destination_rect (const vec2f& parWorldPos) const { return Rect( - m_position.get() - parWorldPos, - m_position.get() - parWorldPos + width_height() + position() - parWorldPos, + position() - parWorldPos + width_height() ); } @@ -64,10 +53,6 @@ namespace curry { ); } - void Character::world_size_changed (const vec2us&, const vec2i& parPixelSize) { - m_position.change_minmax(m_position.get_min(), vector_cast(parPixelSize)); - } - void Character::do_movement (float parDeltaT) { const float speed = parDeltaT * m_speed; vec2f offset(0.0f); @@ -85,10 +70,12 @@ namespace curry { if (position() == old_pos) return; - const vec2us character_tile = pixel_to_world_tile(*m_world, position()); - vec2us stop_at = pixel_to_world_tile(*m_world, old_pos); - auto is_walkable = [world=m_world,&stop_at](vec2us idx) { - const bool walkable = world->tile_property(world->tile(idx)).walkable; + WorldGrid& world = this->world(); + + const vec2us character_tile = pixel_to_world_tile(world, position()); + vec2us stop_at = pixel_to_world_tile(world, old_pos); + auto is_walkable = [&world,&stop_at](vec2us idx) { + const bool walkable = world.tile_property(world.tile(idx)).walkable; if (walkable) stop_at = idx; else @@ -96,10 +83,10 @@ namespace curry { return walkable; }; - for_each_voxel_under_segment(old_pos, this->position(), width_height(), *m_world, is_walkable, false); + for_each_voxel_under_segment(old_pos, this->position(), width_height(), world, is_walkable, false); if (character_tile != stop_at) { - vec2f blocked_pos = world_tile_to_pixel(*m_world, stop_at); + vec2f blocked_pos = world_tile_to_pixel(world, stop_at); vec2f new_pos = position(); if (character_tile.x() != stop_at.x()) new_pos.x() = blocked_pos.x(); @@ -125,7 +112,6 @@ namespace curry { } void Character::on_texture_size_changed (const vec2f& parOldSz, const vec2f& parNewSz) { - if (m_position.get_min() != m_position.get_max()) - m_position.change_minmax(m_position.get_min(), m_position.get_max() + parOldSz - parNewSz); + this->update_moveable_size(parOldSz, parNewSz); } } //namespace curry diff --git a/src/gamelib/character.hpp b/src/gamelib/character.hpp index 73b0665..d6279f1 100644 --- a/src/gamelib/character.hpp +++ b/src/gamelib/character.hpp @@ -21,9 +21,7 @@ #include "vector.hpp" #include "rect.hpp" -#include "constrained_position.hpp" -#include "worldsizenotifiable.hpp" -#include "moveable.hpp" +#include "world_moveable.hpp" #include "drawable.hpp" namespace cloonel { @@ -34,16 +32,13 @@ namespace cloonel { namespace curry { class WorldGrid; - class Character : public Drawable, public WorldSizeNotifiable, public Moveable { + class Character : public Drawable, public WorldMoveable { public: Character (cloonel::InputBag* parInputBag, const WorldSizeNotifiable::DeferredRegister& parDeferredRegister); virtual ~Character() noexcept = default; - const vec2f& position() const; - void set_position (const vec2f& parPos); Rect destination_rect (const vec2f& parWorldPos) const; Rect source_rect() const; - virtual void world_size_changed (const vec2us& parSize, const vec2i& parPixelSize) override; virtual void do_movement (float parDeltaT) override; void set_speed (float parSpeed); virtual void draw (DrawingQueue& parDQ, const WorldViewport& parViewport) override; @@ -51,9 +46,7 @@ namespace curry { private: virtual void on_texture_size_changed (const vec2f& parOldSz, const vec2f& parNewSz) override; - ConstrainedPosition m_position; cloonel::InputBag* m_input_bag; - WorldGrid* m_world; float m_speed; }; } //namespace curry diff --git a/src/gamelib/world_moveable.cpp b/src/gamelib/world_moveable.cpp new file mode 100644 index 0000000..c9ac2be --- /dev/null +++ b/src/gamelib/world_moveable.cpp @@ -0,0 +1,57 @@ +/* + Copyright 2016, 2017 Michele "King_DuckZ" Santullo + + This file is part of MyCurry. + + MyCurry is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + MyCurry is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with MyCurry. If not, see . +*/ + +#include "world_moveable.hpp" +#include + +namespace curry { + WorldMoveable::WorldMoveable (const WorldSizeNotifiable::DeferredRegister& parDeferredRegister) : + WorldSizeNotifiable(parDeferredRegister), + m_position(vec2f(0.0f), vec2f(0.0f), vec2f(0.0f)), + m_world(parDeferredRegister.world()) + { + assert(m_world); + } + void WorldMoveable::world_size_changed (const vec2us&, const vec2i& parPixelSize) { + m_position.change_minmax(m_position.get_min(), vector_cast(parPixelSize)); + } + + void WorldMoveable::set_position (const vec2f& parPos) { + m_position = parPos; + } + + const vec2f& WorldMoveable::position() const { + return m_position.get(); + } + + void WorldMoveable::update_moveable_size (const vec2f& parOldSz, const vec2f& parNewSz) { + if (m_position.get_min() != m_position.get_max()) + m_position.change_minmax(m_position.get_min(), m_position.get_max() + parOldSz - parNewSz); + } + + WorldGrid& WorldMoveable::world() { + assert(m_world); + return *m_world; + } + + const WorldGrid& WorldMoveable::world() const { + assert(m_world); + return *m_world; + } +} //namespace curry diff --git a/src/gamelib/world_moveable.hpp b/src/gamelib/world_moveable.hpp new file mode 100644 index 0000000..cac42de --- /dev/null +++ b/src/gamelib/world_moveable.hpp @@ -0,0 +1,47 @@ +/* + Copyright 2016, 2017 Michele "King_DuckZ" Santullo + + This file is part of MyCurry. + + MyCurry is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + MyCurry is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with MyCurry. If not, see . +*/ + +#pragma once + +#include "moveable.hpp" +#include "worldsizenotifiable.hpp" +#include "constrained_position.hpp" + +namespace curry { + class WorldGrid; + + class WorldMoveable : public Moveable, public WorldSizeNotifiable { + public: + explicit WorldMoveable (const WorldSizeNotifiable::DeferredRegister& parDeferredRegister); + virtual ~WorldMoveable() = default; + + virtual void world_size_changed (const vec2us& parSize, const vec2i& parPixelSize) override; + const vec2f& position() const; + void set_position (const vec2f& parPos); + WorldGrid& world(); + const WorldGrid& world() const; + + protected: + void update_moveable_size (const vec2f& parOldSz, const vec2f& parNewSz); + + private: + ConstrainedPosition m_position; + WorldGrid* m_world; + }; +} //namespace curry