Browse Source

world gen and player stuff

opengl-renderer-broken
Martin Dørum 4 years ago
parent
commit
42bca1603a

+ 6
- 1
core.mod/src/WGDefault.cc View File

@@ -3,8 +3,13 @@
void WGDefault::genChunk(Swan::WorldPlane &plane, Swan::Chunk &chunk) {
for (int cx = 0; cx < Swan::CHUNK_WIDTH; ++cx) {
for (int cy = 0; cy < Swan::CHUNK_HEIGHT; ++cy) {
if (chunk.pos_.y_ == 0 && cy == 3)
Swan::TilePos tpos = Swan::TilePos(cx, cy) + chunk.pos_ * Swan::TILE_SIZE;
if (tpos.y_ == 3)
chunk.tiles_[cx][cy] = tGrass_;
else if (tpos.y_ > 3 && tpos.y_ <= 5)
chunk.tiles_[cx][cy] = tDirt_;
else if (tpos.y_ > 5)
chunk.tiles_[cx][cy] = tStone_;
else
chunk.tiles_[cx][cy] = tAir_;
}

+ 3
- 2
core.mod/src/WGDefault.h View File

@@ -9,10 +9,11 @@ public:
WorldGen *create(Swan::TileMap &tmap) { return new WGDefault(tmap); }
};

Swan::Tile::ID tGrass_, tAir_;
Swan::Tile::ID tGrass_, tDirt_, tStone_, tAir_;

WGDefault(Swan::TileMap &tmap):
tGrass_(tmap.getID("core::grass")), tAir_(tmap.getID("core::air")) {}
tGrass_(tmap.getID("core::grass")), tDirt_(tmap.getID("core::dirt")),
tStone_(tmap.getID("core::stone")), tAir_(tmap.getID("core::air")) {}

void genChunk(Swan::WorldPlane &plane, Swan::Chunk &chunk) override;
Swan::Entity &spawnPlayer(Swan::WorldPlane &plane) override;

+ 2
- 4
core.mod/src/entities/EntPlayer.cc View File

@@ -5,14 +5,12 @@ void EntPlayer::draw(Swan::Win &win) {
}

void EntPlayer::update(Swan::WorldPlane &plane, float dt) {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W) || sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
body_.force_ += Swan::Vec2(0, -FORCE);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S) || sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
body_.force_ += Swan::Vec2(0, FORCE);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) || sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
body_.force_ += Swan::Vec2(-FORCE, 0);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D) || sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
body_.force_ += Swan::Vec2(FORCE, 0);
if (body_.on_ground_ && sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
body_.vel_.y_ = -JUMP_FORCE;

body_.friction(FRICTION);
body_.gravity();

+ 3
- 2
core.mod/src/entities/EntPlayer.h View File

@@ -20,8 +20,9 @@ public:
void update(Swan::WorldPlane &plane, float dt) override;

private:
static constexpr float FORCE = 600;
static constexpr float FRICTION = 100;
static constexpr float FORCE = 3000;
static constexpr float JUMP_FORCE = 7;
static constexpr float MASS = 80;
static constexpr Swan::Vec2 FRICTION = Swan::Vec2(400, 0);
static constexpr Swan::Vec2 SIZE = Swan::Vec2(1, 2);
};

+ 2
- 1
libswan/include/swan/Body.h View File

@@ -12,7 +12,7 @@ public:
Body(Vec2 pos, Vec2 size, float mass):
pos_(pos), size_(size), mass_(mass) {};

void friction(float coef);
void friction(Vec2 coef);
void gravity(Vec2 g = Vec2(0, 9.81));
void collide(WorldPlane &plane);

@@ -24,6 +24,7 @@ public:
Vec2 pos_;
Vec2 size_;
float mass_;
bool on_ground_ = false;
};

}

+ 3
- 1
libswan/src/Body.cc View File

@@ -2,7 +2,7 @@

namespace Swan {

void Body::friction(float coef) {
void Body::friction(Vec2 coef) {
force_ += -vel_ * coef;
}

@@ -15,11 +15,13 @@ void Body::collide(WorldPlane &plane) {
int endx = (int)(pos_.x_ + size_.x_);

int y = (int)(pos_.y_ + size_.y_);
on_ground_ = false;
for (int x = startx; x <= endx; ++x) {
Tile &tile = plane.getTile(TilePos(x, y));
if (tile.is_solid_) {
pos_.y_ = y - size_.y_;
vel_.y_ = 0;
on_ground_ = true;
break;
}
}

Loading…
Cancel
Save