Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
skhaz committed Oct 3, 2024
1 parent 6b88ea4 commit 6480a4c
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 13 deletions.
4 changes: 0 additions & 4 deletions src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,6 @@ void engine::_loop() {

prior = now;

// const auto now = SDL_GetTicks();

_resourcemanager->update(delta);
_eventmanager->update(delta);
_entitymanager->update(delta);
Expand All @@ -141,8 +139,6 @@ void engine::_loop() {
_entitymanager->draw();
// _scenegraph->render();
_renderer->end();

// const auto delta = SDL_GetTicks() - now;
}

int32_t engine::width() const { return _window->width(); }
Expand Down
60 changes: 53 additions & 7 deletions src/entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
#include "resourcemanager.hpp"
#include "soundmanager.hpp"
#include <iostream>
#include <iterator>

using namespace framework;

// constexpr double_t GRAVITY = .5;
constexpr double_t GRAVITY = .1;

entity::entity(const entityprops &&props)
: _props(std::move(props)), _fn(nullptr) {
Expand All @@ -36,6 +37,8 @@ void entity::set_props(entityprops props) noexcept {
}

void entity::update(double delta) noexcept {
const int32_t GROUND_LEVEL = 580; // Substitua com o valor correspondente ao nível do chão no seu jogo.

if (!_props.action.empty()) {
const auto now = SDL_GetTicks();
const auto animation = _props.animations.at(_props.action);
Expand All @@ -44,18 +47,57 @@ void entity::update(double delta) noexcept {
_props.last_frame = now;
}

// Aplicação da gravidade apenas se a entidade estiver acima do chão
if (_props.gravitic && _props.position.y() < GROUND_LEVEL) {
_props.velocity.set_y(_props.velocity.y() + GRAVITY * delta);
}

// Atualiza a posição com base na velocidade
if (_props.gravitic || _props.velocity.x() != 0.0 || _props.velocity.y() != 0.0) {
const auto x = _props.position.x() + static_cast<int32_t>(_props.velocity.x() * delta);
const auto y = _props.position.y() + static_cast<int32_t>(_props.velocity.y() * delta);
_props.position.set(x, y);
const auto new_x = _props.position.x() + static_cast<int32_t>(_props.velocity.x() * delta);
auto new_y = _props.position.y() + static_cast<int32_t>(_props.velocity.y() * delta);

// Verifica se a entidade está abaixo do nível do chão
if (new_y >= GROUND_LEVEL) {
new_y = GROUND_LEVEL; // Posiciona a entidade no nível do chão
_props.velocity.set_y(0.0); // Zera a velocidade vertical
}

_props.position.set(new_x, new_y);
}
}

// Executa a função de callback, se existir
if (_fn) {
_fn(shared_from_this());
}
}

// void entity::update(double delta) noexcept {
// if (!_props.action.empty()) {
// const auto now = SDL_GetTicks();
// const auto animation = _props.animations.at(_props.action);
// if (now - _props.last_frame >= animation[_props.frame].duration) {
// _props.frame = (_props.frame + 1) % animation.size();
// _props.last_frame = now;
// }

// if (_props.gravitic) {
// _props.velocity.set_y(_props.velocity.y() + GRAVITY * delta);
// }

// if (_props.velocity.x() != 0.0 || _props.velocity.y() != 0.0) {
// const auto x = _props.position.x() + static_cast<int32_t>(_props.velocity.x() * delta);
// const auto y = _props.position.y() + static_cast<int32_t>(_props.velocity.y() * delta);
// _props.position.set(x, y);
// }
// }

// if (_fn) {
// _fn(shared_from_this());
// }
// }

void entity::draw() const noexcept {
if (!_props.action.empty()) {
const auto source = _props.animations.at(_props.action)[_props.frame].frame;
Expand All @@ -65,9 +107,9 @@ void entity::draw() const noexcept {
_props.spritesheet->draw(
source,
destination,
.0f,
graphics::flip::none,
255);
_props.angle,
_props.flip,
_props.alpha);
}
}

Expand Down Expand Up @@ -122,6 +164,10 @@ void entity::set_onupdate(const std::function<void(std::shared_ptr<entity>)> &fn
_fn = fn;
}

void entity::set_flip(graphics::flip flip) noexcept {
_props.flip = flip;
}

void entity::set_action(const std::string_view action) noexcept {
_props.action = action;
}
Expand Down
3 changes: 3 additions & 0 deletions src/entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "anchor.hpp"
#include "entityprops.hpp"
#include "pixmap.hpp"
#include "vector2d.hpp"
#include <string_view>

Expand Down Expand Up @@ -38,6 +39,8 @@ class entity : public std::enable_shared_from_this<entity> {

void play_sound(const std::string_view filename);

void set_flip(graphics::flip flip) noexcept;

void set_action(const std::string_view action) noexcept;

std::string action() const noexcept;
Expand Down
1 change: 1 addition & 0 deletions src/pixmap.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "pixmap.hpp"

#include "deleters.hpp"
#include "helpers.hpp"
#include "io.hpp"
#include "rect.hpp"
#include "renderer.hpp"
Expand Down
17 changes: 15 additions & 2 deletions src/scriptengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
#include "eventreceiver.hpp"
#include "io.hpp"
#include "loopable.hpp"
#include "noncopyable.hpp"
#include "pixmap.hpp"
#include "point.hpp"
#include "resourcemanager.hpp"
#include "soundmanager.hpp"
#include "statemanager.hpp"
#include "ticks.hpp"
#include "vector2d.hpp"
#include <sol/property.hpp>
#include <sol/types.hpp>

Expand Down Expand Up @@ -59,6 +59,13 @@ void scriptengine::run() {
"right", anchor::right,
"none", anchor::none);

lua.new_enum(
"Flip",
"none", graphics::flip::none,
"horizontal", graphics::flip::horizontal,
"vertical", graphics::flip::vertical,
"both", graphics::flip::both);

lua.new_usertype<geometry::point>(
"Point",
sol::constructors<geometry::point(int32_t, int32_t)>(),
Expand Down Expand Up @@ -112,6 +119,7 @@ void scriptengine::run() {
// "pixmap", sol::property(&entity::set_pixmap),
// "play", &entity::play_sound,
"on_update", &entity::set_onupdate,
"set_flip", &entity::set_flip,
"set_action", &entity::set_action,
"set_velocity", &entity::set_velocity,
"set_placement", &entity::set_placement);
Expand Down Expand Up @@ -144,7 +152,12 @@ void scriptengine::run() {
"mul_assign", &vector2d::operator*=,
"div_assign", &vector2d::operator/=,

sol::meta_function::equal_to, &vector2d::operator==);
sol::meta_function::equal_to, &vector2d::operator==,

"stationary", &vector2d::stationary,
"moving", &vector2d::moving,
"right", &vector2d::right,
"left", &vector2d::left);
// sol::meta_function::not_equal_to, &vector2d::operator!=);

lua.set_function("sleep", &sleep);
Expand Down
16 changes: 16 additions & 0 deletions src/vector2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,19 @@ vector2d vector2d::unit() const noexcept {
double_t vector2d::dot(const vector2d &other) const noexcept {
return _x * other._x + _y * other._y;
}

bool vector2d::stationary() const noexcept {
return _x == 0.0 && _y == 0.0;
}

bool vector2d::moving() const noexcept {
return !stationary();
}

bool vector2d::right() const noexcept {
return _x > 0;
}

bool vector2d::left() const noexcept {
return _x < 0;
}
8 changes: 8 additions & 0 deletions src/vector2d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ class vector2d {

double_t dot(const vector2d &other) const noexcept;

bool stationary() const noexcept;

bool moving() const noexcept;

bool right() const noexcept;

bool left() const noexcept;

private:
double_t _x, _y;
};

0 comments on commit 6480a4c

Please sign in to comment.