forked from syamcode/PecimenGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
score.cpp
197 lines (182 loc) · 5.38 KB
/
score.cpp
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
void initScore(playerControl *player){ //Fahmi Rosdiansyah
player->score=0;
}
void initLives(playerControl *player){//Fahmi Rosdiansyah
player->lives=5;
}
void incScore(int food, playerControl *player){//Fahmi Rosdiansyah
switch(food){
case FOOD1:
player->score+=FOOD1_SCORE;
break;
case FOOD2:
player->score+=FOOD2_SCORE;
break;
case FOOD3:
player->score+=FOOD3_SCORE;
break;
case FOOD4:
player->score+=FOOD4_SCORE;
break;
case FOOD5:
player->score+=FOOD5_SCORE;
break;
}
}
void eatFood(playerControl *player){//Fahmi Rosdiansyah
incScore(levelMap[player->peciman.pos.x][player->peciman.pos.y].Food, player);
if (levelMap[player->peciman.pos.x][player->peciman.pos.y].Food==FOOD1){
PlaySound("sounds/pacman_chomp.wav",NULL,SND_ASYNC);
player->foodCount--;
}
else if(levelMap[player->peciman.pos.x][player->peciman.pos.y].Food==FOOD2 || levelMap[player->peciman.pos.x][player->peciman.pos.y].Food==FOOD3 || levelMap[player->peciman.pos.x][player->peciman.pos.y].Food==FOOD4 ||levelMap[player->peciman.pos.x][player->peciman.pos.y].Food==FOOD5){
PlaySound("sounds/pacman_eatfruit.wav",NULL,SND_ASYNC);
}
levelMap[player->peciman.pos.x][player->peciman.pos.y].Food=EMPTY;
}
void incLives(playerControl *player, int *liveGiven){
if(player->score>=2500 && *liveGiven==0 && player->lives<MAX_LIVES){
player->lives++;
*liveGiven=1;
}
else if(player->score>=5000 && *liveGiven==1 && player->lives<MAX_LIVES){
player->lives++;
*liveGiven=2;
}
else if(player->score>=15000 && *liveGiven==2 && player->lives<MAX_LIVES){
player->lives++;
*liveGiven=3;
}
else if(player->score>=25000 && *liveGiven==3 && player->lives<MAX_LIVES){
player->lives++;
*liveGiven=4;
}
else if(player->score>=40000 && *liveGiven==4 && player->lives<MAX_LIVES){
player->lives++;
*liveGiven=5;
}
}
int randomise(int min, int max){//Fahmi Rosdiansyah
return rand()%(max+1-min)+min;
}
int foodType(int x){//Fahmi Rosdiansyah
switch(x){
case 1 ... 40: return FOOD2; break; //40%
case 41 ... 70: return FOOD3; break; //30%
case 71 ... 90: return FOOD4; break; //20%
case 91 ... 100: return FOOD5; break; //10%
}
}
void spawnFood(MapController map[20][20], position pos){//Fahmi Rosdiansyah
// position pos;
// pos.x = posX;
// pos.y = posY;
map[pos.x][pos.y].Food=foodType(randomise(1,100));
Food(map[pos.x][pos.y].Food, pos);
}
void despawnFood(MapController map[20][20], position pos){//Fahmi Rosdiansyah
map[pos.x][pos.y].Food=EMPTY;
setcolor(BLACK);
bar(pos.x * GRIDSIZE, pos.y* GRIDSIZE, (pos.x * GRIDSIZE) + GRIDSIZE, pos.y*GRIDSIZE + GRIDSIZE);
}
void spawnPortal(MapController map[20][20], position pos, int in)
{
if(in)
map[pos.x][pos.y].Object=33;
else
map[pos.x][pos.y].Object=34;
DrawPortal(map[pos.x][pos.y].Object, pos);
map[pos.x][pos.y].Object=0;
map[pos.x][pos.y].Object=0;
}
void despawnPortal(MapController map[20][20], position INp, position OUTp)
{
setcolor(BLACK);
bar(INp.x * GRIDSIZE, INp.y* GRIDSIZE, (INp.x * GRIDSIZE) + GRIDSIZE, INp.y*GRIDSIZE + GRIDSIZE);
setcolor(BLACK);
bar(OUTp.x * GRIDSIZE, OUTp.y* GRIDSIZE, (OUTp.x * GRIDSIZE) + GRIDSIZE, OUTp.y*GRIDSIZE + GRIDSIZE);
}
position FindAndRandomEmptyNodeObject(){
position pos;
int i, count=0, arr[nodeCount];
for(i=0;i<nodeCount;i++){
if(levelMap[nodePos[i*2+0]][nodePos[i*2+1]].Object==EMPTY){
arr[count]=i;
count++;
}
}
int idx = randomise(0,count-1);
pos.x = nodePos[arr[idx]*2+0];
pos.y = nodePos[arr[idx]*2+1];
return pos;
// spawnFood(&levelMap[pos.x][pos.y],pos.x, pos.y);
}
position FindAndRandomEmptyNode(){
position pos;
int i, count=0, arr[nodeCount];
for(i=0;i<nodeCount;i++){
if(levelMap[nodePos[i*2+0]][nodePos[i*2+1]].Food==EMPTY){
arr[count]=i;
count++;
}
}
int idx = randomise(0,count-1);
pos.x = nodePos[arr[idx]*2+0];
pos.y = nodePos[arr[idx]*2+1];
return pos;
// spawnFood(&levelMap[pos.x][pos.y],pos.x, pos.y);
}
void storeScore(playerControl *player){
dataStore scoreData;
strcpy(scoreData.name, player->name);
scoreData.score = player->score;
FILE *highscore;
highscore=fopen("assets/files/highscore.dat", "a+");
if(highscore==NULL)
return;
fwrite(&scoreData, sizeof(scoreData), 1, highscore);
fclose(highscore);
sortFile("assets/files/highscore.dat");
}
int scoreCount() {
int sum=0;
dataStore score;
FILE *file;
file=fopen("assets/files/highscore.dat", "rb");
fread(&score, sizeof(score), 1, file);
while(!feof(file)) {
fread(&score, sizeof(score), 1, file);
sum++;
}
fclose(file);
return sum;
}
void sortFile(char *filename){
int structSize, fileSize, i, j;
FILE * file;
file = fopen(filename,"rb+");
if(file==NULL)
return;
dataStore pivot, temp;
structSize = sizeof(pivot);
fseek(file, 0, SEEK_END);
fileSize = ftell(file);
rewind(file);
for (i = 0; i < fileSize; i += structSize){
for (j = 0; j < fileSize - structSize; j += structSize){
fread(&pivot, structSize, 1, file);
fread(&temp, structSize, 1, file);
if (pivot.score < temp.score){
fseek(file, -(structSize * 2), SEEK_CUR);
fwrite(&temp, structSize, 1, file);
fwrite(&pivot, structSize, 1, file);
fseek(file, -structSize, SEEK_CUR);
}
else{
fseek(file, -structSize, SEEK_CUR);
}
}
rewind(file);
}
fclose(file);
}