Skip to content
This repository has been archived by the owner on Dec 2, 2024. It is now read-only.

Commit

Permalink
Next step done; random movement
Browse files Browse the repository at this point in the history
  • Loading branch information
LunarWatcher committed Nov 7, 2023
1 parent 8050efb commit ea2da9b
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 84 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.24)
project(Genesis)

if ( ${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR} )
if (${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
message(FATAL_ERROR "In-source builds not allowed. Please make a new directory and run CMake from there, or use the -B argument.")
endif()

Expand Down
1 change: 0 additions & 1 deletion src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ set (SOURCE_FILES

# Environment {{{
genesis/core/game/chunks/ZLayer.cpp
genesis/core/game/chunks/MapArea.cpp
genesis/core/game/chunks/Tile.cpp

genesis/core/game/chunks/TileGenerator.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/core/genesis/core/game/World.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class World : public Scene {
// Game state
std::string civilisationName;

MapLayers layers;
//std::vector<std::shared_ptr<GameCreature>> gameEntities;

/**
Expand All @@ -48,6 +47,7 @@ class World : public Scene {
}
public:
CreatureController creatures;
MapLayers layers;

World(int width, int height);

Expand Down
8 changes: 7 additions & 1 deletion src/core/genesis/core/game/ai/AIEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "CreatureController.hpp"
#include "genesis/Context.hpp"
#include "genesis/core/game/World.hpp"
#include "genesis/core/game/generation/RNGesus.hpp"

namespace genesis {

Expand All @@ -12,7 +13,12 @@ void AIEngine::simulateColony(CreatureController& cc, World& world) {
for (auto& [groupId, group] : entities) {
if (groupId == 0) {
for (auto& entity : group.getEntities()) {
entity->getPosition().x += 1;
auto newPosition = entity->getPosition();
newPosition.x += util::randInt(-1, 1);
newPosition.y += util::randInt(-1, 1);
if (world.layers.canMove(entity->getPosition(), newPosition)) {
entity->setPosition(newPosition);
}
}
}
}
Expand Down
36 changes: 0 additions & 36 deletions src/core/genesis/core/game/chunks/MapArea.cpp

This file was deleted.

37 changes: 0 additions & 37 deletions src/core/genesis/core/game/chunks/MapArea.hpp

This file was deleted.

27 changes: 27 additions & 0 deletions src/core/genesis/core/game/chunks/MapLayers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,31 @@ void MapLayers::regenerateLayerGraphics() {
glBindVertexArray(0);
}

bool MapLayers::canMove(const glm::vec3& from, const glm::vec3& to) {
// This function disregards there being multiple z layers for now, as it's convenient to do so
// This needs to be addressed when the game has multiple layers

// Trivial; if the x or y exceeds the map size, discard
if (to.x < 0 || to.x >= width
|| to.y < 0 || to.y >= height) {
return false;
}

// TODO: error checking
auto layer = layers.at(from.z);

auto tile = layer.layer.at(to.x).at(to.y);

// Again, this is a very primitive check, but I need to do this iteratively if I want to
// get anywhere with this game
return
// Ensure there isn't a block in the way
tile.block == nullptr
// and ensure the floor exists. can't jump into a void. Whether or not it's valid to go up
// or down a missing floor requires more of the game to be implemented.
&& tile.floor != nullptr;


}

}
2 changes: 2 additions & 0 deletions src/core/genesis/core/game/chunks/MapLayers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class MapLayers : public Entity {
// TODO: this is probably going to have to act as the interface for the contained ZLayers to update specific tiles.
// glSubData will at least increase the performance of on-layer updates.

bool canMove(const glm::vec3& from, const glm::vec3& to);

friend class WorldGenerator;
};

Expand Down
4 changes: 4 additions & 0 deletions src/core/genesis/core/game/generation/RNGesus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,8 @@ std::shared_ptr<GameCreature> RNGesus::genCreature(
}
}

int util::randInt(int min, int max) {
return std::uniform_int_distribution<>(min, max)(_random::engine);
}

}
7 changes: 7 additions & 0 deletions src/core/genesis/core/game/generation/RNGesus.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ extern std::shared_ptr<GameCreature> genCreature(
std::shared_ptr<SpeciesInfo> overrideSpecies
);

}

namespace util {
// I'm sure this will backfire wonderfully some day, and I look forward to future me roasting me for being stupid :)
using namespace RNGesus;

extern int randInt(int min, int max);

}

Expand Down
2 changes: 0 additions & 2 deletions src/core/genesis/logging/Loggers.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "genesis/core/data/DataLoader.hpp"
#include "genesis/rendering/Renderer.hpp"
#include "genesis/core/game/chunks/MapArea.hpp"

#include "spdlog/common.h"
#include "spdlog/logger.h"
Expand Down Expand Up @@ -28,7 +27,6 @@ static std::vector<spdlog::sink_ptr> sinks = {
};

std::shared_ptr<spdlog::logger> Renderer::logger = std::make_shared<spdlog::logger>("renderer", sinks.begin(), sinks.end());
std::shared_ptr<spdlog::logger> MapArea::logger = std::make_shared<spdlog::logger>("map", sinks.begin(), sinks.end());
std::shared_ptr<spdlog::logger> DataLoader::logger = std::make_shared<spdlog::logger>("DataLoader", sinks.begin(), sinks.end());

}
10 changes: 5 additions & 5 deletions src/render-engine/genesis/rendering/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ void Renderer::render() {
}

void Renderer::run() {
auto targetTime = std::chrono::duration<double, std::milli>(1000.0 / 120.0);
//auto targetTime = std::chrono::duration<double, std::milli>(1000.0 / 120.0);

auto lastTime = std::chrono::high_resolution_clock::now();
auto counter = lastTime;
Expand All @@ -162,10 +162,10 @@ void Renderer::run() {

auto end = std::chrono::high_resolution_clock::now();
lastTime = now;
auto sleepFor = targetTime - (now - end);
if (sleepFor > std::chrono::milliseconds(0)) {
std::this_thread::sleep_for(sleepFor);
}
//auto sleepFor = targetTime - (now - end);
//if (sleepFor > std::chrono::milliseconds(0)) {
//std::this_thread::sleep_for(sleepFor);
//}
++frames;
if (std::chrono::high_resolution_clock::now() - counter > std::chrono::seconds(1)) {
counter = std::chrono::high_resolution_clock::now();
Expand Down

0 comments on commit ea2da9b

Please sign in to comment.