@@ -34,6 +34,7 @@ void WGDefault::genChunk(Swan::WorldPlane &plane, Swan::Chunk &chunk) { | |||
} | |||
Swan::BodyTrait::HasBody *WGDefault::spawnPlayer(Swan::WorldPlane &plane) { | |||
int x = 0; | |||
return dynamic_cast<Swan::BodyTrait::HasBody *>( | |||
plane.spawnEntity("core::player", Swan::SRFFloatArray{ 0, (float)getHeightAt(perlin_, 0) - 4 })); | |||
plane.spawnEntity("core::player", Swan::SRFFloatArray{ (float)x, (float)getHeightAt(perlin_, x) - 4 })); | |||
} |
@@ -42,11 +42,17 @@ void EntPlayer::update(const Swan::Context &ctx, float dt) { | |||
state_ = State::RUNNING_R; | |||
} | |||
bool jump_pressed = ctx.game.isKeyPressed(SDL_SCANCODE_SPACE); | |||
// Jump | |||
if (body_.on_ground_ && ctx.game.isKeyPressed(SDL_SCANCODE_SPACE) && jump_timer_.periodic(0.5)) { | |||
if (body_.on_ground_ && jump_pressed && jump_timer_.periodic(0.5)) { | |||
body_.vel_.y = -JUMP_FORCE; | |||
} | |||
// Fall down faster than we went up | |||
if (!body_.on_ground_ && (!jump_pressed || body_.vel_.y > 0)) | |||
body_.force_ += Swan::Vec2(0, DOWN_FORCE); | |||
if (state_ != oldState) | |||
anims_[(int)state_].reset(); | |||
anims_[(int)state_].tick(dt); |
@@ -22,6 +22,7 @@ public: | |||
private: | |||
static constexpr float FORCE = 3000; | |||
static constexpr float JUMP_FORCE = 11; | |||
static constexpr float DOWN_FORCE = 1500; | |||
static constexpr float MASS = 80; | |||
static constexpr Swan::Vec2 SIZE = Swan::Vec2(0.6, 1.9); | |||