Browse Source

make player emit light

fix/style
Martin Dørum 3 years ago
parent
commit
2cfa6b547f
2 changed files with 19 additions and 1 deletions
  1. 14
    1
      core.mod/src/entities/PlayerEntity.cc
  2. 5
    0
      core.mod/src/entities/PlayerEntity.h

+ 14
- 1
core.mod/src/entities/PlayerEntity.cc View File

@@ -26,18 +26,31 @@ void PlayerEntity::update(const Swan::Context &ctx, float dt) {
mouse_tile_ = ctx.game.getMouseTile();
ctx.plane.debugBox(mouse_tile_);
jump_timer_.tick(dt);
place_timer_.tick(dt);

// Break block
if (ctx.game.isMousePressed(SDL_BUTTON_LEFT))
ctx.plane.breakTile(mouse_tile_);

// Place block
if (ctx.game.isMousePressed(SDL_BUTTON_RIGHT)) {
if (ctx.game.isMousePressed(SDL_BUTTON_RIGHT) && place_timer_.periodic(0.50)) {
if (ctx.plane.getTileID(mouse_tile_) == ctx.world.getTileID("@::air")) {
ctx.plane.setTile(mouse_tile_, "core::torch");
}
}

Swan::Vec2 headPos = body_.topMid() + Swan::Vec2(0, 0.5);
Swan::TilePos tilePos = Swan::Vec2i(floor(headPos.x), floor(headPos.y));
if (!placed_light_) {
ctx.plane.addLight(tilePos, LIGHT_LEVEL);
placed_light_ = true;
light_tile_ = tilePos;
} else if (tilePos != light_tile_) {
ctx.plane.removeLight(light_tile_, LIGHT_LEVEL);
ctx.plane.addLight(tilePos, LIGHT_LEVEL);
light_tile_ = tilePos;
}

// Move left
if (ctx.game.isKeyPressed(SDL_SCANCODE_A) || ctx.game.isKeyPressed(SDL_SCANCODE_LEFT)) {
physics_.force += Swan::Vec2(-MOVE_FORCE, 0);

+ 5
- 0
core.mod/src/entities/PlayerEntity.h View File

@@ -24,6 +24,7 @@ private:
static constexpr float JUMP_VEL = 11;
static constexpr float DOWN_FORCE = 20 * MASS;
static constexpr Swan::Vec2 SIZE = Swan::Vec2(0.6, 1.9);
static constexpr int LIGHT_LEVEL = 8;

enum class State {
IDLE,
@@ -46,7 +47,11 @@ private:
std::array<Swan::Animation, (int)State::COUNT> anims_;

Swan::Clock jump_timer_;
Swan::Clock place_timer_;
Swan::TilePos mouse_tile_;

Swan::TilePos light_tile_;
bool placed_light_ = false;

BasicInventory inventory_{INVENTORY_SIZE};
};

Loading…
Cancel
Save