Skip to content

Commit

Permalink
Working movement (includes demo).
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronamk committed Nov 5, 2021
1 parent 4766503 commit e1feb31
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 19 deletions.
10 changes: 5 additions & 5 deletions include/actor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@
*/
class Actor {
public:
Actor(const glob::vect &pos){ _pos = pos; };
Actor(const glob::vect &pos, const glob::vect &size = { 0, 0 })
: _pos(pos), _size(size) {};

const glob::vect &get_pos() { return _pos; };

void get_velocity(const glob::vect &v) { _velocity = v; };
const glob::vect &get_size() { return _size; };

virtual void update() { _pos += _velocity * glob::dt; };

virtual void draw(sf::RenderWindow &window) {};
virtual void draw(sf::RenderWindow &) {};

int priority = 0;


protected:
glob::vect _pos;
glob::vect _velocity {0,0};
glob::vect _pos, _size, _velocity;
};


Expand Down
19 changes: 14 additions & 5 deletions include/character.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@
#define CHARACTER_HPP

#include "actor.hpp"
#include "glob.hpp"


class Character : public Actor {
public:
Character(const glob::vect &pos, const glob::vect &size) : Actor(pos), _size(size) {};
void set_direction(const float &d); // d stands for direction
Character(const glob::vect &pos, const glob::vect &size = { 0, 0 })
: Actor(pos, size) {};

/**
* @param d the new direction of movement in radians
*/
void set_direction(const float &d);


protected:
glob::vect _size;
float _speed = 1;
float _speed; // speed of movement of the actor
};

#endif

#endif
23 changes: 23 additions & 0 deletions include/main_character.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef MAIN_CHARACTER_HPP
#define MAIN_CHARACTER_HPP

#include "actor.hpp"
#include "character.hpp"

#include "sprite_sheet.hpp"


class MainCharacter : public Character {
public:
MainCharacter(const glob::vect &pos);

void draw(sf::RenderWindow &) override;


private:
SpriteSheet _sprite_sheet { "../resources/cannon-barrel.png", 64 };
Loop _walking { 0, 18, 40 };
};


#endif
Binary file added resources/cannon-barrel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 4 additions & 5 deletions src/character.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#include "character.hpp"
#include <cmath>
void Character::set_direction(const float &d) {

_velocity = {std::cos(d)*_speed, std::sin(d)*_speed};
#include "character.hpp"

// glob::velocity +=

}
void Character::set_direction(const float &d) {
_velocity = { std::cos(d)*_speed, std::sin(d)*_speed };
}
16 changes: 16 additions & 0 deletions src/main_character.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "actor.hpp"
#include "character.hpp"
#include "main_character.hpp"
#include "sprite_sheet.hpp"


MainCharacter::MainCharacter(const glob::vect &pos) : Character(pos, { 30, 30 }) {
_sprite_sheet.set_loop(_walking);
_speed = 200;
}


void MainCharacter::draw(sf::RenderWindow &w) {
_sprite_sheet.set_pos(_pos);
w.draw(_sprite_sheet.get_sprite());
}
9 changes: 5 additions & 4 deletions src/state_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "state_controller.hpp"
#include "state_menu.hpp"
#include "state_playing.hpp"
#include "character.hpp"
#include "main_character.hpp"
#include "glob.hpp"


Expand Down Expand Up @@ -37,10 +37,11 @@ void StateMenu::handle_event(const sf::Event &e) {
case sf::Event::MouseButtonPressed:
// TODO: temporary crap, later use a button actor check collision
// with the mouse pointer:
if (sf::Mouse::getPosition(_state_controller->window()).x < 150)
{
if (sf::Mouse::getPosition(_state_controller->window()).x < 150) {
std::shared_ptr<StatePlaying> new_state = std::make_shared<StatePlaying>(_state_controller);
new_state->add_actor(std::make_shared<Character>(glob::vect(30,30), glob::vect(30,30)));
std::shared_ptr<MainCharacter> a = std::make_shared<MainCharacter>(glob::vect(100,100));
a->set_direction(1);
new_state->add_actor(a);
_state_controller->states().push(new_state);
}
break;
Expand Down

0 comments on commit e1feb31

Please sign in to comment.