From dfb3ec8ef25a1f069d47ffff376b8a355c0f12a4 Mon Sep 17 00:00:00 2001 From: Simon Weis Date: Sat, 3 Sep 2022 19:23:31 +0200 Subject: [PATCH] Add basic physics --- impl/gamelib/game_properties.hpp | 8 +-- impl/gamelib/platform.cpp | 30 ++++++++++ impl/gamelib/platform.hpp | 22 +++++++ impl/gamelib/player.cpp | 37 ++++++++++++ impl/gamelib/player.hpp | 19 ++++++ .../graphics/graphics_component_impl.cpp | 58 ------------------ .../graphics/graphics_component_impl.hpp | 26 -------- .../graphics/graphics_component_interface.cpp | 1 - .../graphics/graphics_component_interface.hpp | 21 ------- .../player/input/input_component_impl.cpp | 28 --------- .../player/input/input_component_impl.hpp | 18 ------ .../input/input_component_interface.cpp | 1 - .../input/input_component_interface.hpp | 13 ---- .../player/input/input_target_interface.cpp | 1 - .../player/input/input_target_interface.hpp | 8 --- impl/gamelib/player/player.cpp | 60 ------------------- impl/gamelib/player/player.hpp | 27 --------- .../player/sound/sound_component_impl.cpp | 21 ------- .../player/sound/sound_component_impl.hpp | 25 -------- .../sound/sound_component_interface.cpp | 1 - .../sound/sound_component_interface.hpp | 13 ---- impl/gamelib/state_game.cpp | 14 ++++- impl/gamelib/state_game.hpp | 6 +- 23 files changed, 129 insertions(+), 329 deletions(-) create mode 100644 impl/gamelib/platform.cpp create mode 100644 impl/gamelib/platform.hpp create mode 100644 impl/gamelib/player.cpp create mode 100644 impl/gamelib/player.hpp delete mode 100644 impl/gamelib/player/graphics/graphics_component_impl.cpp delete mode 100644 impl/gamelib/player/graphics/graphics_component_impl.hpp delete mode 100644 impl/gamelib/player/graphics/graphics_component_interface.cpp delete mode 100644 impl/gamelib/player/graphics/graphics_component_interface.hpp delete mode 100644 impl/gamelib/player/input/input_component_impl.cpp delete mode 100644 impl/gamelib/player/input/input_component_impl.hpp delete mode 100644 impl/gamelib/player/input/input_component_interface.cpp delete mode 100644 impl/gamelib/player/input/input_component_interface.hpp delete mode 100644 impl/gamelib/player/input/input_target_interface.cpp delete mode 100644 impl/gamelib/player/input/input_target_interface.hpp delete mode 100644 impl/gamelib/player/player.cpp delete mode 100644 impl/gamelib/player/player.hpp delete mode 100644 impl/gamelib/player/sound/sound_component_impl.cpp delete mode 100644 impl/gamelib/player/sound/sound_component_impl.hpp delete mode 100644 impl/gamelib/player/sound/sound_component_interface.cpp delete mode 100644 impl/gamelib/player/sound/sound_component_interface.hpp diff --git a/impl/gamelib/game_properties.hpp b/impl/gamelib/game_properties.hpp index f4415152..ce1e46f0 100644 --- a/impl/gamelib/game_properties.hpp +++ b/impl/gamelib/game_properties.hpp @@ -10,10 +10,10 @@ class GP { public: GP() = delete; - static std::string GameName() { return "MyAwesomeGame"; } - static std::string AuthorName() { return "TODO"; } - static std::string JamName() { return "TODO"; } - static std::string JamDate() { return "TODO"; } + static std::string GameName() { return "Run Run Run!"; } + static std::string AuthorName() { return "Laguna_999"; } + static std::string JamName() { return "1hgj384"; } + static std::string JamDate() { return "2022-09-02"; } static jt::Vector2f GetWindowSize() { return jt::Vector2f { 1200, 800 }; } static float GetZoom() { return 2.0f; } diff --git a/impl/gamelib/platform.cpp b/impl/gamelib/platform.cpp new file mode 100644 index 00000000..34297f10 --- /dev/null +++ b/impl/gamelib/platform.cpp @@ -0,0 +1,30 @@ +#include "platform.hpp" + +Platform::Platform(std::shared_ptr world, b2BodyDef const* def, + jt::Vector2f const& pos, jt::Vector2f const& size) + : Box2DObject(world, def) +{ + m_size = size; + + b2FixtureDef fixtureDef; + b2PolygonShape boxCollider {}; + boxCollider.SetAsBox(size.x / 2.0f, size.y / 2.0f); + fixtureDef.shape = &boxCollider; + getB2Body()->CreateFixture(&fixtureDef); + + setPosition(pos); +} + +void Platform::doCreate() +{ + m_shape = std::make_shared(); + m_shape->makeRect(m_size, textureManager()); + m_shape->setOffset(jt::OffsetMode::CENTER); +} + +void Platform::doUpdate(float const elapsed) +{ + m_shape->setPosition(getPosition()); + m_shape->update(elapsed); +} +void Platform::doDraw() const { m_shape->draw(renderTarget()); } diff --git a/impl/gamelib/platform.hpp b/impl/gamelib/platform.hpp new file mode 100644 index 00000000..f80d9ae7 --- /dev/null +++ b/impl/gamelib/platform.hpp @@ -0,0 +1,22 @@ +#ifndef INC_1HGJ384_PLATFORM_HPP +#define INC_1HGJ384_PLATFORM_HPP + +#include +#include + +class Platform : public jt::Box2DObject { +public: + Platform(std::shared_ptr world, b2BodyDef const* def, + jt::Vector2f const& pos, jt::Vector2f const& size); + +private: + std::shared_ptr m_shape; + + jt::Vector2f m_size; + + void doCreate() override; + void doUpdate(float const elapsed) override; + void doDraw() const override; +}; + +#endif // INC_1HGJ384_PLATFORM_HPP diff --git a/impl/gamelib/player.cpp b/impl/gamelib/player.cpp new file mode 100644 index 00000000..4def95b5 --- /dev/null +++ b/impl/gamelib/player.cpp @@ -0,0 +1,37 @@ +#include "player.hpp" +#include + +Player::Player(std::shared_ptr world, b2BodyDef const* def) + : Box2DObject(world, def) +{ + b2FixtureDef fixtureDef; + b2PolygonShape boxCollider {}; + boxCollider.SetAsBox(8, 24); + fixtureDef.shape = &boxCollider; + getB2Body()->CreateFixture(&fixtureDef); + + setPosition(jt::Vector2f { 50.0f, 50.0f }); +} + +void Player::doCreate() +{ + m_shape = std::make_shared(); + m_shape->makeRect(jt::Vector2f { 16, 48 }, textureManager()); + m_shape->setOffset(jt::OffsetMode::CENTER); +} + +void Player::doUpdate(float const elapsed) +{ + + auto& input = getGame()->input(); + if (input.keyboard()->pressed(jt::KeyCode::D)) { + getB2Body()->ApplyForceToCenter(b2Vec2 { 100.0f, 0.0f }, true); + } else if (input.keyboard()->pressed(jt::KeyCode::A)) { + getB2Body()->ApplyForceToCenter(b2Vec2 { -100.0f, 0.0f }, true); + } + + m_shape->setPosition(getPosition()); + m_shape->update(elapsed); +} + +void Player::doDraw() const { m_shape->draw(renderTarget()); } diff --git a/impl/gamelib/player.hpp b/impl/gamelib/player.hpp new file mode 100644 index 00000000..a15a91a8 --- /dev/null +++ b/impl/gamelib/player.hpp @@ -0,0 +1,19 @@ +#ifndef INC_1HGJ384_PLAYER_HPP +#define INC_1HGJ384_PLAYER_HPP + +#include +#include + +class Player : public jt::Box2DObject { +public: + Player(std::shared_ptr world, b2BodyDef const* def); + +private: + std::shared_ptr m_shape; + + void doCreate() override; + void doUpdate(float const elapsed) override; + void doDraw() const override; +}; + +#endif // INC_1HGJ384_PLAYER_HPP diff --git a/impl/gamelib/player/graphics/graphics_component_impl.cpp b/impl/gamelib/player/graphics/graphics_component_impl.cpp deleted file mode 100644 index deff7785..00000000 --- a/impl/gamelib/player/graphics/graphics_component_impl.cpp +++ /dev/null @@ -1,58 +0,0 @@ -#include "graphics_component_impl.hpp" -#include -#include - -GraphicsComponentImpl::GraphicsComponentImpl(std::shared_ptr gameInterface) -{ - createAnimation(gameInterface->gfx().textureManager()); -} - -void GraphicsComponentImpl::createAnimation(jt::TextureManagerInterface& textureManager) -{ - m_animation = std::make_shared(); - m_animation->loadFromJson("assets/Pilz.json", textureManager); - m_animation->play("idle"); -} - -void GraphicsComponentImpl::updateGraphics(float elapsed) { m_animation->update(elapsed); } - -void GraphicsComponentImpl::setPosition(jt::Vector2f const& playerPosition) -{ - auto spritePosition = playerPosition - GP::PlayerSize() * 0.5f; - - m_animation->setPosition(spritePosition); -} - -void GraphicsComponentImpl::draw(std::shared_ptr target) -{ - m_animation->draw(target); -} - -void GraphicsComponentImpl::flash(float time, jt::Color const& color) -{ - m_animation->flash(time, color); -} - -bool GraphicsComponentImpl::setAnimationIfNotSet(std::string const& newAnimationName) -{ - std::string const currentAnimationName = m_animation->getCurrentAnimationName(); - - if (currentAnimationName == "die") { - return true; - } - - if (currentAnimationName == "hurt" && newAnimationName == "idle") { - return true; - } - - if (currentAnimationName != newAnimationName) { - m_animation->play(newAnimationName); - return true; - } - return false; -} - -std::string GraphicsComponentImpl::getCurrentAnimation() const -{ - return m_animation->getCurrentAnimationName(); -} diff --git a/impl/gamelib/player/graphics/graphics_component_impl.hpp b/impl/gamelib/player/graphics/graphics_component_impl.hpp deleted file mode 100644 index 1ce32e86..00000000 --- a/impl/gamelib/player/graphics/graphics_component_impl.hpp +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef GAME_PLAYER_GRAPHICS_COMPONENT_HPP -#define GAME_PLAYER_GRAPHICS_COMPONENT_HPP - -#include -#include -#include -#include - -class GraphicsComponentImpl : public GraphicsComponentInterface { -public: - explicit GraphicsComponentImpl(std::shared_ptr gameInterface); - - void updateGraphics(float elapsed) override; - void setPosition(jt::Vector2f const& playerPosition) override; - - void draw(std::shared_ptr target) override; - void flash(float time, jt::Color const& color) override; - bool setAnimationIfNotSet(std::string const& newAnimationName) override; - std::string getCurrentAnimation() const override; - -private: - void createAnimation(jt::TextureManagerInterface& textureManager); - std::shared_ptr m_animation; -}; - -#endif // GAME_PLAYER_GRAPHICS_COMPONENT_HPP diff --git a/impl/gamelib/player/graphics/graphics_component_interface.cpp b/impl/gamelib/player/graphics/graphics_component_interface.cpp deleted file mode 100644 index dd19d354..00000000 --- a/impl/gamelib/player/graphics/graphics_component_interface.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "graphics_component_interface.hpp" diff --git a/impl/gamelib/player/graphics/graphics_component_interface.hpp b/impl/gamelib/player/graphics/graphics_component_interface.hpp deleted file mode 100644 index 0cb7949b..00000000 --- a/impl/gamelib/player/graphics/graphics_component_interface.hpp +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef MYAWESOMEGAME_GRAPHICS_COMPONENT_INTERFACE_HPP -#define MYAWESOMEGAME_GRAPHICS_COMPONENT_INTERFACE_HPP - -#include -#include -#include -#include - -class GraphicsComponentInterface { -public: - virtual ~GraphicsComponentInterface() = default; - virtual void updateGraphics(float elapsed) = 0; - virtual void setPosition(jt::Vector2f const& position) = 0; - virtual void draw(std::shared_ptr target) = 0; - virtual void flash(float time, jt::Color const& color) = 0; - virtual bool setAnimationIfNotSet(std::string const& newAnimationName) = 0; - - virtual std::string getCurrentAnimation() const = 0; -}; - -#endif // MYAWESOMEGAME_GRAPHICS_COMPONENT_INTERFACE_HPP diff --git a/impl/gamelib/player/input/input_component_impl.cpp b/impl/gamelib/player/input/input_component_impl.cpp deleted file mode 100644 index 38b0ba0f..00000000 --- a/impl/gamelib/player/input/input_component_impl.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include "input_component_impl.hpp" -#include - -InputComponentImpl::InputComponentImpl(std::shared_ptr keyboardInterface) - : m_keyboard { keyboardInterface } -{ -} - -void InputComponentImpl::updateMovement(InputTargetInterface& target) -{ - target.setVelocity(jt::Vector2f { 0.0f, 0.0f }); - - auto const speed = 100.0f; - - if (m_keyboard->pressed(jt::KeyCode::D) || m_keyboard->pressed(jt::KeyCode::Right)) { - target.addVelocity(jt::Vector2f { speed, 0.0f }); - } - if (m_keyboard->pressed(jt::KeyCode::A) || m_keyboard->pressed(jt::KeyCode::Left)) { - target.addVelocity(jt::Vector2f { -speed, 0.0f }); - } - - if (m_keyboard->pressed(jt::KeyCode::W) || m_keyboard->pressed(jt::KeyCode::Up)) { - target.addVelocity(jt::Vector2f { 0.0f, -speed }); - } - if (m_keyboard->pressed(jt::KeyCode::S) || m_keyboard->pressed(jt::KeyCode::Down)) { - target.addVelocity(jt::Vector2f { 0.0f, speed }); - } -} diff --git a/impl/gamelib/player/input/input_component_impl.hpp b/impl/gamelib/player/input/input_component_impl.hpp deleted file mode 100644 index f9b020b7..00000000 --- a/impl/gamelib/player/input/input_component_impl.hpp +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef GAME_PLAYER_INPUT_COMPONENT_HPP -#define GAME_PLAYER_INPUT_COMPONENT_HPP - -#include -#include -#include - -class InputComponentImpl : public InputComponentInterface { -public: - explicit InputComponentImpl(std::shared_ptr keyboardInterface); - - void updateMovement(InputTargetInterface& player) override; - -private: - std::shared_ptr m_keyboard { nullptr }; -}; - -#endif // GAME_PLAYER_INPUT_COMPONENT_HPP diff --git a/impl/gamelib/player/input/input_component_interface.cpp b/impl/gamelib/player/input/input_component_interface.cpp deleted file mode 100644 index e56ab40f..00000000 --- a/impl/gamelib/player/input/input_component_interface.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "input_component_interface.hpp" diff --git a/impl/gamelib/player/input/input_component_interface.hpp b/impl/gamelib/player/input/input_component_interface.hpp deleted file mode 100644 index 74a2a156..00000000 --- a/impl/gamelib/player/input/input_component_interface.hpp +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef MYAWESOMEGAME_INPUT_COMPONENT_INTERFACE_HPP -#define MYAWESOMEGAME_INPUT_COMPONENT_INTERFACE_HPP - -#include - -class InputComponentInterface { -public: - virtual ~InputComponentInterface() = default; - - virtual void updateMovement(InputTargetInterface& target) = 0; -}; - -#endif // MYAWESOMEGAME_INPUT_COMPONENT_INTERFACE_HPP diff --git a/impl/gamelib/player/input/input_target_interface.cpp b/impl/gamelib/player/input/input_target_interface.cpp deleted file mode 100644 index 047c2253..00000000 --- a/impl/gamelib/player/input/input_target_interface.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "input_target_interface.hpp" diff --git a/impl/gamelib/player/input/input_target_interface.hpp b/impl/gamelib/player/input/input_target_interface.hpp deleted file mode 100644 index 10ae41f7..00000000 --- a/impl/gamelib/player/input/input_target_interface.hpp +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef MYAWESOMEGAME_INPUT_TARGET_INTERFACE_HPP -#define MYAWESOMEGAME_INPUT_TARGET_INTERFACE_HPP - -#include - -using InputTargetInterface = jt::Box2DObject; - -#endif // MYAWESOMEGAME_INPUT_TARGET_INTERFACE_HPP diff --git a/impl/gamelib/player/player.cpp b/impl/gamelib/player/player.cpp deleted file mode 100644 index 66ff18e8..00000000 --- a/impl/gamelib/player/player.cpp +++ /dev/null @@ -1,60 +0,0 @@ -#include "player.hpp" -#include -#include -#include -#include -#include -#include - -namespace { -std::string selectWalkAnimation(jt::Vector2f const& velocity) -{ - std::string walkAnimationName { "idle" }; - - if (jt::MathHelper::lengthSquared(velocity) < 2) { - walkAnimationName = "idle"; - } else if (abs(velocity.x) > abs(velocity.y)) { - if (velocity.x > 0) { - walkAnimationName = "right"; - } else { - walkAnimationName = "left"; - } - } else { - if (velocity.y > 0 && abs(velocity.x) < 0.1f) { - walkAnimationName = "down"; - } else if (velocity.y > 0 && velocity.x > 0) { - walkAnimationName = "down_right"; - } else if (velocity.y > 0 && velocity.x < 0) { - walkAnimationName = "down_left"; - } else { - walkAnimationName = "up"; - } - } - - return walkAnimationName; -} -} // namespace - -Player::Player( - std::shared_ptr world, b2BodyDef const* def, StateGame& state) - : jt::Box2DObject { world, def } - , m_state { state } -{ -} - -void Player::doCreate() -{ - m_input = std::make_unique(getGame()->input().keyboard()); - m_sound = std::make_unique(getGame()->audio(), getGame()->logger()); - m_graphics = std::make_unique(getGame()); -} - -void Player::doUpdate(float const elapsed) -{ - m_input->updateMovement(*this); - m_graphics->setPosition(getPosition()); - - m_graphics->setAnimationIfNotSet(selectWalkAnimation(getVelocity())); - m_graphics->updateGraphics(elapsed); -} -void Player::doDraw() const { m_graphics->draw(renderTarget()); } diff --git a/impl/gamelib/player/player.hpp b/impl/gamelib/player/player.hpp deleted file mode 100644 index 9a2c973f..00000000 --- a/impl/gamelib/player/player.hpp +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef MYAWESOMEGAME_PLAYER_HPP -#define MYAWESOMEGAME_PLAYER_HPP - -#include -#include -#include -#include - -class StateGame; - -class Player : public jt::Box2DObject { -public: - Player(std::shared_ptr world, b2BodyDef const* def, StateGame& state); - -private: - StateGame& m_state; - - std::unique_ptr m_input { nullptr }; - std::unique_ptr m_graphics { nullptr }; - std::unique_ptr m_sound { nullptr }; - - virtual void doCreate(); - virtual void doUpdate(float const elapsed); - virtual void doDraw() const; -}; - -#endif // MYAWESOMEGAME_PLAYER_HPP diff --git a/impl/gamelib/player/sound/sound_component_impl.cpp b/impl/gamelib/player/sound/sound_component_impl.cpp deleted file mode 100644 index cfdb798d..00000000 --- a/impl/gamelib/player/sound/sound_component_impl.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "sound_component_impl.hpp" -#include