1 | /*********************************************************************************** |
2 | * |
3 | * KING GAME JAM - GRAY TEAM |
4 | * |
5 | * <Game title> |
6 | * <Game description> |
7 | * |
8 | * This game has been created using raylib (www.raylib.com) |
9 | * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) |
10 | * |
11 | * Copyright (c) 2014 Ramon Santamaria (@raysan5) |
12 | * |
13 | ************************************************************************************/ |
14 | |
15 | #include "raylib.h" |
16 | #include "player.h" |
17 | |
18 | #define PLAYER_ANIM_FRAMES 4 |
19 | #define PLAYER_ANIM_SEQ 6 |
20 | |
21 | //---------------------------------------------------------------------------------- |
22 | // Module Variables Definition |
23 | //---------------------------------------------------------------------------------- |
24 | |
25 | // Player mouse moving variables |
26 | static bool movingAnim; |
27 | static int moveDirection; |
28 | static int nextMovePoint; |
29 | |
30 | // Mouse pointer variables |
31 | static Vector2 pointerPosition; |
32 | static bool pointerAnim; |
33 | static float pointerAlpha; |
34 | |
35 | static int framesCounter; |
36 | static bool outControl = false; |
37 | |
38 | static int animTimer = 0; |
39 | |
40 | static Texture2D texLife; |
41 | |
42 | static void DrawLifes(void); |
43 | |
44 | // player initialitaction definition |
45 | void InitPlayer(void) |
46 | { |
47 | // NOTE: Some player variables are only initialized once |
48 | player.texture = LoadTexture("resources/textures/skully.png" ); |
49 | player.position = (Vector2){ 350, 400 }; |
50 | player.numLifes = 4; |
51 | |
52 | ResetPlayer(); |
53 | |
54 | framesCounter = 0; |
55 | |
56 | texLife = LoadTexture("resources/textures/skully_icon.png" ); |
57 | } |
58 | |
59 | // player update definition |
60 | void UpdatePlayer(void) |
61 | { |
62 | if (!outControl) |
63 | { |
64 | if ((IsKeyDown(KEY_LEFT)) || (IsKeyDown(KEY_RIGHT))) |
65 | { |
66 | moveDirection = -1; |
67 | movingAnim = false; |
68 | } |
69 | |
70 | if ((IsKeyDown(KEY_RIGHT)) || (moveDirection == 0)) |
71 | { |
72 | player.currentSeq = WALK_RIGHT; |
73 | framesCounter++; |
74 | |
75 | if (framesCounter > 15) |
76 | { |
77 | player.currentFrame++; |
78 | framesCounter = 0; |
79 | |
80 | if (player.currentFrame > PLAYER_ANIM_FRAMES - 1) player.currentFrame = 0; |
81 | } |
82 | |
83 | player.position.x += 4; |
84 | } |
85 | else if ((IsKeyDown(KEY_LEFT)) || (moveDirection == 1)) |
86 | { |
87 | player.currentSeq = WALK_LEFT; |
88 | framesCounter++; |
89 | |
90 | if (framesCounter > 15) |
91 | { |
92 | player.currentFrame++; |
93 | framesCounter = 0; |
94 | |
95 | if (player.currentFrame > PLAYER_ANIM_FRAMES - 1) player.currentFrame = 0; |
96 | } |
97 | |
98 | player.position.x -= 4; |
99 | } |
100 | else player.currentFrame = 0; |
101 | } |
102 | else |
103 | { |
104 | framesCounter++; |
105 | animTimer++; |
106 | |
107 | if (framesCounter > 10) |
108 | { |
109 | player.currentFrame++; |
110 | framesCounter = 0; |
111 | |
112 | if (player.currentFrame > PLAYER_ANIM_FRAMES - 1) player.currentFrame = 0; |
113 | |
114 | // We can adjust animation playing time depending on sequence |
115 | switch (player.currentSeq) |
116 | { |
117 | case SCARE_RIGHT: |
118 | { |
119 | if (animTimer > 180) |
120 | { |
121 | animTimer = 0; |
122 | outControl = false; |
123 | player.currentSeq = WALK_LEFT; |
124 | } |
125 | } break; |
126 | case SCARE_LEFT: |
127 | { |
128 | if (animTimer > 240) |
129 | { |
130 | animTimer = 0; |
131 | outControl = false; |
132 | player.currentSeq = WALK_RIGHT; |
133 | } |
134 | } break; |
135 | case SEARCH: |
136 | case FIND_KEY: |
137 | { |
138 | if (animTimer > 240) |
139 | { |
140 | animTimer = 0; |
141 | outControl = false; |
142 | player.currentSeq = WALK_RIGHT; |
143 | } |
144 | } break; |
145 | } |
146 | } |
147 | } |
148 | |
149 | if (player.position.x < 30) player.position.x = 30; |
150 | else if (player.position.x > (GetScreenWidth() - 200)) player.position.x = GetScreenWidth() - 200; |
151 | |
152 | if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) |
153 | { |
154 | pointerPosition = GetMousePosition(); |
155 | pointerAnim = true; |
156 | pointerAlpha = 1.0f; |
157 | |
158 | nextMovePoint = (int)pointerPosition.x; |
159 | movingAnim = true; |
160 | } |
161 | |
162 | if (movingAnim) |
163 | { |
164 | if (nextMovePoint > (player.position.x + (player.frameRec.width/2) + 5)) moveDirection = 0; // Move Left |
165 | else if (nextMovePoint < (player.position.x + (player.frameRec.width/2) - 5)) moveDirection = 1; // Move Right |
166 | else |
167 | { |
168 | moveDirection = -1; |
169 | movingAnim = 0; |
170 | } |
171 | } |
172 | |
173 | player.frameRec.x = player.currentFrame*player.texture.width/PLAYER_ANIM_FRAMES; |
174 | player.frameRec.y = (player.currentSeq - 1)*player.texture.height/PLAYER_ANIM_SEQ; |
175 | |
176 | // Update player bounds |
177 | player.bounds = (Rectangle){ player.position.x + 50, player.position.y - 60, 100, 300 }; |
178 | |
179 | // Mouse pointer alpha animation |
180 | if (pointerAnim) |
181 | { |
182 | pointerAlpha -= 0.1f; |
183 | |
184 | if (pointerAlpha <= 0.0f) |
185 | { |
186 | pointerAlpha = 0.0f; |
187 | pointerAnim = false; |
188 | } |
189 | } |
190 | } |
191 | // |
192 | void DrawPlayer(void) |
193 | { |
194 | DrawTextureRec(player.texture, player.frameRec, player.position, WHITE); |
195 | |
196 | // Draw mouse pointer on click |
197 | if (pointerAnim) DrawCircleV(pointerPosition, 20, Fade(RED, pointerAlpha)); |
198 | |
199 | DrawLifes(); |
200 | } |
201 | |
202 | void UnloadPlayer(void) |
203 | { |
204 | UnloadTexture(player.texture); |
205 | UnloadTexture(texLife); |
206 | } |
207 | |
208 | void ResetPlayer(void) |
209 | { |
210 | // Reset player variables |
211 | player.frameRec = (Rectangle){ 0, 0, player.texture.width/PLAYER_ANIM_FRAMES, player.texture.height/PLAYER_ANIM_SEQ }; |
212 | player.currentFrame = 0; |
213 | player.currentSeq = WALK_RIGHT; |
214 | |
215 | player.key = false; |
216 | player.dead = false; |
217 | |
218 | // Reset player position |
219 | if (player.position.x < 400) player.position.x = GetScreenWidth() - 350; |
220 | if (player.position.x > (GetScreenWidth() - 400)) player.position.x = 350; |
221 | |
222 | // Reset moving variables |
223 | movingAnim = false; |
224 | moveDirection = -1; |
225 | nextMovePoint = 0; |
226 | framesCounter = 0; |
227 | outControl = false; |
228 | animTimer = 0; |
229 | |
230 | // Reset pointer |
231 | pointerAlpha = 0.0f; |
232 | pointerAnim = false; |
233 | } |
234 | |
235 | void ScarePlayer(void) |
236 | { |
237 | player.currentFrame = 0; |
238 | |
239 | if (moveDirection == 0) player.currentSeq = SCARE_RIGHT; |
240 | else if (moveDirection == 1) player.currentSeq = SCARE_LEFT; |
241 | else player.currentSeq = SCARE_RIGHT; |
242 | |
243 | player.numLifes--; |
244 | |
245 | if (player.numLifes <= 0) player.dead = true; |
246 | |
247 | outControl = true; |
248 | } |
249 | |
250 | void SearchKeyPlayer(void) |
251 | { |
252 | moveDirection = -1; |
253 | movingAnim = 0; |
254 | |
255 | player.currentFrame = 0; |
256 | player.currentSeq = SEARCH; |
257 | |
258 | outControl = true; |
259 | } |
260 | |
261 | void FindKeyPlayer(void) |
262 | { |
263 | player.currentFrame = 0; |
264 | player.currentSeq = FIND_KEY; |
265 | player.key = true; |
266 | |
267 | outControl = true; |
268 | } |
269 | |
270 | static void DrawLifes(void) |
271 | { |
272 | if (player.numLifes != 0) |
273 | { |
274 | Vector2 position = { 20, GetScreenHeight() - texLife.height - 20 }; |
275 | |
276 | for(int i = 0; i < player.numLifes; i++) |
277 | { |
278 | DrawTexture(texLife, position.x + i*texLife.width, position.y, Fade(RAYWHITE, 0.7f)); |
279 | } |
280 | } |
281 | } |