From 186bbdb014f4d812ab656b409c672e3316d0deff Mon Sep 17 00:00:00 2001 From: Stan_Lewry Date: Tue, 1 Oct 2024 17:04:21 +0100 Subject: [PATCH] first basic attempt at transitioning from overworld to combat --- combat.cpp | 13 +++++++---- combat.h | 12 +++++++++- main.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ main_menu.cpp | 2 ++ 4 files changed, 83 insertions(+), 5 deletions(-) diff --git a/combat.cpp b/combat.cpp index d91be2d..68e4263 100644 --- a/combat.cpp +++ b/combat.cpp @@ -58,8 +58,8 @@ S2DE::Camera combatCam = { 23.5, 11.5 }; S2DE::Vec2 moveCursorPos; S2DE::Vec2 arenaCenter = { 23, 11 }; -static const int numActions = 4; -static const char* actions[numActions] = {"Move", "Attack", "End", "FLEE"}; +static const int numActions = 5; +static const char* actions[numActions] = {"Move", "Attack", "Abilities", "End", "FLEE"}; int currentAction = 0; SDL_Colour hpColour = { 136, 8, 8, 255 }; @@ -344,8 +344,11 @@ void initCombatScene() /*combatants.push_back(enemy); combatants.push_back(frog);*/ +} +void beginCombat() +{ Character dummy; dummy.allied = false; dummy.name = "Practice Dummy"; @@ -362,7 +365,7 @@ void initCombatScene() combatants.push_back(*eric); // push back copies of the players? combatants.push_back(*monica); - + // calculates turn order. Can expand the lambda to support other things std::sort(combatants.begin(), combatants.end(), [](const Character& a, const Character& b) {return a.agility > b.agility; }); @@ -450,7 +453,9 @@ void actionSelectState(double delta) { currentCombatState = SELECT_TARGET; } break; - case 2: // end selected + case 2: // transition to select abilities state + break; + case 3: // end selected currentCombatState = TURN_ORDER; break; diff --git a/combat.h b/combat.h index 613bd3e..6f3a723 100644 --- a/combat.h +++ b/combat.h @@ -1,5 +1,15 @@ #pragma once -void initCombatScene(); +void initCombatScene(); // call this at program start + + +/* +Call this before transitioning into the combat state. +Should setup the vector of combatants. +Eventually this will have params allowing the combatants +and the combat arena to be set externally. +*/ +void beginCombat(); // call this before transitioning into the combat state + void runCombat(double deltaTime); \ No newline at end of file diff --git a/main.cpp b/main.cpp index 777221a..9ac80ea 100644 --- a/main.cpp +++ b/main.cpp @@ -43,6 +43,54 @@ void renderWorld(); //bool checkCollision(S2DE::Vec2 worldPos); +/* +This structure defines any entity that is not a tile that can be drawn on the overworld. +Will need a function to detect collisions with these, although will assume all the same size. +Will also need some way to mark them as "dead" or innactive. The hostile flag determines if collision +with one initiates combat or not. +*/ +struct OverworldEntity { + bool hostile; + S2DE::Vec2 position; + SDL_Texture* texture; +}; + +std::vector overworldEntities; + +void initOverworldEntities(); +/* +This function checks collisions against all overworld entities. +Currently returns true if a collision is found. This needs to be changed though depending on what the player collides with. +*/ +bool checkCollisionOverworldEntities(); + + +void initOverworldEntities() { + overworldEntities.push_back({ true, {29, 22}, practice_dummy_texture }); +} + +bool checkCollisionOverworldEntities() { + + // assumes the enemy has size 1x1; + + for (auto oe : overworldEntities) { + + float minX = playerPos.x; + float minY = playerPos.y; + float maxX = playerPos.x + 1.0f; + float maxy = playerPos.y + 1.0f; + float width = 1.0f; + float height = 1.0f; + if ((playerPos.x > oe.position.x && playerPos.x < oe.position.x + width) + && (playerPos.y > oe.position.y && playerPos.y < oe.position.y + height)){ + return true; + } + + } + + return false; +} + void renderWorld() { @@ -53,6 +101,12 @@ void renderWorld() S2DE::Rect enemySRect = { 0, 32 * enemyDirection, 32, 32 }; S2DE::renderTexture(&renderer, &bird, &enemySRect, &enemyDRect); + // render overworld Entities + for (auto oe : overworldEntities) { + S2DE::Rect oeDRect = S2DE::worldToScreenRect(&camera, &oe.position, 2, WINDOW_WIDTH, WINDOW_HEIGHT, 32, 32); + S2DE::Rect oeSRect = { 0, 0, 32, 32 }; + S2DE::renderTexture(&renderer, &oe.texture, &oeSRect, &oeDRect); + } // render player @@ -74,6 +128,8 @@ int main(int argc, char** args) for (int i = 0; i < WORLD_HEIGHT; ++i) world[i] = new WorldCell[WORLD_WIDTH]; loadWorld("maps/overworld_1.tmj", world, WORLD_WIDTH, WORLD_HEIGHT); + + initOverworldEntities(); bool running = true; @@ -161,6 +217,11 @@ int main(int argc, char** args) camera = playerPos; + if (checkCollisionOverworldEntities()) { + beginCombat(); + gameState = COMBAT; + } + if (enemyWalkTimer > enemyWalkTime) { // flip the direction and reset the timer diff --git a/main_menu.cpp b/main_menu.cpp index b821faf..8dbacf5 100644 --- a/main_menu.cpp +++ b/main_menu.cpp @@ -1,6 +1,7 @@ #include "main_menu.h" #include "globals.h" #include "ui.h" +#include "combat.h" static const char* menuItems[] = { "Overworld", "Combat", "Load Game", "Options", "Quit" }; static constexpr int numMenuItems = 5; @@ -52,6 +53,7 @@ void mainMenu(double deltaTime) { gameState = OVERWORLD; break; case 1: + beginCombat(); gameState = COMBAT; break; default: -- 2.20.1