@@ -1,19 +1,19 @@ | |||
#include "WGDefault.h" | |||
void WGDefault::genChunk(Swan::WorldPlane &plane, Swan::Chunk &chunk) { | |||
int height = perlin_.octaveNoise0_1(chunk.pos_.x_ / (64.0 / 16), 8) * 10; | |||
int depth = perlin_.octaveNoise0_1(chunk.pos_.x_ / (64.0 / 9), 5) * 10 + 10; | |||
int height = perlin_.octaveNoise0_1(chunk.pos_.x / (64.0 / 16), 8) * 10; | |||
int depth = perlin_.octaveNoise0_1(chunk.pos_.x / (64.0 / 9), 5) * 10 + 10; | |||
for (int cx = 0; cx < Swan::CHUNK_WIDTH; ++cx) { | |||
for (int cy = 0; cy < Swan::CHUNK_HEIGHT; ++cy) { | |||
Swan::TilePos tpos = Swan::TilePos(cx, cy) + Swan::TilePos( | |||
chunk.pos_.x_ * Swan::CHUNK_WIDTH, chunk.pos_.y_ * Swan::CHUNK_HEIGHT); | |||
chunk.pos_.x * Swan::CHUNK_WIDTH, chunk.pos_.y * Swan::CHUNK_HEIGHT); | |||
if (tpos.y_ == height) | |||
if (tpos.y == height) | |||
chunk.setTileID(Swan::TilePos(cx, cy), tGrass_); | |||
else if (tpos.y_ > height && tpos.y_ <= depth) | |||
else if (tpos.y > height && tpos.y <= depth) | |||
chunk.setTileID(Swan::TilePos(cx, cy), tDirt_); | |||
else if (tpos.y_ > depth) | |||
else if (tpos.y > depth) | |||
chunk.setTileID(Swan::TilePos(cx, cy), tStone_); | |||
else | |||
chunk.setTileID(Swan::TilePos(cx, cy), tAir_); |
@@ -31,7 +31,7 @@ void EntItemStack::readSRF(const Swan::Context &ctx, const Swan::SRF &srf) { | |||
Swan::SRF *EntItemStack::writeSRF(const Swan::Context &ctx) { | |||
return new Swan::SRFArray{ | |||
new Swan::SRFFloatArray{ body_.pos_.x_, body_.pos_.y_ }, | |||
new Swan::SRFFloatArray{ body_.pos_.x, body_.pos_.y }, | |||
new Swan::SRFString{ item_->name }, | |||
}; | |||
} |
@@ -49,7 +49,7 @@ void EntPlayer::update(const Swan::Context &ctx, float dt) { | |||
// Jump | |||
if (body_.on_ground_ && ctx.game.isKeyPressed(sf::Keyboard::Space) && jump_timer_.periodic(0.5)) { | |||
body_.vel_.y_ = -JUMP_FORCE; | |||
body_.vel_.y = -JUMP_FORCE; | |||
} | |||
if (state_ != oldState) | |||
@@ -68,5 +68,5 @@ void EntPlayer::readSRF(const Swan::Context &ctx, const Swan::SRF &srf) { | |||
} | |||
Swan::SRF *EntPlayer::writeSRF(const Swan::Context &ctx) { | |||
return new Swan::SRFFloatArray{ body_.pos_.x_, body_.pos_.y_ }; | |||
return new Swan::SRFFloatArray{ body_.pos_.x, body_.pos_.y }; | |||
} |
@@ -6,30 +6,32 @@ | |||
namespace Swan { | |||
template<typename T> | |||
class Vector2 { | |||
public: | |||
constexpr Vector2(T x = 0, T y = 0): x_(x), y_(y) {} | |||
struct Vector2 { | |||
T x; | |||
T y; | |||
constexpr Vector2(T x = 0, T y = 0): x(x), y(y) {} | |||
Vector2<T> &set(T x, T y) { | |||
x_ = x; | |||
y_ = y; | |||
this->x = x; | |||
this->y = y; | |||
return *this; | |||
} | |||
operator sf::Vector2<T>() const { | |||
return sf::Vector2<T>(x_, y_); | |||
return sf::Vector2<T>(x, y); | |||
} | |||
operator std::pair<T, T>() const { | |||
return std::pair<T, T>(x_, y_); | |||
return std::pair<T, T>(x, y); | |||
} | |||
operator Vector2<float>() const { | |||
return Vector2<float>(x_, y_); | |||
return Vector2<float>(x, y); | |||
} | |||
bool operator==(const Vector2<T> &vec) const { | |||
return x_ == vec.x_ && y_ == vec.y_; | |||
return x == vec.x && y == vec.y; | |||
} | |||
bool operator!=(const Vector2<T> &vec) const { | |||
@@ -37,66 +39,63 @@ public: | |||
} | |||
Vector2<T> operator-() const { | |||
return Vector2<T>(-x_, -y_); | |||
return Vector2<T>(-x, -y); | |||
} | |||
Vector2<T> operator+(const Vector2<T> &vec) const { | |||
return Vector2<T>(x_ + vec.x_, y_ + vec.y_); | |||
return Vector2<T>(x + vec.x, y + vec.y); | |||
} | |||
Vector2<T> &operator+=(const Vector2<T> &vec) { | |||
x_ += vec.x_; | |||
y_ += vec.y_; | |||
x += vec.x; | |||
y += vec.y; | |||
return *this; | |||
} | |||
Vector2<T> operator-(const Vector2<T> &vec) const { | |||
return Vector2<T>(x_ - vec.x_, y_ - vec.y_); | |||
return Vector2<T>(x - vec.x, y - vec.y); | |||
} | |||
Vector2<T> &operator-=(const Vector2<T> &vec) { | |||
x_ -= vec.x_; | |||
y_ -= vec.y_; | |||
x -= vec.x; | |||
y -= vec.y; | |||
return *this; | |||
} | |||
Vector2<T> operator*(const Vector2<T> &vec) const { | |||
return Vector2<T>(x_ * vec.x_, y_ * vec.y_); | |||
return Vector2<T>(x * vec.x, y * vec.y); | |||
} | |||
Vector2<T> &operator*=(const Vector2<T> &vec) { | |||
x_ *= vec.x_; | |||
y_ *= vec.y_; | |||
x *= vec.x; | |||
y *= vec.y; | |||
return *this; | |||
} | |||
Vector2<T> operator*(T num) const { | |||
return Vector2<T>(x_ * num, y_ * num); | |||
return Vector2<T>(x * num, y * num); | |||
} | |||
Vector2<T> operator*=(T num) { | |||
x_ *= num; | |||
y_ *= num; | |||
x *= num; | |||
y *= num; | |||
return *this; | |||
} | |||
Vector2<T> operator/(const Vector2<T> &vec) const { | |||
return Vector2<T>(x_ / vec.x_, y_ / vec.y_); | |||
return Vector2<T>(x / vec.x, y / vec.y); | |||
} | |||
Vector2<T> &operator/=(const Vector2<T> &vec) { | |||
x_ /= vec.x_; | |||
y_ /= vec.y_; | |||
x /= vec.x; | |||
y /= vec.y; | |||
return *this; | |||
} | |||
Vector2<T> operator/(T num) const { | |||
return Vector2<T>(x_ / num, y_ / num); | |||
return Vector2<T>(x / num, y / num); | |||
} | |||
Vector2<T> operator/=(T num) { | |||
x_ /= num; | |||
y_ /= num; | |||
x /= num; | |||
y /= num; | |||
return *this; | |||
} | |||
T x_; | |||
T y_; | |||
static const Vector2<T> ZERO; | |||
}; | |||
@@ -19,32 +19,32 @@ void Body::collide(WorldPlane &plane) { | |||
// I will fix later, ok? This works for now while working on more interesting things. | |||
// Collide with sides | |||
startx = (int)floor(pos_.x_); | |||
endx = (int)floor(pos_.x_ + size_.x_); | |||
y = (int)ceil(pos_.y_ + size_.y_ - 1.3); | |||
startx = (int)floor(pos_.x); | |||
endx = (int)floor(pos_.x + size_.x); | |||
y = (int)ceil(pos_.y + size_.y - 1.3); | |||
for (int x = startx; x <= endx; ++x) { | |||
for (int ry = y - 1; ry <= y; ++ry) { | |||
Tile &wall = plane.getTile(TilePos(x, ry)); | |||
if (x == startx && vel_.x_ < 0 && wall.is_solid) { | |||
vel_.x_ = 0; | |||
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.001; | |||
endx = (int)floor(pos_.x_ + size_.x_); | |||
if (x == startx && vel_.x < 0 && wall.is_solid) { | |||
vel_.x = 0; | |||
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.001; | |||
endx = (int)floor(pos_.x + size_.x); | |||
} | |||
plane.debugBox(TilePos(x, ry)); | |||
} | |||
} | |||
// Collide with top | |||
y = (int)ceil(pos_.y_ - 1); | |||
y = (int)ceil(pos_.y - 1); | |||
for (int x = startx; x <= endx; ++x) { | |||
Tile &roof = plane.getTile(TilePos(x, y)); | |||
if (roof.is_solid && vel_.y_ < 0) { | |||
pos_.y_ = y + 1; | |||
vel_.y_ = 0; | |||
if (roof.is_solid && vel_.y < 0) { | |||
pos_.y = y + 1; | |||
vel_.y = 0; | |||
} | |||
plane.debugBox(TilePos(x, y)); | |||
@@ -52,12 +52,12 @@ void Body::collide(WorldPlane &plane) { | |||
// Collide with floor | |||
on_ground_ = false; | |||
y = (int)ceil(pos_.y_ + size_.y_ - 1); | |||
y = (int)ceil(pos_.y + size_.y - 1); | |||
for (int x = startx; x <= endx; ++x) { | |||
Tile &ground = plane.getTile(TilePos(x, y)); | |||
if (ground.is_solid && vel_.y_ > 0) { | |||
pos_.y_ = y - size_.y_; | |||
vel_.y_ = 0; | |||
if (ground.is_solid && vel_.y > 0) { | |||
pos_.y = y - size_.y; | |||
vel_.y = 0; | |||
on_ground_ = true; | |||
} | |||
plane.debugBox(TilePos(x, y)); |
@@ -16,17 +16,17 @@ Tile::ID *Chunk::getTileData() { | |||
} | |||
Tile::ID Chunk::getTileID(RelPos pos) { | |||
return getTileData()[pos.y_ * CHUNK_WIDTH + pos.x_]; | |||
return getTileData()[pos.y * CHUNK_WIDTH + pos.x]; | |||
} | |||
void Chunk::setTileID(RelPos pos, Tile::ID id) { | |||
getTileData()[pos.y_ * CHUNK_WIDTH + pos.x_] = id; | |||
getTileData()[pos.y * CHUNK_WIDTH + pos.x] = id; | |||
} | |||
void Chunk::drawBlock(RelPos pos, const Tile &t) { | |||
keepActive(); | |||
visuals_->tex_.update(*t.image, pos.x_ * TILE_SIZE, pos.y_ * TILE_SIZE); | |||
visuals_->tex_.update(*t.image, pos.x * TILE_SIZE, pos.y * TILE_SIZE); | |||
visuals_->dirty_ = true; | |||
} | |||
@@ -54,11 +54,11 @@ void Chunk::compress() { | |||
compressed_size_ = destlen; | |||
fprintf(stderr, "Compressed chunk %i,%i from %lu bytes to %lu bytes in %.3fs.\n", | |||
pos_.x_, pos_.y_, CHUNK_WIDTH * CHUNK_HEIGHT * sizeof(Tile::ID), destlen, | |||
pos_.x, pos_.y, CHUNK_WIDTH * CHUNK_HEIGHT * sizeof(Tile::ID), destlen, | |||
clock.getElapsedTime().asSeconds()); | |||
} else if (ret == Z_BUF_ERROR) { | |||
fprintf(stderr, "Didn't compress chunk %i,%i because compressing it would've made it bigger.\n", | |||
pos_.x_, pos_.y_); | |||
pos_.x, pos_.y); | |||
} else { | |||
fprintf(stderr, "Chunk compression error: %i (Out of memory?)\n", ret); | |||
} | |||
@@ -89,7 +89,7 @@ void Chunk::decompress() { | |||
visuals_->dirty_ = true; | |||
need_render_ = true; | |||
fprintf(stderr, "Decompressed chunk %i,%i from %li bytes to %lu bytes in %.3fs.\n", | |||
pos_.x_, pos_.y_, compressed_size_, CHUNK_WIDTH * CHUNK_HEIGHT * sizeof(Tile::ID), | |||
pos_.x, pos_.y, compressed_size_, CHUNK_WIDTH * CHUNK_HEIGHT * sizeof(Tile::ID), | |||
clock.getElapsedTime().asSeconds()); | |||
compressed_size_ = -1; | |||
} |
@@ -53,8 +53,8 @@ void Game::createWorld(std::string worldgen) { | |||
TilePos Game::getMouseTile() { | |||
auto mousePos = getMousePos(); | |||
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_))); | |||
(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() { |
@@ -122,7 +122,7 @@ WorldPlane &World::addPlane(std::string gen) { | |||
void World::draw(Win &win) { | |||
auto bounds = *player_->getBounds(); | |||
win.cam_ = bounds.pos - (win.getSize() / 2) + (bounds.size.x_ / 2); | |||
win.cam_ = bounds.pos - (win.getSize() / 2) + (bounds.size / 2); | |||
planes_[current_plane_].draw(win); | |||
} | |||
@@ -138,7 +138,7 @@ void World::tick() { | |||
auto bounds = *player_->getBounds(); | |||
chunk_renderer_.tick( | |||
planes_[current_plane_], | |||
ChunkPos((int)bounds.pos.x_ / CHUNK_WIDTH, (int)bounds.pos.y_ / CHUNK_HEIGHT)); | |||
ChunkPos((int)bounds.pos.x / CHUNK_WIDTH, (int)bounds.pos.y / CHUNK_HEIGHT)); | |||
} | |||
} |
@@ -11,17 +11,17 @@ | |||
namespace Swan { | |||
static ChunkPos chunkPos(TilePos pos) { | |||
int chx = pos.x_ / CHUNK_WIDTH; | |||
if (pos.x_ < 0 && pos.x_ % CHUNK_WIDTH != 0) chx -= 1; | |||
int chy = pos.y_ / CHUNK_HEIGHT; | |||
if (pos.y_ < 0 && pos.y_ % CHUNK_HEIGHT != 0) chy -= 1; | |||
int chx = pos.x / CHUNK_WIDTH; | |||
if (pos.x < 0 && pos.x % CHUNK_WIDTH != 0) chx -= 1; | |||
int chy = pos.y / CHUNK_HEIGHT; | |||
if (pos.y < 0 && pos.y % CHUNK_HEIGHT != 0) chy -= 1; | |||
return ChunkPos(chx, chy); | |||
} | |||
static Chunk::RelPos relPos(TilePos pos) { | |||
int rx = pos.x_ % CHUNK_WIDTH; | |||
int rx = pos.x % CHUNK_WIDTH; | |||
if (rx < 0) rx += CHUNK_WIDTH; | |||
int ry = pos.y_ % CHUNK_HEIGHT; | |||
int ry = pos.y % CHUNK_HEIGHT; | |||
if (ry < 0) ry += CHUNK_HEIGHT; | |||
return Chunk::RelPos(rx, ry); | |||
} | |||
@@ -100,7 +100,7 @@ void WorldPlane::breakBlock(TilePos pos) { | |||
if (t.dropped_item != "") { | |||
spawnEntity("core::item-stack", SRFArray{ | |||
new SRFFloatArray{ (float)pos.x_, (float)pos.y_ }, | |||
new SRFFloatArray{ (float)pos.x, (float)pos.y }, | |||
new SRFString{ t.dropped_item }, | |||
}); | |||
} | |||
@@ -109,8 +109,8 @@ void WorldPlane::breakBlock(TilePos pos) { | |||
void WorldPlane::draw(Win &win) { | |||
auto pbounds = *world_->player_->getBounds(); | |||
ChunkPos pcpos = ChunkPos( | |||
(int)floor(pbounds.pos.x_ / CHUNK_WIDTH), | |||
(int)floor(pbounds.pos.y_ / CHUNK_HEIGHT)); | |||
(int)floor(pbounds.pos.x / CHUNK_WIDTH), | |||
(int)floor(pbounds.pos.y / CHUNK_HEIGHT)); | |||
for (int x = -1; x <= 1; ++x) { | |||
for (int y = -1; y <= 1; ++y) { |