From 410f971972bde37fb852420ea2ca0e2f69f27256 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sat, 30 Jan 2021 09:41:31 -0500 Subject: Encapsulated some player movement stuff Imported vector from therapy5 --- src/consts.h | 2 ++ src/game.h | 7 ++++ src/main.cpp | 27 ++++++--------- src/party.cpp | 42 +++++++++++++++++++++++ src/party.h | 23 +++++++++++++ src/renderer.cpp | 2 +- src/sprite.h | 19 ++++------- src/vector.h | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 195 insertions(+), 29 deletions(-) create mode 100644 src/party.cpp create mode 100644 src/party.h create mode 100644 src/vector.h (limited to 'src') diff --git a/src/consts.h b/src/consts.h index 2088f45..96176a4 100644 --- a/src/consts.h +++ b/src/consts.h @@ -4,4 +4,6 @@ const int GAME_WIDTH = 640; const int GAME_HEIGHT = 480; +const int MOVEMENT_SPEED = 4; + #endif /* end of include guard: CONSTS_H_9561E49C */ diff --git a/src/game.h b/src/game.h index 46f1ab2..bb99543 100644 --- a/src/game.h +++ b/src/game.h @@ -4,6 +4,13 @@ #include #include "sprite.h" +struct Input { + bool left = false; + bool right = false; + bool up = false; + bool down = false; +}; + class Game { public: diff --git a/src/main.cpp b/src/main.cpp index 8bd7f89..b282a2e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,11 +1,17 @@ #include #include "renderer.h" #include "game.h" +#include "party.h" void loop(Renderer& renderer) { Game game; int playerSpriteId = game.addSprite(Sprite("../res/lucas.png", renderer)); + Input keystate; + + Party party; + party.addMember(playerSpriteId); + renderer.render(game); size_t inputDt = 50; @@ -25,28 +31,17 @@ void loop(Renderer& renderer) { } } - Sprite& playerSprite = game.getSprite(playerSpriteId); const Uint8* state = SDL_GetKeyboardState(NULL); + keystate.left = state[SDL_SCANCODE_LEFT]; + keystate.right = state[SDL_SCANCODE_RIGHT]; + keystate.up = state[SDL_SCANCODE_UP]; + keystate.down = state[SDL_SCANCODE_DOWN]; inputAcc += frameTime; while (inputAcc > inputDt) { inputAcc -= inputDt; - if (state[SDL_SCANCODE_LEFT]) { - playerSprite.setX(playerSprite.getX()-8); - } - - if (state[SDL_SCANCODE_RIGHT]) { - playerSprite.setX(playerSprite.getX()+8); - } - - if (state[SDL_SCANCODE_UP]) { - playerSprite.setY(playerSprite.getY()-8); - } - - if (state[SDL_SCANCODE_DOWN]) { - playerSprite.setY(playerSprite.getY()+8); - } + party.move(game, keystate); } renderer.render(game); diff --git a/src/party.cpp b/src/party.cpp new file mode 100644 index 0000000..559aacb --- /dev/null +++ b/src/party.cpp @@ -0,0 +1,42 @@ +#include "party.h" +#include "consts.h" + +void Party::addMember(int spriteId) { + PartyMember newMember; + newMember.spriteId = spriteId; + + members_.push_back(std::move(newMember)); +} + +void Party::move(Game& game, const Input& keystate) { + if (members_.empty()) { + return; + } + + Sprite& p1 = game.getSprite(members_[0].spriteId); + vec2i pLoc = p1.loc(); + + if (keystate.up) + { + pLoc.y() -= MOVEMENT_SPEED; + } + + if (keystate.down) + { + pLoc.y() += MOVEMENT_SPEED; + } + + if (keystate.left) + { + pLoc.x() -= MOVEMENT_SPEED; + } + + if (keystate.right) + { + pLoc.x() += MOVEMENT_SPEED; + } + + if (pLoc != p1.loc()) { + p1.loc() = pLoc; + } +} \ No newline at end of file diff --git a/src/party.h b/src/party.h new file mode 100644 index 0000000..18c57b5 --- /dev/null +++ b/src/party.h @@ -0,0 +1,23 @@ +#ifndef PARTY_H_826F91BA +#define PARTY_H_826F91BA + +#include +#include "game.h" + +class Party { +public: + + void addMember(int spriteId); + + void move(Game& game, const Input& keystate); + +private: + + struct PartyMember { + int spriteId; + }; + + std::vector members_; +}; + +#endif /* end of include guard: PARTY_H_826F91BA */ diff --git a/src/renderer.cpp b/src/renderer.cpp index 3bd0beb..bcc3e95 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -49,7 +49,7 @@ void Renderer::render(Game& game) { SDL_RenderClear(ren_.get()); for (Sprite& sprite : game.getSprites()) { - SDL_Rect dest { sprite.getX(), sprite.getY(), sprite.getWidth(), sprite.getHeight() }; + SDL_Rect dest { sprite.loc().x(), sprite.loc().y(), sprite.size().w(), sprite.size().h() }; SDL_RenderCopy(ren_.get(), textures_.at(sprite.getTextureId()).get(), nullptr, &dest); } diff --git a/src/sprite.h b/src/sprite.h index d868bdb..c45e208 100644 --- a/src/sprite.h +++ b/src/sprite.h @@ -3,6 +3,7 @@ #include #include "renderer.h" +#include "vector.h" class Sprite { public: @@ -13,25 +14,19 @@ public: return textureId_; } - int getX() { return x_; } + const vec2i& loc() const { return loc_; } - void setX(int x) { x_ = x; } + vec2i& loc() { return loc_; } - void setY(int y) { y_ = y; } + const vec2i& size() const { return size_; } - int getY() { return y_; } - - int getWidth() { return width_; } - - int getHeight() { return height_; } + vec2i& size() { return size_; } private: int textureId_; - int x_ = 0; - int y_ = 0; - int width_ = 17*4; - int height_ = 27*4; + vec2i loc_ { 0, 0 }; + vec2i size_ { 17*4, 27 * 4 }; }; #endif /* end of include guard: SPRITE_H_70503825 */ diff --git a/src/vector.h b/src/vector.h new file mode 100644 index 0000000..9973ea6 --- /dev/null +++ b/src/vector.h @@ -0,0 +1,102 @@ +#ifndef VECTOR_H_5458ED71 +#define VECTOR_H_5458ED71 + +template +class vec2 { +public: + + T coords[2]; + + vec2() : coords{0, 0} { + } + + vec2(T x, T y) : coords{x, y} { + } + + inline T& x() { + return coords[0]; + } + + inline const T& x() const { + return coords[0]; + } + + inline T& w() { + return coords[0]; + } + + inline const T& w() const { + return coords[0]; + } + + inline T& y() { + return coords[1]; + } + + inline const T& y() const { + return coords[1]; + } + + inline T& h() { + return coords[1]; + } + + inline const T& h() const { + return coords[1]; + } + + template + operator vec2() const { + return vec2(x(), y()); + } + + vec2 operator+(const vec2& other) const { + return vec2(x() + other.x(), y() + other.y()); + } + + vec2& operator+=(const vec2& other) { + x() += other.x(); + y() += other.y(); + + return *this; + } + + vec2 operator-(const vec2& other) const { + return vec2(x() - other.x(), y() - other.y()); + } + + vec2 operator-=(const vec2& other) { + x() -= other.x(); + y() -= other.y(); + + return *this; + } + + vec2 operator-() const { + return vec2(-x(), -y()); + } + + vec2 operator*(T s) const { + return vec2(x() * s, y() * s); + } + + vec2& operator*=(T s) { + x() *= s; + y() *= s; + + return *this; + } + + bool operator==(const vec2& other) const { + return std::tie(x(), other.x()) == std::tie(y(), other.y()); + } + + bool operator!=(const vec2& other) const { + return std::tie(x(), other.x()) != std::tie(y(), other.y()); + } + +}; + +using vec2i = vec2; + +#endif /* end of include guard: VECTOR_H_5458ED71 */ -- cgit 1.4.1