diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2018-01-06 11:30:24 +0000 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2018-01-06 11:30:24 +0000 |
commit | c7090180503f263c60ec34844992e0e8d4bea85a (patch) | |
tree | 6ecc5b2e16856db49de056738b36e1ba103d3049 /examples/shooter/player.cpp | |
parent | cf4c73f2a75b470a4d4c4167105f92bc46f1926c (diff) | |
parent | 07012cf0982d3f86aebe83b5bdc4a67332c635da (diff) | |
download | YAGE-c7090180503f263c60ec34844992e0e8d4bea85a.tar.gz YAGE-c7090180503f263c60ec34844992e0e8d4bea85a.zip |
Merge branch 'develop'
Diffstat (limited to 'examples/shooter/player.cpp')
-rw-r--r-- | examples/shooter/player.cpp | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/examples/shooter/player.cpp b/examples/shooter/player.cpp new file mode 100644 index 00000000..dab743a9 --- /dev/null +++ b/examples/shooter/player.cpp @@ -0,0 +1,72 @@ +#include "player.h" + +Player::Player(const glm::vec4 &bound, const yage::Texture &texture) + : bound_(bound), texture_(texture), direction_(Direction::DOWN), + action_(Action::IDLE), speed_(4) +{ +} + +void Player::setTexture(const yage::Texture &texture) { + texture_ = texture; +} + +void Player::draw(yage::SpriteBatch &sp) +{ + static int time = 0; + static int iteration = 0; + float width = 1.f / static_cast<float>(texture_.x); + float height = 1.f / static_cast<float>(texture_.y); + + switch (action_) { + case Action::IDLE: + sp.draw(bound_, + {width, static_cast<int>(direction_) * height, width, height}, + texture_.id, yage::Colour(255, 255, 255, 255), 1); + break; + case Action::MOVING: + if(time % 15 == 0) { + iteration = (iteration + 1) % 2; + } + sp.draw(bound_, + {iteration * 2 * width, static_cast<int>(direction_) * height, width, height}, + texture_.id, yage::Colour(255, 255, 255, 255), 1); + time = (time + 1) % 59; + break; + } +} + +void Player::move(Direction direction) +{ + direction_ = direction; + action_ = Action::MOVING; + + switch (direction_) { + case Direction::LEFT: + bound_.x -= speed_; + break; + case Direction::DOWN: + bound_.y -= speed_; + break; + case Direction::RIGHT: + bound_.x += speed_; + break; + case Direction::UP: + bound_.y += speed_; + break; + } +} + +void Player::idle() +{ + action_ = Action::IDLE; +} + +void Player::look(Direction direction) +{ + direction_ = direction; +} + +glm::vec4 Player::position() const +{ + return bound_; +} |