Skip to content

Commit

Permalink
creating an animated entity as the base of the pnj and the character …
Browse files Browse the repository at this point in the history
…; the pnjmanager need to be enhanced we have a bug watson #3 no image for pnj ; centering map and 'content' based on upper left corner
  • Loading branch information
Loodoor committed Nov 27, 2016
1 parent 2d19b90 commit a5072b7
Show file tree
Hide file tree
Showing 54 changed files with 278 additions and 438 deletions.
Binary file modified obj/Debug/main.o
Binary file not shown.
Binary file modified obj/Debug/src/abstract/creatures_loader.o
Binary file not shown.
Binary file modified obj/Debug/src/abstract/statemachine.o
Binary file not shown.
Binary file modified obj/Debug/src/entities/character.o
Binary file not shown.
Binary file modified obj/Debug/src/entities/pnj.o
Binary file not shown.
Binary file modified obj/Debug/src/game.o
Binary file not shown.
Binary file modified obj/Debug/src/map/map.o
Binary file not shown.
Binary file modified obj/Debug/src/scripting/scripting.o
Binary file not shown.
Binary file modified obj/Debug/src/views/default.o
Binary file not shown.
Binary file removed obj/Test/main.o
Binary file not shown.
Binary file removed obj/Test/src/abstract/competence.o
Binary file not shown.
Binary file removed obj/Test/src/abstract/creatures_loader.o
Binary file not shown.
Binary file removed obj/Test/src/abstract/equip.o
Binary file not shown.
Binary file removed obj/Test/src/abstract/functions.o
Binary file not shown.
Binary file removed obj/Test/src/abstract/point.o
Binary file not shown.
Binary file removed obj/Test/src/abstract/statemachine.o
Binary file not shown.
Binary file removed obj/Test/src/abstract/texturesmanager.o
Binary file not shown.
Binary file removed obj/Test/src/bag/bag.o
Binary file not shown.
Binary file removed obj/Test/src/bag/objectstable.o
Binary file not shown.
Binary file removed obj/Test/src/bag/pockets/objects/object.o
Binary file not shown.
Binary file removed obj/Test/src/bag/pockets/pocket.o
Binary file not shown.
Binary file removed obj/Test/src/entities/character.o
Binary file not shown.
Binary file removed obj/Test/src/entities/creature.o
Binary file not shown.
Binary file removed obj/Test/src/entities/effects/sort.o
Binary file not shown.
Binary file removed obj/Test/src/game.o
Binary file not shown.
Binary file removed obj/Test/src/jsoncpp.o
Binary file not shown.
Binary file removed obj/Test/src/map/blocks/block.o
Binary file not shown.
Binary file removed obj/Test/src/map/map.o
Binary file not shown.
Binary file removed obj/Test/src/map/tilemap.o
Binary file not shown.
Binary file removed obj/Test/src/particles/particles.o
Binary file not shown.
Binary file removed obj/Test/src/scripting/scripting.o
Binary file not shown.
Binary file removed obj/Test/src/views/creatures.o
Binary file not shown.
Binary file removed obj/Test/src/views/default.o
Binary file not shown.
Binary file removed obj/Test/src/views/dex.o
Binary file not shown.
Binary file removed obj/Test/src/views/huds/hud.o
Binary file not shown.
Binary file removed obj/Test/src/views/huds/menu.o
Binary file not shown.
Binary file removed obj/Test/src/views/inventory.o
Binary file not shown.
Binary file removed obj/Test/src/views/map.o
Binary file not shown.
Binary file removed obj/Test/src/views/save.o
Binary file not shown.
Binary file removed obj/Test/src/views/view.o
Binary file not shown.
1 change: 0 additions & 1 deletion src/abstract/creatures_loader.cpp
@@ -1,7 +1,6 @@
#include<iostream>

#include "creatures_loader.hpp"
#include "../constants.hpp"

#ifdef PLATFORM_WIN
#include <windows.h>
Expand Down
1 change: 1 addition & 0 deletions src/abstract/creatures_loader.hpp
Expand Up @@ -6,6 +6,7 @@
#include <SFML/Graphics.hpp>

#include "texturesmanager.hpp"
#include "../constants.hpp"

class CreaturesLoader
{
Expand Down
1 change: 1 addition & 0 deletions src/constants.cpp
@@ -0,0 +1 @@
#include "constants.hpp"
21 changes: 0 additions & 21 deletions src/constants.hpp
Expand Up @@ -52,25 +52,4 @@ enum class ChState {
running
};

namespace Characters
{
std::vector<std::string> specials = {
"clemence",
"darth",
"elpadrino",
"euloo",
"folaefolc",
"minus",
"tress",
"vader"
};

bool is_special(const std::string& name)
{
if (std::find(specials.begin(), specials.end(), name) != specials.end())
return true;
return false;
}
}

#endif // DEF_CONSTANTS
190 changes: 190 additions & 0 deletions src/entities/animatedentity.cpp
@@ -1 +1,191 @@
#include <iostream>

#include "animatedentity.hpp"

void AnimatedEntity::update_anim(sf::Time elapsed)
{
this->elapsed_mvt_time += elapsed;

if (this->elapsed_mvt_time.asMilliseconds() % 24 < 3)
{
if (this->state == ChState::walking)
this->update_walk_anim();
else if (this->state == ChState::running)
this->update_run_anim();
}

// reset timer to prevent an overflow
if (this->elapsed_mvt_time.asSeconds() > 3600.0f)
this->elapsed_mvt_time = sf::seconds((this->elapsed_mvt_time % sf::seconds(3600.0f)).asSeconds());
}

void AnimatedEntity::update_walk_anim()
{
if (this->anim_cursor == MvState::idle)
{
this->anim_cursor = MvState::walking;
goto stop;
}
else if (this->anim_cursor == MvState::walking)
{
this->anim_cursor = MvState::idle2;
goto stop;
}
else if (this->anim_cursor == MvState::idle2)
{
this->anim_cursor = MvState::walking2;
goto stop;
}
else if (this->anim_cursor == MvState::walking2)
{
this->anim_cursor = MvState::idle;
goto stop;
}

stop:;
}

void AnimatedEntity::update_run_anim()
{
if (this->anim_cursor == MvState::idle)
{
this->anim_cursor = MvState::walking;
goto stop2;
}
else if (this->anim_cursor == MvState::walking)
{
this->anim_cursor = MvState::walking2;
goto stop2;
}
else if (this->anim_cursor == MvState::walking2)
{
this->anim_cursor = MvState::idle;
goto stop2;
}

stop2:;
}

AnimatedEntity::AnimatedEntity(int x, int y) :
pos(x, y)
, state(ChState::idle)
, anim_cursor(MvState::idle)
, direction(DIRECTION::down)
, speed(1.0f / 32.0f)
{
}

bool AnimatedEntity::load()
{
const std::vector<std::string> chtexfname = {"up", "down", "left", "right"};
std::cout << "Loading AnimatedEntity " << this->path << std::endl;

for (const auto& dir: chtexfname)
{
for (int i=0; i < 4; i++)
{
sf::Image image;

if (!image.loadFromFile(this->path + dir + to_string<int>(i) + ".png"))
{
std::cout << "Unable to open " << this->path << dir << to_string<int>(i) << ".png" << std::endl;
return false;
}

image.createMaskFromColor(sf::Color(255, 0, 255, 255));
sf::Texture texture;
texture.loadFromImage(image);

this->textures.addTexture(this->path + dir + to_string<int>(i), texture);
this->sprites.push_back(sf::Sprite(this->textures.getTexture(this->path + dir + to_string<int>(i))));
}
}
this->_load();

return true;
}

int AnimatedEntity::move(DIRECTION dir, Map map_, sf::Time elapsed)
{
// update state
if (this->state == ChState::idle)
this->state = ChState::walking; // default value, we will change it regarding the AnimatedEntity equipment in the future

// set the new direction
if (this->direction != dir)
this->anim_cursor = MvState::idle;
// the AnimatedEntity change his direction so we reset the anim cursor
// to prevent some visual glitches
this->direction = dir;

// update anim
this->not_moving_time = sf::seconds(0.0f); // reset it
this->update_anim(elapsed);

float speed = this->speed * TILE_SIZE * 2; // * (elapsed.asMilliseconds() / 100.0f);
std::vector<float> vect {0, 0};
sf::Vector2u csprite_size = (this->getCurrentSprite().getTexture())->getSize();

if (dir == DIRECTION::up)
{
if (this->pos.getY() - speed >= 0.0f)
vect[1] = -1 * speed;
}
else if (dir == DIRECTION::down)
{
if (this->pos.getY() + speed - csprite_size.y < map_.getHeight() * TILE_SIZE)
vect[1] = 1 * speed;
}
else if (dir == DIRECTION::left)
{
if (this->pos.getX() - speed >= 0.0f)
vect[0] = -1 * speed;
}
else if (dir == DIRECTION::right)
{
if (this->pos.getX() + speed - csprite_size.x < map_.getWidth() * TILE_SIZE)
vect[0] = 1 * speed;
}

bool pass = !map_.colliding_at(vect[0] / TILE_SIZE + this->pos.getX() / TILE_SIZE, vect[1] / TILE_SIZE + this->pos.getY() / TILE_SIZE)
&& !map_.colliding_at(vect[0] / TILE_SIZE + this->pos.getX() / TILE_SIZE + 1, vect[1] / TILE_SIZE + this->pos.getY() / TILE_SIZE)
&& !map_.colliding_at(vect[0] / TILE_SIZE + this->pos.getX() / TILE_SIZE, vect[1] / TILE_SIZE + this->pos.getY() / TILE_SIZE + 1)
&& !map_.colliding_at(vect[0] / TILE_SIZE + this->pos.getX() / TILE_SIZE + 1, vect[1] / TILE_SIZE + this->pos.getY() / TILE_SIZE + 1);
if (pass)
{
// we can set the new position
this->pos.move(int(vect[0]), int(vect[1]));
return 0;
}
// we need to recalculate a valid position
std::cout << "need to recalculate a valid position " << this->pos.getX() << " " << this->pos.getY() << std::endl;
return 0;
}

sf::Sprite& AnimatedEntity::getCurrentSprite()
{
return this->sprites[static_cast<int>(this->direction) * 4 + static_cast<int>(this->anim_cursor)];
}

void AnimatedEntity::update(sf::RenderWindow& window, sf::Time elapsed)
{
sf::Vector2f _pos {
float(int(this->pos.getX()))
, float(int(this->pos.getY()))
};

if (this->state != ChState::idle)
this->not_moving_time += elapsed;
if (this->not_moving_time.asSeconds() > 1.0f)
this->state = ChState::idle;
}

void AnimatedEntity::setDir(DIRECTION direc)
{
this->direction = direc;
}

Point& AnimatedEntity::getPos()
{
return this->pos;
}
36 changes: 36 additions & 0 deletions src/entities/animatedentity.hpp
@@ -1,9 +1,45 @@
#ifndef DEF_ANIMATED_ENTITY
#define DEF_ANIMATED_ENTITY

#include <algorithm>
#include <vector>
#include <string>
#include <SFML/System/Time.hpp>
#include <SFML/Graphics.hpp>

#include "../map/map.hpp"
#include "../abstract/texturesmanager.hpp"
#include "../abstract/point.hpp"
#include "../abstract/functions.hpp"
#include "../constants.hpp"

class AnimatedEntity
{
protected:
Point pos;
ChState state;
MvState anim_cursor;
DIRECTION direction;
float speed;
std::string path;
TexturesManager textures;
std::vector<sf::Sprite> sprites;
sf::Time elapsed_mvt_time;
sf::Time not_moving_time;

void update_anim(sf::Time);
void update_walk_anim();
void update_run_anim();

public:
AnimatedEntity(int x=32, int y=32);
bool load();
virtual void _load() = 0;
int move(DIRECTION, Map, sf::Time);
sf::Sprite& getCurrentSprite();
void update(sf::RenderWindow&, sf::Time);
void setDir(DIRECTION);
Point& getPos();
};

#endif // DEF_ANIMATED_ENTITY

0 comments on commit a5072b7

Please sign in to comment.