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