aboutsummaryrefslogtreecommitdiff
path: root/games/skully_escape/player.c
blob: 11006f65d1baa947e4c8e026f2b817aa771d3cae (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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/***********************************************************************************
*
*   KING GAME JAM - GRAY TEAM
*
*   <Game title>
*   <Game description>
*
*   This game has been created using raylib (www.raylib.com)
*   raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
*   Copyright (c) 2014 Ramon Santamaria (Ray San - raysan@raysanweb.com)
*
************************************************************************************/

#include "raylib.h"
#include "player.h"

#define PLAYER_ANIM_FRAMES     4
#define PLAYER_ANIM_SEQ        6

//----------------------------------------------------------------------------------
// Module Variables Definition
//----------------------------------------------------------------------------------

// Player mouse moving variables
static bool movingAnim;
static int moveDirection;
static int nextMovePoint;

// Mouse pointer variables
static Vector2 pointerPosition;
static bool pointerAnim;
static float pointerAlpha;

static int framesCounter;
static bool outControl = false;

static int animTimer = 0;

static Texture2D texLife;

static void DrawLifes(void);

// player initialitaction definition
void InitPlayer(void)
{
    // NOTE: Some player variables are only initialized once
    player.texture = LoadTexture("resources/textures/skully.png");
    player.position = (Vector2){ 350, 400 };
    player.numLifes = 4;
    
    ResetPlayer();
    
    framesCounter = 0;

    texLife = LoadTexture("resources/textures/skully_icon.png");
}

// player update definition
void UpdatePlayer(void)
{
    if (!outControl)
    {
        if ((IsKeyDown(KEY_LEFT)) || (IsKeyDown(KEY_RIGHT)))
        {
            moveDirection = -1;
            movingAnim = false;
        }
        
        if ((IsKeyDown(KEY_RIGHT)) || (moveDirection == 0))
        {
            player.currentSeq = WALK_RIGHT;
            framesCounter++;
            
            if (framesCounter > 15)
            {
                player.currentFrame++;
                framesCounter = 0;
                
                if (player.currentFrame > PLAYER_ANIM_FRAMES - 1) player.currentFrame = 0;
            }
            
            player.position.x += 4;
        }
        else if ((IsKeyDown(KEY_LEFT)) || (moveDirection == 1))
        {
            player.currentSeq = WALK_LEFT;
            framesCounter++;
            
            if (framesCounter > 15)
            {
                player.currentFrame++;
                framesCounter = 0;
                
                if (player.currentFrame > PLAYER_ANIM_FRAMES - 1) player.currentFrame = 0;
            }
            
            player.position.x -= 4;
        }
        else player.currentFrame = 0;
    }
    else
    {
        framesCounter++;
        animTimer++;
        
        if (framesCounter > 10)
        {
            player.currentFrame++;
            framesCounter = 0;
            
            if (player.currentFrame > PLAYER_ANIM_FRAMES - 1) player.currentFrame = 0;
            
            // We can adjust animation playing time depending on sequence
            switch (player.currentSeq)
            {
                case SCARE_RIGHT:
                {
                    if (animTimer > 180)
                    {
                        animTimer = 0;
                        outControl = false;
                        player.currentSeq = WALK_LEFT;
                    }
                } break;
                case SCARE_LEFT:
                {
                    if (animTimer > 240)
                    {
                        animTimer = 0;
                        outControl = false;
                        player.currentSeq = WALK_RIGHT;
                    }
                } break;
                case SEARCH:
                case FIND_KEY:
                {
                    if (animTimer > 240)
                    {
                        animTimer = 0;
                        outControl = false;
                        player.currentSeq = WALK_RIGHT;
                    }
                } break;
            }
        }
    }
    
    if (player.position.x < 30) player.position.x = 30;
    else if (player.position.x > (GetScreenWidth() - 200)) player.position.x = GetScreenWidth() - 200;
    
    if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
    {      
        pointerPosition = GetMousePosition();
        pointerAnim = true;
        pointerAlpha = 1.0f;
        
        nextMovePoint = (int)pointerPosition.x;
        movingAnim = true;
    }
    
    if (movingAnim)
    {
        if (nextMovePoint > (player.position.x + (player.frameRec.width/2) + 5))  moveDirection = 0;  // Move Left
        else if (nextMovePoint < (player.position.x + (player.frameRec.width/2) - 5)) moveDirection = 1;  // Move Right
        else 
        {
            moveDirection = -1;
            movingAnim = 0;
        }
    }
    
    player.frameRec.x = player.currentFrame*player.texture.width/PLAYER_ANIM_FRAMES;
    player.frameRec.y = (player.currentSeq - 1)*player.texture.height/PLAYER_ANIM_SEQ;
    
    // Update player bounds
    player.bounds = (Rectangle){ player.position.x + 50, player.position.y - 60, 100, 300 };
    
    // Mouse pointer alpha animation
    if (pointerAnim)
    {
        pointerAlpha -= 0.1f;
        
        if (pointerAlpha <= 0.0f) 
        {
            pointerAlpha = 0.0f;
            pointerAnim = false;
        }
    }
}
//
void DrawPlayer(void)
{
    DrawTextureRec(player.texture, player.frameRec, player.position, WHITE);
    
    // Draw mouse pointer on click
    if (pointerAnim) DrawCircleV(pointerPosition, 20, Fade(RED, pointerAlpha));
    
    DrawLifes();
}

void UnloadPlayer(void)
{
    UnloadTexture(player.texture);
    UnloadTexture(texLife);
}

void ResetPlayer(void)
{   
    // Reset player variables
    player.frameRec = (Rectangle){ 0, 0, player.texture.width/PLAYER_ANIM_FRAMES, player.texture.height/PLAYER_ANIM_SEQ };
    player.currentFrame = 0;
    player.currentSeq = WALK_RIGHT;
    
    player.key = false;
    player.dead = false;

    // Reset player position
    if (player.position.x < 400) player.position.x = GetScreenWidth() - 350;
    if (player.position.x > (GetScreenWidth() - 400)) player.position.x = 350;
    
    // Reset moving variables
    movingAnim = false;
    moveDirection = -1;
    nextMovePoint = 0;
    framesCounter = 0;
    outControl = false;
    animTimer = 0;
    
    // Reset pointer   
    pointerAlpha = 0.0f;
    pointerAnim = false;
}

void ScarePlayer(void)
{
    player.currentFrame = 0;

    if (moveDirection == 0) player.currentSeq = SCARE_RIGHT;
    else if (moveDirection == 1) player.currentSeq = SCARE_LEFT;
    else player.currentSeq = SCARE_RIGHT;
    
    player.numLifes--;
    
    if (player.numLifes <= 0) player.dead = true;
    
    outControl = true;
}

void SearchKeyPlayer(void)
{
    moveDirection = -1;
    movingAnim = 0;
            
    player.currentFrame = 0;
    player.currentSeq = SEARCH;
    
    outControl = true;
}

void FindKeyPlayer(void)
{
    player.currentFrame = 0;
    player.currentSeq = FIND_KEY;
    player.key = true;
    
    outControl = true;
}

static void DrawLifes(void)
{
    if (player.numLifes != 0)
    {
    	Vector2 position = { 20, GetScreenHeight() - texLife.height - 20 };
		
        for(int i = 0; i < player.numLifes; i++)
        {
        	DrawTexture(texLife, position.x + i*texLife.width, position.y, Fade(RAYWHITE, 0.7f));
        }
    }
}