Tile Implementations

This commit is contained in:
2024-12-17 15:20:59 -06:00
parent fcd580cc3b
commit 717182c35c
6 changed files with 167 additions and 103 deletions

View File

@@ -170,29 +170,9 @@ namespace CaveGame::Client {
}
JGL::J2D::DrawPoint(t_color, tile_coords);
/*if (tile == TileID::STONE) // Stone
Core::Tiles::Stone::Draw(tile_coords);
//stone_coords.push_back(real_tile_coords);
else if (tile == TileID::DIRT) // Dirt
//dirt_coords.push_back(real_tile_coords);
else if (tile == TileID::GRASS) // Grass
//grass_coords.push_back(real_tile_coords);
else if (tile == TileID::CLAY) // Ore
//clay_coords.push_back(real_tile_coords);
else if (tile == TileID::MUD) // Water
//mud_coords.push_back(real_tile_coords); */
}
}
JGL::J2D::End();
//JGL::J2D::Begin(destination, true);
//JGL::J2D::Begin(destination, true);
// JGL::J2D::DrawPoints(Colors::Gray, stone_coords.data(), stone_coords.size());
// JGL::J2D::DrawPoints(Colors::Browns::Chocolate, dirt_coords.data(), dirt_coords.size());
// JGL::J2D::DrawPoints(Colors::Green, grass_coords.data(), grass_coords.size());
// JGL::J2D::DrawPoints(Colors::Reds::Firebrick, clay_coords.data(), clay_coords.size());
// JGL::J2D::DrawPoints(Colors::Browns::BurlyWood, mud_coords.data(), mud_coords.size());
//JGL::J2D::Begin(destination, true);
}
void LocalWorld::RenderChunk(const Vector2& coords, const Core::Chunk& chunk) {

View File

@@ -70,6 +70,7 @@ namespace CaveGame::ClientApp
/// This function sets up the initial program state, particularly that which must be performed **after** the window is opened.
void Init();
void tile_draw(int x, int y, int radius, Core::TileID tile);
/// This function cleans up any data or assets before closing the game.
void Cleanup();

View File

@@ -129,6 +129,18 @@ namespace CaveGame::ClientApp
create_window_widgets();
}
void CaveGameWindow::tile_draw(int x, int y, int radius, Core::TileID tile)
{
for (int dx = -radius; dx <= radius; ++dx)
{
for (int dy = -radius; dy <= radius; ++dy)
{
if (Math::Abs(dx)+Math::Abs(dy) < radius*1.5f)
game_ctx->world->SetTile(x+dx, y+dy, tile);
}
}
}
void CaveGameWindow::draw_debug_info(std::vector<std::string> tokens)
{
int font_size = 14;
@@ -185,22 +197,22 @@ namespace CaveGame::ClientApp
{
Vector2 transformed = game_ctx->world->camera.ScreenToWorld(GetMouseCoordinates());
game_ctx->world->SetTile(transformed.x, transformed.y, Core::TileID::AIR);
game_ctx->world->SetTile(transformed.x+1, transformed.y, Core::TileID::AIR);
game_ctx->world->SetTile(transformed.x-1, transformed.y, Core::TileID::AIR);
game_ctx->world->SetTile(transformed.x, transformed.y+1, Core::TileID::AIR);
game_ctx->world->SetTile(transformed.x, transformed.y-1, Core::TileID::AIR);
tile_draw(transformed.x, transformed.y, 4, Core::TileID::AIR);
}
if (IsMouseButtonDown(MouseButtons::Right))
{
Vector2 transformed = game_ctx->world->camera.ScreenToWorld(GetMouseCoordinates());
game_ctx->world->SetTile(transformed.x, transformed.y, Core::TileID::GRASS);
tile_draw(transformed.x, transformed.y, 2, Core::TileID::GRASS);
/*game_ctx->world->SetTile(transformed.x, transformed.y, Core::TileID::GRASS);
game_ctx->world->SetTile(transformed.x+1, transformed.y, Core::TileID::GRASS);
game_ctx->world->SetTile(transformed.x-1, transformed.y, Core::TileID::GRASS);
game_ctx->world->SetTile(transformed.x, transformed.y+1, Core::TileID::GRASS);
game_ctx->world->SetTile(transformed.x, transformed.y-1, Core::TileID::GRASS);
game_ctx->world->SetTile(transformed.x, transformed.y-1, Core::TileID::GRASS);*/
}
}

View File

@@ -107,13 +107,14 @@ namespace CaveGame::Core
class Tile;
void RegisterTile(Tile* data);
Tile* GetByNumeric(TileID id);
Tile* GetByName(const std::string& name);
/// Tiles are instantiated as static members in the Tiles namespace.
/// Chunks store tiles by their numeric ID, which can be used to retrieve the tile
class Tile
{
protected:
@@ -125,40 +126,29 @@ namespace CaveGame::Core
bool collides;
bool does_random_ticc;
bool has_color_pallet = false;
bool solid = true;
public:
Tile();
Tile(TileID id, const std::string& name, const Color4& color);
Tile(TileID id, const std::string& name, const Color4& color, const std::vector<Color4>& pallet);
[[nodiscard]] TileID NumericID() const;
[[nodiscard]] std::string MnemonicID() const;
virtual bool DoesRandomTicc() const { return false; }
virtual bool Solid() const { return false;}
virtual void ForcedTicc(ITileMap* world, TileState state, int x, int y) {}
virtual void RandomTicc(ITileMap* world, TileState state, int x, int y) {}
template <TileID TDecaysTo>
void DecayTo(ITileMap *world, TileState state, int x, int y)
{
DecayTo(world, state, x, y, TDecaysTo);
}
void DecayTo(ITileMap *world, TileState state, int x, int y, TileID TDecaysTo);
bool ShouldSpread(ITileMap* world, int x, int y, TileID spreads_to) const;
bool ShouldSuffocate(ITileMap* world, int x, int y) const;
bool DecayCheck(ITileMap* world, TileState state, int x, int y, TileID decays_to);
bool SpreadCheck(ITileMap* world, TileState state, int x, int y, TileID spreads_to);
virtual void DecayTo(ITileMap *world, TileState state, int x, int y, TileID TDecaysTo);
virtual bool ShouldSpread(ITileMap* world, int x, int y, TileID spreads_to) const;
virtual bool ShouldSuffocate(ITileMap* world, int x, int y) const;
virtual bool DecayCheck(ITileMap* world, TileState state, int x, int y, TileID decays_to);
virtual bool SpreadCheck(ITileMap* world, TileState state, int x, int y, TileID spreads_to) {}
};
class LiquidTile : public Tile
{
public:
bool Solid() const override { return true;}
};
class SoilTile : public Tile
{
@@ -166,69 +156,51 @@ namespace CaveGame::Core
TileID decays_to;
SoilTile();
SoilTile(TileID id, const std::string& name, const Color4& color, const std::vector<Color4>& pallet, TileID decays_target);
SoilTile(TileID numeric, const std::string& name, Color4 color, TileID decays_target);
bool DoesRandomTicc() const override { return true; }
void RandomTicc(ITileMap *world, TileState state, int x, int y) override;
bool SpreadCheck(ITileMap* world, TileState state, int x, int y, TileID spreads_to) override;
};
class GrassyTile : public SoilTile
{
class GrassyTile : public SoilTile {
public:
GrassyTile();
GrassyTile(TileID id, const std::string& name, const Color4& color, const std::vector<Color4>& pallet, TileID decays_target);
GrassyTile(TileID numeric, const std::string& name, Color4 color, TileID decays_target);
GrassyTile(TileID id, const std::string& name, const Color4& color, const std::vector<Color4>& pallet);
GrassyTile(TileID numeric, const std::string& name, Color4 color);
void RandomTicc(ITileMap *world, TileState state, int x, int y) override
{
if (DecayCheck(world, state, x, y, decays_to))
return;
if (SpreadCheck(world, state, x, y, decays_to))
return;
}
void RandomTicc(ITileMap *world, TileState state, int x, int y) override;
};
class MossyTile : public SoilTile {
public:
MossyTile() : SoilTile() {}
MossyTile(TileID id, const std::string& name, const Color4& color, TileID decays_target)
: SoilTile(id, name, color, decays_target) {}
MossyTile();
MossyTile(TileID id, const std::string& name, const Color4& color, TileID decays_target);
MossyTile(TileID numeric, const std::string &name, Color4 color, TileID decays_target);
MossyTile(TileID id, const std::string &name, const Color4 &color, const std::vector<Color4> &pallet);
MossyTile(TileID numeric, const std::string &name, Color4 color);
void RandomTicc(ITileMap *world, TileState state, int x, int y) override
{
if (DecayCheck(world, state, x, y, decays_to))
return;
if (SpreadCheck(world, state, x, y, decays_to))
return;
}
void RandomTicc(ITileMap *world, TileState state, int x, int y) override;
};
class VineTile : public Tile {
public:
VineTile() : Tile()
{}
VineTile();
VineTile(TileID id, const std::string& name, const Color4& color);
VineTile(TileID id, const std::string& name, const Color4& color, const std::vector<Color4>& pallet);
bool Solid() const override { return true;}
bool DoesRandomTicc() const override { return true; }
void ForcedTicc(ITileMap* world, TileState state, int x, int y) override;
void RandomTicc(ITileMap* world, TileState state, int x, int y) override;
bool DecayCheck(ITileMap* world, TileState state, int x, int y, TileID decays_to) override;
bool ShouldSpread(ITileMap* world, int x, int y, TileID spreads_to) const override;
bool SpreadCheck(ITileMap* world, TileState state, int x, int y, TileID spreads_to) override;
};
class GasTile : public Tile
@@ -264,7 +236,7 @@ namespace CaveGame::Core
{{126, 252, 5}, {124, 240, 0}, {124, 248, 8}}};
static const GrassyTile GlowyGrass {ID::GLOWY_GRASS, "Glowy Grass", Colors::Blues::PowderBlue};
static const VineTile Vine {ID::VINE, "Vine"};
static const VineTile Vine {ID::VINE, "Vine", Colors::Greens::ForestGreen};
static const Tile Sand {ID::SAND, "Sand", Colors::Yellows::PaleGoldenrod};
@@ -291,8 +263,6 @@ namespace CaveGame::Core
static const Tile Bamboo;
static const Tile Sugarcane;
static const Tile Vine;
static const Tile VineFlower;
static const Tile FlowerStem;
static const Tile RedFlowerPetal;
@@ -378,15 +348,15 @@ namespace CaveGame::Core
static const Tile BirchPlank;
static const Tile Water;
static const Tile Blood;
static const Tile Sludge;
static const Tile Lava;
static const Tile MuddyWater;
static const Tile Ectoplasm;
static const Tile Oil;
static const Tile Honey;
static const Tile Milk;
static const LiquidTile Water;
static const LiquidTile Blood;
static const LiquidTile Sludge;
static const LiquidTile Lava;
static const LiquidTile MuddyWater;
static const LiquidTile Ectoplasm;
static const LiquidTile Oil;
static const LiquidTile Honey;
static const LiquidTile Milk;
static const Tile Rope;

View File

@@ -4,7 +4,11 @@ namespace CaveGame::Core
{
bool ITileMap::IsNonSolidTile(int x, int y) const
{
return GetTile(x, y) == TileID::AIR;
TileID tile = GetTile(x, y);
if (tile == TileID::AIR || tile == TileID::VINE || tile == TileID::WATER)
return true;
return false;
}
bool ITileMap::IsSolidTile(int x, int y) const

View File

@@ -46,7 +46,7 @@ namespace CaveGame::Core
return false;
}
bool Tile::SpreadCheck(ITileMap *world, TileState state, int x, int y, TileID spreads_to) {
bool SoilTile::SpreadCheck(ITileMap *world, TileState state, int x, int y, TileID spreads_to) {
if (ShouldSpread(world, x, y+1, spreads_to)) {
world->SetTile(x, y+1, numeric_id);
return true;
@@ -108,12 +108,109 @@ namespace CaveGame::Core
GrassyTile::GrassyTile(TileID numeric, const std::string &name, Color4 color): SoilTile(numeric, name, color, TileID::DIRT) { }
void GrassyTile::RandomTicc(ITileMap* world, TileState state, int x, int y)
{
if (DecayCheck(world, state, x, y, decays_to))
return;
if (SpreadCheck(world, state, x, y, decays_to))
return;
if (world->GetTile(x, y+1) == TileID::AIR)
{
world->SetTile(x, y+1, TileID::VINE);
return;
}
}
MossyTile::MossyTile(): SoilTile()
{}
MossyTile::MossyTile(TileID id, const std::string& name, const Color4& color, TileID decays_target): SoilTile(id, name, color, decays_target)
{}
MossyTile::MossyTile(TileID numeric, const std::string &name, Color4 color, TileID decays_target): SoilTile(numeric, name, color, decays_target) { }
MossyTile::MossyTile(TileID id, const std::string &name, const Color4 &color, const std::vector<Color4> &pallet): SoilTile(id,name, color,pallet, TileID::STONE) { }
MossyTile::MossyTile(TileID numeric, const std::string &name, Color4 color): SoilTile(numeric, name, color, TileID::STONE) { }
void MossyTile::RandomTicc(ITileMap* world, TileState state, int x, int y)
{
if (DecayCheck(world, state, x, y, decays_to))
return;
if (SpreadCheck(world, state, x, y, decays_to))
return;
}
VineTile::VineTile(): Tile()
{
does_random_ticc = true;
}
VineTile::VineTile(TileID id, const std::string& name, const Color4& color): Tile(id, name, color)
{
does_random_ticc = true;
}
VineTile::VineTile(TileID id, const std::string& name, const Color4& color, const std::vector<Color4>& pallet):Tile(id, name, color, pallet)
{
does_random_ticc = true;
}
void VineTile::ForcedTicc(ITileMap* world, TileState state, int x, int y)
{
if (DecayCheck(world, state, x, y, TileID::AIR))
return;
}
void VineTile::RandomTicc(ITileMap* world, TileState state, int x, int y)
{
if (DecayCheck(world, state, x,y, TileID::AIR))
return;
if (SpreadCheck(world, state, x, y, TileID::AIR))
return;
}
bool VineTile::DecayCheck(ITileMap* world, TileState state, int x, int y, TileID decays_to)
{
TileID above = world->GetTile(x, y-1);
if (above != TileID::VINE && above != TileID::GRASS)
{
DecayTo(world, state, x, y, TileID::AIR);
return true;
}
return false;
}
bool VineTile::ShouldSpread(ITileMap* world, int x, int y, TileID spreads_to) const
{
if (world->GetTile(x, y) == spreads_to)
{
return true;
}
return false;
}
bool VineTile::SpreadCheck(ITileMap* world, TileState state, int x, int y, TileID spreads_to)
{
if (ShouldSpread(world, x, y + 1, spreads_to))
{
if (world->GetTile(x+1, y+1) == TileID::AIR && world->GetTile(x-1, y+1) == TileID::AIR)
{
world->SetTile(x, y+1, numeric_id);
return true;
}
}
return false;
}
void DefineTiles() {
/*RegisterTile(new Tile(TileID::AIR, "air", {0,0,0,0}));