more enemy variants

main
Efertone 10 months ago
parent 591f8d0c7b
commit 4a4bc72e2d

@ -11,6 +11,9 @@ include_directories(headers headers/ds headers/game "${SFML_ROOT}/include")
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake_modules")
add_custom_target(copy_assets COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_LIST_DIR}/assets ${CMAKE_CURRENT_BINARY_DIR}/assets)
add_dependencies(playground copy_assets)
find_package(SFML REQUIRED system window graphics network audio)
if (SFML_FOUND)
include_directories(${SFML_INCLUDE_DIR})
@ -19,8 +22,5 @@ endif ()
if (WIN32)
file(GLOB BINARY_DEP_DLLS "${SFML_INCLUDE_DIR}/../bin/*.dll")
file(COPY ${BINARY_DEP_DLLS} DESTINATION ${CMAKE_BINARY_DIR})
endif ()
file(GLOB ASSETS "${CMAKE_CURRENT_LIST_DIR}/assets/*")
file(COPY ${ASSETS} DESTINATION ${CMAKE_BINARY_DIR}/assets/)
file(COPY ${BINARY_DEP_DLLS} DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
endif ()

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Before

Width:  |  Height:  |  Size: 1.6 KiB

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

@ -14,6 +14,7 @@ namespace game {
namespace actor {
enum Type { UnknownType, EnemyType, PlayerType };
enum AnimationState { Idle, Special, Walk };
enum Direction { Up, Down, Left, Right };
class Base : public game::interface::Drawable,
public game::interface::EventSubscriber,
@ -43,6 +44,8 @@ namespace game {
sf::Color m_color;
sf::Font m_hud_font;
Direction m_facing;
AnimationState m_animation_state;
graphics::AnimatedSprite m_animation_idle;

@ -21,6 +21,7 @@ namespace game {
private:
sf::Clock m_movementClock;
int m_direction;
float m_speed;
void initSprite(ds::Vector position, sf::Sprite sprite);
float randomGenerator();

@ -18,6 +18,7 @@ namespace graphics {
void setTexture(const std::basic_string<char> &filename);
void setLength(int length);
void setSize(int width, int height);
void setSize(int width, int height, int crop_x, int crop_y);
void setPosition(float x, float y);
void setScale(float x, float y);
@ -36,6 +37,10 @@ namespace graphics {
int m_width{};
int m_height{};
std::pair<int, int> m_crop{};
int actualWidth() const;
int actualHeight() const;
};
}

@ -26,6 +26,7 @@ void game::actor::Base::init(ds::Vector position, sf::RectangleShape shape, sf::
void game::actor::Base::initSprite(ds::Vector position, sf::Sprite sprite) {
this->m_position = position;
this->m_animation_state = game::actor::Idle;
this->m_facing = game::actor::Right;
initHudElements();
}

@ -9,6 +9,7 @@
#include <iostream>
#define PLAYER_SPEED 200
#define PLAYER_SCALE 1.5f
game::actor::Player::Player() : Base() {}
@ -68,6 +69,8 @@ void game::actor::Player::update(float dt, sf::RenderTarget* window, std::vector
if (this->m_animationLockClock.getElapsedTime().asMilliseconds() < 1000) {
this->updateAnimation(game::actor::Special);
this->m_currentAnimation->setScale((this->m_facing == game::actor::Left ? -1.f : 1.f) * PLAYER_SCALE, PLAYER_SCALE);
return;
}
@ -102,23 +105,27 @@ void game::actor::Player::update(float dt, sf::RenderTarget* window, std::vector
this->moveLeft(deltaValue);
// Update in case we have other buttons pressed.
shapeBound = this->getGlobalBounds();
this->m_facing = game::actor::Left;
nextState = game::actor::Walk;
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
if (shapeBound.left + shapeBound.width + deltaValue < float(windowBound.x)) {
this->moveRight(deltaValue);
this->m_facing = game::actor::Right;
nextState = game::actor::Walk;
}
}
this->updateAnimation(nextState);
this->m_currentAnimation->setScale((this->m_facing == game::actor::Left ? -1.f : 1.f) * PLAYER_SCALE, PLAYER_SCALE);
}
void game::actor::Player::collisionAlert(game::actor::Base *) {
this->m_hits++;
this->m_specialAnimationLocked = true;
this->updateTexture(game::actor::Special);
// this->updateTexture(game::actor::Special);
this->m_animationLockClock.restart();
}
@ -126,22 +133,22 @@ void game::actor::Player::initSprite(ds::Vector position, sf::Sprite sprite) {
Base::initSprite(position, std::move(sprite));
// Idle
this->m_animation_idle.setSize(48, 48);
this->m_animation_idle.setSize(48, 48, 32, 0);
this->m_animation_idle.setLength(4);
this->m_animation_idle.setTexture("assets/sprite/player_idle.png");
this->m_animation_idle.setScale(1.5f, 1.5f);
this->m_animation_idle.setScale(PLAYER_SCALE, PLAYER_SCALE);
// Special
this->m_animation_special.setSize(48, 48);
this->m_animation_special.setSize(48, 48, 32, 0);
this->m_animation_special.setLength(6);
this->m_animation_special.setTexture("assets/sprite/player_special.png");
this->m_animation_special.setScale(1.5f, 1.5f);
this->m_animation_special.setScale(PLAYER_SCALE, PLAYER_SCALE);
// Walk
this->m_animation_walk.setSize(48, 48);
this->m_animation_walk.setSize(48, 48, 32, 0);
this->m_animation_walk.setLength(6);
this->m_animation_walk.setTexture("assets/sprite/player_walk.png");
this->m_animation_walk.setScale(1.5f, 1.5f);
this->m_animation_walk.setScale(PLAYER_SCALE, PLAYER_SCALE);
this->updateTexture(game::actor::Idle);
}

@ -7,6 +7,7 @@
#include <random>
#define SIMPLE_ENEMY_SPEED 20
#define SIMPLE_ENEMY_SCALE 1.5f
game::actor::SimpleEnemy::SimpleEnemy() : Base() {}
@ -36,10 +37,11 @@ void game::actor::SimpleEnemy::update(float dt, sf::RenderTarget* window, std::v
this->m_movementClock.restart();
}
float deltaValue = SIMPLE_ENEMY_SPEED * this->randomGenerator() * dt;
float deltaValue = this->m_speed * this->randomGenerator() * dt;
game::actor::AnimationState nextState = game::actor::Idle;
float diffValue;
// Up
if (this->m_direction == 0 || this->m_direction == 4 || this->m_direction == 5) {
diffValue = shapeBound.top - deltaValue;
if (diffValue > 0 && !this->doesCollideWithAny(diffValue, shapeBound.left, others)) {
@ -48,6 +50,7 @@ void game::actor::SimpleEnemy::update(float dt, sf::RenderTarget* window, std::v
}
}
// Down
if (this->m_direction == 1 || this->m_direction == 6 || this->m_direction == 7) {
diffValue = shapeBound.top + shapeBound.height + deltaValue;
if (diffValue < float(windowBound.y) && !this->doesCollideWithAny(diffValue, shapeBound.left, others)) {
@ -56,37 +59,71 @@ void game::actor::SimpleEnemy::update(float dt, sf::RenderTarget* window, std::v
}
}
// Left
if (this->m_direction == 2 || this->m_direction == 4 || this->m_direction == 6) {
diffValue = shapeBound.left - deltaValue;
if (diffValue > 0 && !this->doesCollideWithAny(shapeBound.top, diffValue, others)) {
this->moveLeft(deltaValue);
nextState = game::actor::Walk;
this->m_facing = game::actor::Left;
}
}
// Right
if (this->m_direction == 3 || this->m_direction == 5 || this->m_direction == 7) {
diffValue = shapeBound.left + shapeBound.width + deltaValue;
if (diffValue < float(windowBound.x) && !this->doesCollideWithAny(shapeBound.top, diffValue, others)) {
this->moveRight(deltaValue);
nextState = game::actor::Walk;
this->m_facing = game::actor::Right;
}
}
this->updateAnimation(nextState);
this->m_currentAnimation->setScale((this->m_facing == game::actor::Left ? -1.f : 1.f) * SIMPLE_ENEMY_SCALE, SIMPLE_ENEMY_SCALE);
}
void game::actor::SimpleEnemy::initSprite(ds::Vector position, sf::Sprite sprite) {
Base::initSprite(position, std::move(sprite));
std::basic_string<char> path;
int variant = (int(this->randomGenerator()) % 3) + 49;
if (variant == '1')
this->m_speed = SIMPLE_ENEMY_SPEED * 1.5;
else if (variant == '2')
this->m_speed = SIMPLE_ENEMY_SPEED;
else if (variant == '3')
this->m_speed = SIMPLE_ENEMY_SPEED * 0.5;
// Idle
this->m_animation_idle.setSize(48, 48);
this->m_animation_idle.setSize(48, 48, 32, 0);
this->m_animation_idle.setLength(4);
path = "assets/sprite/simpleEnemy_idle_";
path.push_back(char(variant));
path.append(".png");
this->m_animation_idle.setTexture(path);
this->m_animation_idle.setScale(SIMPLE_ENEMY_SCALE, SIMPLE_ENEMY_SCALE);
// Special (nothing)
this->m_animation_idle.setSize(48, 48, 32, 0);
this->m_animation_idle.setLength(4);
this->m_animation_idle.setTexture("assets/sprite/simpleEnemy_idle.png");
path = "assets/sprite/simpleEnemy_idle_";
path.push_back(char(variant));
path.append(".png");
this->m_animation_idle.setTexture(path);
this->m_animation_idle.setScale(SIMPLE_ENEMY_SCALE, SIMPLE_ENEMY_SCALE);
// Walk
this->m_animation_walk.setSize(48, 48);
this->m_animation_walk.setSize(48, 48, 32, 0);
this->m_animation_walk.setLength(6);
this->m_animation_walk.setTexture("assets/sprite/simpleEnemy_walk.png");
path = "assets/sprite/simpleEnemy_walk_";
path.push_back(char(variant));
path.append(".png");
this->m_animation_walk.setTexture(path);
this->m_animation_idle.setScale(SIMPLE_ENEMY_SCALE, SIMPLE_ENEMY_SCALE);
this->updateTexture(game::actor::Idle);

@ -5,6 +5,7 @@
#include <iostream>
#include <utility>
#include "graphics/AnimatedSprite.h"
#define DEBUG false
graphics::AnimatedSprite::AnimatedSprite() = default;
graphics::AnimatedSprite::~AnimatedSprite() = default;
@ -32,17 +33,39 @@ void graphics::AnimatedSprite::setLength(int length) {
void graphics::AnimatedSprite::setSize(int width, int height) {
this->m_width = width;
this->m_height = height;
this->m_sprite.setOrigin(float(this->actualWidth())/2, float(height));
this->m_sprite.setTextureRect(sf::IntRect(
this->m_currentFrame * this->m_width, 0,
this->m_width, this->m_height));
this->actualWidth(), this->actualHeight()));
}
int graphics::AnimatedSprite::actualWidth() const {
if (this->m_crop.first > 0) {
return this->m_crop.first;
}
return this->m_width;
}
int graphics::AnimatedSprite::actualHeight() const {
if (this->m_crop.second > 0) {
return this->m_crop.second;
}
return this->m_width;
}
void graphics::AnimatedSprite::setSize(int width, int height, int crop_x, int crop_y) {
this->m_crop = {crop_x, crop_y};
this->setSize(width, height);
}
void graphics::AnimatedSprite::update() {
if (this->m_animationClock.getElapsedTime().asMilliseconds() > 100) {
this->m_animationClock.restart();
sf::IntRect tile(this->m_currentFrame * this->m_width, 0, this->m_width, this->m_height);
sf::IntRect tile(this->m_currentFrame * this->m_width, 0, this->actualWidth(), this->actualHeight());
this->m_sprite.setTextureRect(tile);
this->m_currentFrame = (this->m_currentFrame + 1) % this->m_length;
@ -55,6 +78,22 @@ void graphics::AnimatedSprite::reset() {
}
void graphics::AnimatedSprite::draw(sf::RenderTarget *target) {
if (DEBUG) {
sf::CircleShape circle;
circle.setFillColor(sf::Color::Red);
circle.setRadius(5.f);
circle.setPosition(this->m_sprite.getPosition());
target->draw(circle);
sf::RectangleShape bound;
bound.setPosition(this->m_sprite.getGlobalBounds().left, this->m_sprite.getGlobalBounds().top);
bound.setSize(sf::Vector2<float>(this->m_sprite.getGlobalBounds().width, this->m_sprite.getGlobalBounds().height));
bound.setOutlineColor(sf::Color::Green);
bound.setOutlineThickness(2.f);
bound.setFillColor(sf::Color::Transparent);
target->draw(bound);
}
target->draw(this->m_sprite);
}

Loading…
Cancel
Save