aboutsummaryrefslogtreecommitdiff
path: root/src/gameScreen.c
blob: ba5981a2763b1427cb12038232f6f7de56674d3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "gameScreen.h"
#include "game.h"
#include "world.h"
#include "bullets.h"
#include "assets.h"

void initGameScreenGui(GameScreen * gameScreen) {
	float width = GetScreenWidth();
	float height = GetScreenHeight();

	// It is kind of terrible but works.
	gameScreen->infoText = (Vector2){0.0, height / 1.5};

	// Gyroscope indeed
	initGyroscope(&gameScreen->gyroscope);

	// Radar indeed.
	initRadar(&gameScreen->radar);

	gameScreen->mainCamera = FIRST_PERSON_CAMERA;
}

void initGameScreen(GameScreen * gameScreen) {
	initGameScreenGui(gameScreen);
}

void freeGameScreen(GameScreen * gameScreen) {
	closeGyroscope(&gameScreen->gyroscope);
	closeRadar(&gameScreen->radar);
}

void drawCrossHair(float size, float thick, Color color) {
	Vector3 center = (Vector3){GetScreenWidth() / 2.0, GetScreenHeight() / 2.0};

	// Left to right.
	DrawLineEx(
		(Vector2){center.x - size, center.y},
		(Vector2){center.x + size, center.y},
		thick,
		color
	);

	// Top to bottom.
	DrawLineEx(
		(Vector2){center.x, center.y - size},
		(Vector2){center.x, center.y + size},
		thick,
		color
	);
}

void drawGameScreenGui(Game * game) {
	GameScreen * gameScreen = &game->gameScreen;
	Entity * player = getEntityFromWorld(game->world, 0);

	// Hello reader. I fucking hate you!
	size_t bufSize = 255;
	char buf[bufSize];

	// Draw cross hair.
	if (gameScreen->mainCamera == FIRST_PERSON_CAMERA)
		drawCrossHair(10.0, 2.0, BLUE);

	Vector3 position = player->position;
	Vector3 velocity = player->velocity.velocity;

	// Format text.
	snprintf(
		buf,
		bufSize,
		"Health %.2f\n\nX %.2f\nY %.2f\nZ %.2f\n\nSpeed %.2f",
		player->health,
		position.x,
		position.y,
		position.z,
		Vector3Length(velocity)
	);

	// Draw info text.
	DrawText(
		buf,
		gameScreen->infoText.x,
		gameScreen->infoText.y,
		GAME_SCREEN_TEXT_SIZE,
		GREEN
	);

	drawGyroscope(game, &gameScreen->gyroscope);
	drawRadar(game, &gameScreen->radar);
}

void handleGameScreenInput(Game * game, GameScreen * gameScreen) {
	switch(GetKeyPressed()) {
		case KEY_ONE:
			gameScreen->mainCamera = FIRST_PERSON_CAMERA;
			break;
		case KEY_TWO:
			gameScreen->mainCamera = THIRD_PERSON_CAMERA;
			break;
		case KEY_THREE:
			gameScreen->mainCamera = DEBUG_CAMERA;
			break;
		default:
			break;
	}
}

void updateGameScreen(Game * game) {
	GameScreen * gameScreen = &game->gameScreen;

	handleGameScreenInput(game, gameScreen);

	ClearBackground(BLACK);

	drawGameScreenGui(game);

	// Update world.
	updateWorld(&game->world, game);

	// Camera.
	runCameraUpdate(game, game->cameras, gameScreen->mainCamera);

	// Draw.
	BeginMode3D(game->cameras[gameScreen->mainCamera]);

	DrawGrid(50, 25.0);

	// Draw world.
	drawWorld(&game->world, game);

	EndMode3D();
}

void openGameScreen(Game * game) {
	game->screenId = SCREEN_GAME;

	if (game->settings.lockMouse)
		DisableCursor();
}

void closeGameScreen(Game * game) {
	if (game->settings.lockMouse)
		EnableCursor();
}