3 #include "engine/asset.h"
4 #include "engine/render.h"
8 static const float movementSpeed = 2.5f;
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;
18 static void movePlayer(Vector direction, float delta);
19 static Vector worldMovementDirection(float x, float y);
24 screenToWorldMovementTransform = identity();
25 rotate(&screenToWorldMovementTransform, (Vector) { 0.0f, 1.0f, 0.0f }, - TAU / 8.0f);
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);
32 playerCharacter = newScene();
33 cameraAnchor = playerCharacter;
34 playerCharacter->solid = importSolid("assets/playercharacter.3ds");
37 void spawnPlayer(Transform transform) {
38 playerCharacter->transform = transform;
39 reparentScene(playerCharacter, currentScene);
42 void updatePlayer(float delta) {
43 Vector direction = { 0.0f, 0.0f, 0.0f };
44 if (movementDirection & DIRECTION_UP) {
45 direction = addVectors(direction, worldMovementUp);
47 if (movementDirection & DIRECTION_DOWN) {
48 direction = addVectors(direction, worldMovementDown);
50 if (movementDirection & DIRECTION_LEFT) {
51 direction = addVectors(direction, worldMovementLeft);
53 if (movementDirection & DIRECTION_RIGHT) {
54 direction = addVectors(direction, worldMovementRight);
56 movePlayer(direction, delta);
59 void startMovement(Direction direction) {
60 movementDirection |= direction;
63 void stopMovement(Direction direction) {
64 movementDirection &= ~direction;
67 static void movePlayer(Vector direction, float delta) {
68 direction = clampMagnitude(direction, 1.0f);
69 Vector displacement = scaleVector(direction, delta * movementSpeed);
72 //GridLocation location = gridLocationFromTransform(playerCharacter->transform);
74 if (displacement.x >= 0) {
75 // need to test +X edge
77 if (displacement.x <= 0) {
78 // need to test -X edge
80 if (displacement.z >= 0) {
81 // need to test +Z edge
83 if (displacement.z <= 0) {
84 // need to test -Z edge
88 translate(&playerCharacter->transform, displacement);
91 static Vector worldMovementDirection(float x, float y) {
92 Vector direction = (Vector) { x, 0.0f, -y };
93 direction = normalized(
94 applyTransform(screenToWorldMovementTransform, direction));