@@ -17,10 +17,13 @@ void EntPlayer::draw(Swan::Win &win) { | |||
anims_[(int)state_].draw(win); | |||
} | |||
void EntPlayer::update(Swan::WorldPlane &plane, float dt) { | |||
void EntPlayer::update(Swan::Game &game, Swan::WorldPlane &plane, float dt) { | |||
State oldState = state_; | |||
state_ = State::IDLE; | |||
mouse_tile_ = game.getMouseTile(); | |||
plane.debugBox(mouse_tile_); | |||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) || sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { | |||
body_.force_ += Swan::Vec2(-FORCE, 0); | |||
state_ = State::RUNNING_L; |
@@ -16,7 +16,7 @@ public: | |||
const Swan::Vec2 &getPos() override { return body_.pos_; } | |||
void draw(Swan::Win &win) override; | |||
void update(Swan::WorldPlane &plane, float dt) override; | |||
void update(Swan::Game &game, Swan::WorldPlane &plane, float dt) override; | |||
private: | |||
static constexpr float FORCE = 3000; | |||
@@ -35,5 +35,7 @@ private: | |||
State state_ = State::IDLE; | |||
Swan::Animation anims_[(int)State::COUNT]; | |||
Swan::TilePos mouse_tile_; | |||
Swan::Body body_; | |||
}; |
@@ -8,6 +8,7 @@ namespace Swan { | |||
class World; | |||
class WorldPlane; | |||
class Game; | |||
class Entity { | |||
public: | |||
@@ -23,7 +24,7 @@ public: | |||
virtual const Vec2 &getPos() { return Vec2::ZERO; } | |||
virtual void draw(Win &win) {} | |||
virtual void update(WorldPlane &plane, float dt) {} | |||
virtual void update(Game &game, WorldPlane &plane, float dt) {} | |||
virtual void tick() {} | |||
}; | |||
@@ -12,10 +12,14 @@ namespace Swan { | |||
class Game { | |||
public: | |||
Game(Win &win): win_(win) {} | |||
void loadMod(const std::string &path); | |||
void createWorld(std::string worldgen); | |||
void draw(Win &win); | |||
TilePos getMouseTile(); | |||
void draw(); | |||
void update(float dt); | |||
void tick(); | |||
@@ -25,6 +29,7 @@ public: | |||
private: | |||
std::vector<Mod> registered_mods_; | |||
Win &win_; | |||
}; | |||
} |
@@ -33,7 +33,7 @@ public: | |||
Tile &getTile(const std::string &name); | |||
void draw(Win &win); | |||
void update(float dt); | |||
void update(Game &game, float dt); | |||
void tick(); | |||
std::map<std::string, std::shared_ptr<WorldGen::Factory>> worldgens_; |
@@ -31,7 +31,7 @@ public: | |||
Entity &spawnPlayer(); | |||
void draw(Win &win); | |||
void update(float dt); | |||
void update(Game &game, float dt); | |||
void tick(); | |||
void debugBox(TilePos pos); |
@@ -22,11 +22,11 @@ void Body::collide(WorldPlane &plane) { | |||
Tile &wall = plane.getTile(TilePos(x, y)); | |||
if (x == startx && vel_.x_ < 0 && wall.is_solid_) { | |||
vel_.x_ = 0; | |||
pos_.x_ = startx + 1.01; | |||
pos_.x_ = startx + 1.001; | |||
startx = (int)floor(pos_.x_); | |||
} else if (x == endx && vel_.x_ > 0 && wall.is_solid_) { | |||
vel_.x_ = 0; | |||
pos_.x_ = endx - size_.x_ - 0.01; | |||
pos_.x_ = endx - size_.x_ - 0.001; | |||
endx = (int)floor(pos_.x_ + size_.x_); | |||
} | |||
plane.debugBox(TilePos(x, y)); |
@@ -1,6 +1,8 @@ | |||
#include "Game.h" | |||
#include <dlfcn.h> | |||
#include <math.h> | |||
#include <SFML/Window/Mouse.hpp> | |||
#include "Tile.h" | |||
#include "Asset.h" | |||
@@ -47,12 +49,19 @@ void Game::createWorld(std::string worldgen) { | |||
world_->spawnPlayer(); | |||
} | |||
void Game::draw(Win &win) { | |||
world_->draw(win); | |||
TilePos Game::getMouseTile() { | |||
auto mousePos = sf::Mouse::getPosition(*win_.window_); | |||
return TilePos( | |||
(int)floor(win_.cam_.x_ + mousePos.x / (Swan::TILE_SIZE * win_.scale_)), | |||
(int)floor(win_.cam_.y_ + mousePos.y / (Swan::TILE_SIZE * win_.scale_))); | |||
} | |||
void Game::draw() { | |||
world_->draw(win_); | |||
} | |||
void Game::update(float dt) { | |||
world_->update(dt); | |||
world_->update(*this, dt); | |||
} | |||
void Game::tick() { |
@@ -116,9 +116,9 @@ void World::draw(Win &win) { | |||
planes_[current_plane_].draw(win); | |||
} | |||
void World::update(float dt) { | |||
void World::update(Game &game, float dt) { | |||
for (auto &plane: planes_) | |||
plane.update(dt); | |||
plane.update(game, dt); | |||
} | |||
void World::tick() { |
@@ -80,10 +80,10 @@ void WorldPlane::draw(Win &win) { | |||
} | |||
} | |||
void WorldPlane::update(float dt) { | |||
void WorldPlane::update(Game &game, float dt) { | |||
debug_boxes_.clear(); | |||
for (auto &ent: entities_) | |||
ent->update(*this, dt); | |||
ent->update(game, *this, dt); | |||
} | |||
void WorldPlane::tick() { |
@@ -17,14 +17,14 @@ int main() { | |||
abort(); | |||
} | |||
sf::RenderWindow window(sf::VideoMode(800, 600), "good gaem"); | |||
sf::RenderWindow window(sf::VideoMode(800, 600), "Project: SWAN"); | |||
window.setVerticalSyncEnabled(true); | |||
window.setIcon(icon.getSize().x, icon.getSize().y, icon.getPixelsPtr()); | |||
Win win(&window); | |||
Game::initGlobal(); | |||
Game game; | |||
Game game(win); | |||
game.loadMod("core.mod"); | |||
game.createWorld("core::default"); | |||
@@ -80,7 +80,7 @@ int main() { | |||
} | |||
window.clear(); | |||
game.draw(win); | |||
game.draw(); | |||
window.display(); | |||
} | |||