]> git.lizzy.rs Git - shadowclad.git/commitdiff
Preparation for player obstacle detection
authoroutfrost <kotlet.bahn@gmail.com>
Wed, 15 Jul 2020 03:13:58 +0000 (05:13 +0200)
committeroutfrost <kotlet.bahn@gmail.com>
Wed, 15 Jul 2020 03:13:58 +0000 (05:13 +0200)
src/game/level.c
src/game/level.h
src/game/player.c

index 0544ba8d8722635258254df52b17768851c7c3e0..c814e4e48588f68b946712c5a035e08c5246f057 100644 (file)
@@ -21,16 +21,14 @@ static Block blockWall01 = { .type = BLOCKTYPE_OBSTACLE,
 static Transform playerSpawnTransform;
 
 static void buildLevelFromImage(const TgaImage* image);
-static inline Block* getBlockFromGrid(BlockGrid grid, size_t x, size_t z);
-static inline void setBlockInGrid(BlockGrid grid, size_t x, size_t z, Block* block);
 
 
 
 void initLevel() {
        playerSpawnTransform = identity();
-       translate(&playerSpawnTransform, (Vector) { .x = -BLOCKGRID_CELL_SIZE,
-                                                   .y = 0.0f,
-                                                   .z = -BLOCKGRID_CELL_SIZE });
+       translate(&playerSpawnTransform, (Vector) { -BLOCKGRID_CELL_SIZE,
+                                                   0.0f,
+                                                   -BLOCKGRID_CELL_SIZE });
 
        blockWall01.solid = importSolid("assets/wall01.3ds");
 
@@ -41,9 +39,10 @@ void initLevel() {
        for (size_t z = 0; z < levelGrid.depth; ++z) {
                for (size_t x = 0; x < levelGrid.width; ++x) {
                        Scene* blockScene = newScene();
-                       translate(&blockScene->transform, (Vector) { .x = x * BLOCKGRID_CELL_SIZE,
-                                                                    .y = 0.0f,
-                                                                    .z = z * BLOCKGRID_CELL_SIZE });
+                       translate(&blockScene->transform, (Vector) {
+                               (x * BLOCKGRID_CELL_SIZE) + (BLOCKGRID_CELL_SIZE * 0.5f),
+                               0.0f,
+                               (z * BLOCKGRID_CELL_SIZE) + (BLOCKGRID_CELL_SIZE * 0.5f) });
                        blockScene->solid = getBlockFromGrid(levelGrid, x, z)->solid;
                        insertChildScene(levelScene, blockScene);
                }
@@ -87,9 +86,10 @@ static void buildLevelFromImage(const TgaImage* image) {
                                case 0xFF00FFFF:
                                        block = &blockEmpty;
                                        playerSpawnTransform = identity();
-                                       translate(&playerSpawnTransform, (Vector) { .x = x * BLOCKGRID_CELL_SIZE,
-                                                                                   .y = 0.0f,
-                                                                                   .z = z * BLOCKGRID_CELL_SIZE });
+                                       translate(&playerSpawnTransform, (Vector) {
+                                               (x * BLOCKGRID_CELL_SIZE) + (BLOCKGRID_CELL_SIZE * 0.5f),
+                                               0.0f,
+                                               (z * BLOCKGRID_CELL_SIZE) + (BLOCKGRID_CELL_SIZE * 0.5f) });
                                        break;
                                default:
                                        block = &blockEmpty;
@@ -102,10 +102,12 @@ static void buildLevelFromImage(const TgaImage* image) {
        levelGrid = newGrid;
 }
 
-static inline Block* getBlockFromGrid(BlockGrid grid, size_t x, size_t z) {
-       return grid.blocks[(z * grid.width) + x];
+static inline size_t nonNegative(long n) {
+       return n < 0 ? 0u : n;
 }
 
-static inline void setBlockInGrid(BlockGrid grid, size_t x, size_t z, Block* block) {
-       grid.blocks[(z * grid.width) + x] = block;
+GridLocation gridLocationFromTransform(Transform transform) {
+       Vector scaledPos = scaleVector(translationOf(transform), 1.0f / BLOCKGRID_CELL_SIZE);
+       return (GridLocation) { .x = nonNegative(scaledPos.x),
+                               .z = nonNegative(scaledPos.z) };
 }
index 097b73acdda82744d0ae81ef70f4d1f43b384292..3ce8241f8a85b33e060ffa63182bd716c631e589 100644 (file)
@@ -15,6 +15,7 @@ enum BlockType {
 typedef enum BlockType BlockType;
 typedef struct Block Block;
 typedef struct BlockGrid BlockGrid;
+typedef struct GridLocation GridLocation;
 
 struct Block {
        BlockType type;
@@ -27,9 +28,23 @@ struct BlockGrid {
        Block** blocks;
 };
 
+struct GridLocation {
+       size_t x;
+       size_t z;
+};
+
 extern BlockGrid levelGrid;
 
 void initLevel();
 void startLevel();
+GridLocation gridLocationFromTransform(Transform transform);
+
+static inline Block* getBlockFromGrid(BlockGrid grid, size_t x, size_t z) {
+       return grid.blocks[(z * grid.width) + x];
+}
+
+static inline void setBlockInGrid(BlockGrid grid, size_t x, size_t z, Block* block) {
+       grid.blocks[(z * grid.width) + x] = block;
+}
 
 #endif // LEVEL_H_
index 432341c6aece912859747e44b25462001d9ad7e6..f5dcac79e84aae2a2889fff0634b8e767ea316c8 100644 (file)
@@ -3,6 +3,8 @@
 #include "engine/asset.h"
 #include "engine/render.h"
 
+#include "level.h"
+
 static const float movementSpeed = 2.5f;
 
 Scene* playerCharacter;
@@ -65,6 +67,24 @@ void stopMovement(Direction direction) {
 static void movePlayer(Vector direction, float delta) {
        direction = clampMagnitude(direction, 1.0f);
        Vector displacement = scaleVector(direction, delta * movementSpeed);
+
+
+       //GridLocation location = gridLocationFromTransform(playerCharacter->transform);
+
+       if (displacement.x >= 0) {
+               // need to test +X edge
+       }
+       if (displacement.x <= 0) {
+               // need to test -X edge
+       }
+       if (displacement.z >= 0) {
+               // need to test +Z edge
+       }
+       if (displacement.z <= 0) {
+               // need to test -Z edge
+       }
+
+
        translate(&playerCharacter->transform, displacement);
 }