do not allow characters to occupy same space as other characters
authorStan_Lewry <stanley.jml@gmail.com>
Fri, 30 Aug 2024 20:34:54 +0000 (21:34 +0100)
committerStan_Lewry <stanley.jml@gmail.com>
Fri, 30 Aug 2024 20:34:54 +0000 (21:34 +0100)
combat.cpp

index a69026f..6fc0f39 100644 (file)
@@ -27,7 +27,7 @@ struct Character {
        // how to handle "moves"
        S2DE::Vec2<float> combatMoveDestination = { 0.0f, 0.0f };
 
-       int basicAttackRange = 1.0f;
+       int basicAttackRange = 1.5f;
 
        bool canMove = true;
        bool canAttack = true;
@@ -278,6 +278,23 @@ void renderCombatants() {
        S2DE::renderTexture(&renderer, &indicatorSprite, NULL, &arrowDRect);
 }
 
+bool checkCharacterCollision(S2DE::Vec2<float> position) {
+       for (auto c : combatants) {
+               if (c.name != activeCombatant->name) { // this sucks
+                       float charMinX = c.position.x;
+                       float charMaxX = c.position.x + 1.0;
+                       float charMinY = c.position.y;
+                       float charMaxY = c.position.y + 1.0;
+
+                       if (position.x > charMinX && position.x < charMaxX
+                               && position.y > charMinY && position.y < charMaxY) {
+                               return true;
+                       }
+               }
+       }
+       return false;
+}
+
 void initCombatScene()
 {
        // I think all resources should be initialized and cleaned up in the globals tbh
@@ -479,11 +496,13 @@ void playerMoveActionState(double delta) {
                }
 
                if (distance({ moveCursorPos.x, newY }, player.position) <= player.moveDist
-                       && checkCollisionPoint({ moveCursorPos.x + 0.5f, newY + 0.5f }, arena) == false) {
+                       && checkCollisionPoint({ moveCursorPos.x + 0.5f, newY + 0.5f }, arena) == false
+                       && checkCharacterCollision({moveCursorPos.x + 0.5f, newY + 0.5f}) == false) {
                        moveCursorPos.y = newY;
                }
                if (distance({ newX, moveCursorPos.y }, player.position) <= player.moveDist
-                       && checkCollisionPoint({ newX + 0.5f, moveCursorPos.y + 0.5f }, arena) == false) {
+                       && checkCollisionPoint({ newX + 0.5f, moveCursorPos.y + 0.5f }, arena) == false
+                       && checkCharacterCollision({ newX + 0.5f, moveCursorPos.y + 0.5f }) == false) {
                        moveCursorPos.x = newX;
                }