-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.c
189 lines (164 loc) · 5.71 KB
/
input.c
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include "input.h"
#include "Structs/World.h"
#include "Structs/Circle.h"
#include "Structs/Vec2.h"
#include "graphics.h"
#include "raylib.h"
#include "Structs/PlayerScore.h"
#include <stdio.h>
#include <string.h>
// Updates the input according to the game state
void updateInput(World* w, float delta) {
switch (w->state) {
case PLAYING:
updateInputPlaying(w, delta);
break;
case GAME_OVER:
updateInputGameOver(w, delta);
break;
case MAIN_MENU:
updateInputMainMenu(w, delta);
break;
case PAUSE_MENU:
updateInputPauseMenu(w, delta);
break;
case ASK_NAME:
updateAskName(w, delta);
case HIGH_SCORE_SCREEN:
updateHighScoreScreen(w, delta);
break;
}
}
// Moves the player to chase the cursor
void movePlayer(World* w, float delta) {
// Get the mouse position
Vec2 mousePos = vector2ToVec2(GetScreenToWorld2D(GetMousePosition(), getCam()));
// Calculate the angle between the player and the mouse
float angle = angleBetween(w->player.position, mousePos);
// Transform the angle into a X and Y motion and multiply it by delta
Vec2 movement = getMovementByAngle(angle, getCircleSpeed(w->player));
movement = scaleVec2(movement, delta);
// Apply the movement to the player
moveCircle(&w->player, movement);
}
// Converts between raylib Vectors (Vector2) and Hr.io Vectors (Vec2)
Vec2 vector2ToVec2(Vector2 v) {
return (Vec2) {
v.x,
v.y
};
}
// Update the input on the PLAYING game state
void updateInputPlaying(World *w, float delta) {
// Move the player
movePlayer(w, delta);
// Check for pause
if (IsKeyPressed(KEY_ESCAPE)) {
w->state = PAUSE_MENU;
}
}
// Update the input on the GAME_OVER state
void updateInputGameOver(World* w, float delta) {
// If the user is clicking
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) {
// Check if the restart button was pressed
if (pointInRect(GAME_OVER_BUTTON, GetMousePosition())){
*w = newWorld(w);
w->state = PLAYING;
// Check if the exit button was pressed
} else if (pointInRect(GAME_OVER_MENU_BUTTON, GetMousePosition())) {
w->state = MAIN_MENU;
}
}
}
// Tells if a point is inside a rectangle
int pointInRect(Rectangle r, Vector2 v) {
return v.x < r.width + r.x &&
v.x > r.x &&
v.y < r.y + r.height &&
v.y > r.y;
}
// Updates the input for the MAIN_MENU state
void updateInputMainMenu(World* w, float delta) {
// If the user is clicking...
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) {
// Check if the start button was clicked
if (pointInRect(MAIN_MENU_START_BUTTON, GetMousePosition())){
// Start a new world, and carry no information from a previous world
*w = newWorld(NULL);
w->state = ASK_NAME;
// If the load button was clicked
} else if (pointInRect(MAIN_MENU_LOAD_BUTTON, GetMousePosition())) {
// If loading was successful, start playing
if (loadWorld(w)) {
w->state = PLAYING;
}
// If the highscore button was clicked, go to the highscore screen
} else if (pointInRect(MAIN_MENU_HISCORE_BUTTON, GetMousePosition())) {
w->state = HIGH_SCORE_SCREEN;
// If the exit button was clicked, tell the game to quit
} else if (pointInRect(MAIN_MENU_EXIT_BUTTON, GetMousePosition())) {
setIsRunning(0);
}
}
}
// Update the input for the pause menu
void updateInputPauseMenu(World* w, float delta) {
// Go back to playing if the ESC key was pressed
if (IsKeyPressed(KEY_ESCAPE)) {
w->state = PLAYING;
}
// If the user is clicking...
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) {
// Check if the resume button was clicked
if (pointInRect(PAUSE_MENU_RESUME_BUTTON, GetMousePosition())) {
w->state = PLAYING;
}
// Check if the save button was clicked
if (pointInRect(PAUSE_MENU_SAVE_BUTTON, GetMousePosition())) {
saveWorld(*w);
}
// Check if the exit button was clicked
if (pointInRect(PAUSE_MENU_EXIT_BUTTON, GetMousePosition())) {
w->state = MAIN_MENU;
}
}
}
// Update the input for the ASK_NAME state
void updateAskName(World* w, float delta) {
// Get the keu the user is pressing
int key = GetKeyPressed();
// User to treat a char as a string of length 1
char tmp[2] = {0};
if (key > 0) {
// If the key is a allowed character...
if ((key >= '0' && key <= '9') || (key >= 'A' && key <= 'z') || key == ' ') {
// "Convert" it to a string
tmp[0] = (char)key;
// Concatenate it to the player's name
strcat(w->player.name, tmp);
}
}
// Check for the backspace key
if (IsKeyPressed(KEY_BACKSPACE)) {
// Get the index for the last char
int index = strlen(w->player.name) - 1;
// If the index is valid, erase that char
if (index >= 0) {
w->player.name[index] = '\0';
}
}
// If the user got the mouse over the confirmation button...
if (pointInRect(ASK_NAME_CONFIRM_BUTTON, GetMousePosition())) {
// If the user clicked the button and typed a name, start the game
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON) && strlen(w->player.name) > 0) {
w->state = PLAYING;
}
}
}
// Update the input for the HIGH_SCORE_SCREEN state
void updateHighScoreScreen(World* w, float delta) {
if (pointInRect(HIGH_SCORE_BACK_BUTTON, GetMousePosition()) && IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) {
w->state = MAIN_MENU;
}
}