]> git.lizzy.rs Git - shadowclad.git/blob - src/game/player.c
Preparation for player obstacle detection
[shadowclad.git] / src / game / player.c
1 #include "player.h"
2
3 #include "engine/asset.h"
4 #include "engine/render.h"
5
6 #include "level.h"
7
8 static const float movementSpeed = 2.5f;
9
10 Scene* playerCharacter;
11 static Transform screenToWorldMovementTransform;
12 static Vector worldMovementUp;
13 static Vector worldMovementDown;
14 static Vector worldMovementLeft;
15 static Vector worldMovementRight;
16 static Direction movementDirection;
17
18 static void movePlayer(Vector direction, float delta);
19 static Vector worldMovementDirection(float x, float y);
20
21
22
23 void initPlayer() {
24         screenToWorldMovementTransform = identity();
25         rotate(&screenToWorldMovementTransform, (Vector) { 0.0f, 1.0f, 0.0f }, - TAU / 8.0f);
26
27         worldMovementUp = worldMovementDirection(0.0f, 1.0f);
28         worldMovementDown = worldMovementDirection(0.0f, -1.0f);
29         worldMovementLeft = worldMovementDirection(-1.0f, 0.0f);
30         worldMovementRight = worldMovementDirection(1.0f, 0.0f);
31
32         playerCharacter = newScene();
33         cameraAnchor = playerCharacter;
34         playerCharacter->solid = importSolid("assets/playercharacter.3ds");
35 }
36
37 void spawnPlayer(Transform transform) {
38         playerCharacter->transform = transform;
39         reparentScene(playerCharacter, currentScene);
40 }
41
42 void updatePlayer(float delta) {
43         Vector direction = { 0.0f, 0.0f, 0.0f };
44         if (movementDirection & DIRECTION_UP) {
45                 direction = addVectors(direction, worldMovementUp);
46         }
47         if (movementDirection & DIRECTION_DOWN) {
48                 direction = addVectors(direction, worldMovementDown);
49         }
50         if (movementDirection & DIRECTION_LEFT) {
51                 direction = addVectors(direction, worldMovementLeft);
52         }
53         if (movementDirection & DIRECTION_RIGHT) {
54                 direction = addVectors(direction, worldMovementRight);
55         }
56         movePlayer(direction, delta);
57 }
58
59 void startMovement(Direction direction) {
60         movementDirection |= direction;
61 }
62
63 void stopMovement(Direction direction) {
64         movementDirection &= ~direction;
65 }
66
67 static void movePlayer(Vector direction, float delta) {
68         direction = clampMagnitude(direction, 1.0f);
69         Vector displacement = scaleVector(direction, delta * movementSpeed);
70
71
72         //GridLocation location = gridLocationFromTransform(playerCharacter->transform);
73
74         if (displacement.x >= 0) {
75                 // need to test +X edge
76         }
77         if (displacement.x <= 0) {
78                 // need to test -X edge
79         }
80         if (displacement.z >= 0) {
81                 // need to test +Z edge
82         }
83         if (displacement.z <= 0) {
84                 // need to test -Z edge
85         }
86
87
88         translate(&playerCharacter->transform, displacement);
89 }
90
91 static Vector worldMovementDirection(float x, float y) {
92         Vector direction = (Vector) { x, 0.0f, -y };
93         direction = normalized(
94                 applyTransform(screenToWorldMovementTransform, direction));
95         return direction;
96 }