-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.c
212 lines (184 loc) · 4.94 KB
/
player.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "player.h"
#include "enemy.h"
#include "deck.h"
#include "card.h"
#include "screen.h"
#include "card.h"
#include "button.h"
int Player_Init(Player *player, Button* button)
{
player->handCapacity = 5;
player->handSize = 0;
player->hand = (Card**)malloc(sizeof(Card*) * player->handCapacity);
player->activeCard = NULL;
player->health = PLAYER_INITIAL_HEALTH;
player->energy = PLAYER_INITIAL_ENERGY;
player->block = 0;
player->useCardButton = button;
return 0;
}
void Player_CleanUp(Player *player)
{
free(player->hand);
}
void Player_AddCard(Player *player, Card *card, int index)
{
player->hand[index] = card;
player->handSize++;
int lengthOfAllCards = (player->handCapacity * CARD_WIDTH) + ((player->handCapacity - 1) * 20);
int handStartPosition = (SCREEN_WIDTH - lengthOfAllCards) / 2;
player->hand[index]->model->x = handStartPosition + ((index * CARD_WIDTH) + (index * 20) + 20);
player->hand[index]->model->y = SCREEN_HEIGHT - CARD_HEIGHT - 20;
}
void Player_SelectCard(Player *player, int cardIndex)
{
if(player->hand[cardIndex] == NULL)
{
return;
}
if(player->hand[cardIndex] == player->activeCard)
{
Card_Toggle(player->hand[cardIndex], 0);
player->activeCard = NULL;
} else {
for(int i = 0; i < player->handSize; i++)
{
if(player->hand[i] != NULL)
{
if(player->hand[i] != player->hand[cardIndex])
{
Card_Toggle(player->hand[i], 0);
}
}
}
// if player has enough energy, select the card
if(player->hand[cardIndex]->cost <= player->energy)
{
player->activeCard = player->hand[cardIndex];
Card_Toggle(player->activeCard, 1);
}
}
}
void Player_HandleClick(Player *player, int x, int y)
{
// if the player has no energy, they can't select a card
if (player->energy < 1)
{
return;
}
for (int i = 0; i < player->handSize; i++)
{
if (player->hand[i] != NULL)
{
if(Card_Intersect(player->hand[i], x, y))
{
Player_SelectCard(player, i);
}
}
}
}
void Player_UseCard(Player *player, Enemy *enemy)
{
if (player->energy < 1)
{
return;
}
if(player->activeCard == NULL)
{
return;
}
if(player->energy < player->activeCard->cost)
{
return;
}
if(player->activeCard->type == ATTACK_CARD)
{
enemy->health -= player->activeCard->value;
}
if(player->activeCard->type == BLOCK_CARD)
{
player->block += player->activeCard->value;
}
player->energy -= player->activeCard->cost;
// remove card from hand
for (int i = 0; i < player->handSize; i++)
{
if (player->hand[i] != NULL)
{
if(player->hand[i] == player->activeCard)
{
player->hand[i] = NULL;
player->handSize--;
player->activeCard = NULL;
}
}
}
// move each card in hand array back by one
for (int i = 0; i < player->handCapacity; i++)
{
if (player->hand[i] == NULL)
{
for (int j = i; j < player->handCapacity; j++)
{
player->hand[j] = player->hand[j + 1];
}
}
}
// reposition cards
int lengthOfAllCards = (player->handSize * CARD_WIDTH) + ((player->handSize - 1) * 20);
int handStartPosition = (SCREEN_WIDTH - lengthOfAllCards) / 2;
for (int i = 0; i < player->handSize; i++)
{
if (player->hand[i] != NULL)
{
player->hand[i]->model->x = handStartPosition + ((i * CARD_WIDTH) + (i * 20) + 20);
player->hand[i]->model->y = SCREEN_HEIGHT - CARD_HEIGHT - 20;
}
}
}
void Player_Update(Player *player, float deltaTime)
{
for (int i = 0; i < player->handSize; i++)
{
if (player->hand[i] != NULL)
{
Card_Update(player->hand[i], deltaTime);
}
}
}
int Player_Render(SDL_Renderer *renderer, Player *player, TTF_Font *font)
{
// render health
char healthText[12];
sprintf(healthText, "Health: %d", player->health);
TTF_SetFontSize(font, 24);
TTF_SetFontStyle(font, TTF_STYLE_NORMAL);
SDL_Surface *healthSurface = Text_Create(font, healthText, (SDL_Color){255, 255, 255});
Text_Render(renderer, healthSurface, 32, SCREEN_HEIGHT - 192);
// // render energy
char energyText[8];
sprintf(energyText, "Mana: %d", player->energy);
TTF_SetFontSize(font, 24);
TTF_SetFontStyle(font, TTF_STYLE_NORMAL);
SDL_Surface *energySurface = Text_Create(font, energyText, (SDL_Color){255, 255, 255});
Text_Render(renderer, energySurface, 32, SCREEN_HEIGHT - 160);
// render block
char blockText[12];
sprintf(blockText, "Block: %d", player->block);
TTF_SetFontSize(font, 24);
TTF_SetFontStyle(font, TTF_STYLE_NORMAL);
SDL_Surface *blockSurface = Text_Create(font, blockText, (SDL_Color){255, 255, 255});
Text_Render(renderer, blockSurface, 32, SCREEN_HEIGHT - 128);
// render hand
for (int i = 0; i < player->handSize; i++)
{
if (player->hand[i] != NULL)
{
Card_Render(renderer, player->hand[i], font);
}
}
if(player->activeCard != NULL) {
Button_Render(renderer, player->useCardButton, font);
}
return 0;
}