Parcourir la source

get tile at mouse position

opengl-renderer-broken
Martin Dørum il y a 4 ans
Parent
révision
e01584038f

+ 4
- 1
core.mod/src/entities/EntPlayer.cc Voir le fichier

@@ -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;

+ 3
- 1
core.mod/src/entities/EntPlayer.h Voir le fichier

@@ -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_;
};

+ 2
- 1
libswan/include/swan/Entity.h Voir le fichier

@@ -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() {}
};


+ 6
- 1
libswan/include/swan/Game.h Voir le fichier

@@ -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_;
};

}

+ 1
- 1
libswan/include/swan/World.h Voir le fichier

@@ -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_;

+ 1
- 1
libswan/include/swan/WorldPlane.h Voir le fichier

@@ -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);

+ 2
- 2
libswan/src/Body.cc Voir le fichier

@@ -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));

+ 12
- 3
libswan/src/Game.cc Voir le fichier

@@ -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() {

+ 2
- 2
libswan/src/World.cc Voir le fichier

@@ -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() {

+ 2
- 2
libswan/src/WorldPlane.cc Voir le fichier

@@ -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() {

+ 3
- 3
src/main.cc Voir le fichier

@@ -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();
}


Chargement…
Annuler
Enregistrer