-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPlayerStatus.cpp
More file actions
177 lines (149 loc) · 4.25 KB
/
PlayerStatus.cpp
File metadata and controls
177 lines (149 loc) · 4.25 KB
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
#include "Utility/package.hpp"
#include "package.h"
//---------------------------------------------------------------------------------
// Written by Terence J. Grant - tjgrant [at] tatewake [dot] com
// Find the full tutorial at: http://gamedev.tutsplus.com/series/
//----------------------------------------------------------------------------------
const float PlayerStatus::kMultiplierExpiryTime = 0.8f;
const int PlayerStatus::kMaxMultiplier = 20;
const std::string PlayerStatus::kHighScoreFilename("highscore.txt");
void CreatePathIfNonExistant2(const std::string& newPath);
void CreatePathIfNonExistant2(const std::string& newPath)
{
// TODO: RR: Implement this
/*@autoreleasepool
{
// Create the path if it doesn't exist
NSError *error;
[[NSFileManager defaultManager]
createDirectoryAtPath:[NSString stringWithUTF8String:newPath.c_str()]
withIntermediateDirectories:YES
attributes:nil
error:&error];
}*/
}
std::string GetExecutableName2();
std::string GetExecutableName2()
{
// TODO: RR: Implement this
//return [[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleExecutable"] UTF8String];
return "TODO";
}
std::string GetPreferencePath2(const std::string& file);
std::string GetPreferencePath2(const std::string& file)
{
// TODO: RR: Implement this
/*std::string result = std::string([[NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) objectAtIndex:0] UTF8String]) + "/" +
GetExecutableName2() + "/";*/
std::string result = "";
CreatePathIfNonExistant2(result);
return result + file;
}
int PlayerStatus::LoadHighScore()
{
int score = 0;
std::string fstring;
// TODO: RR: Implement this
/*if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithUTF8String:GetPreferencePath2(kHighScoreFilename).c_str()]])
{
fstring = [[NSString stringWithContentsOfFile:[NSString stringWithUTF8String:GetPreferencePath2(kHighScoreFilename).c_str()]
encoding:NSUTF8StringEncoding error:nil] UTF8String];
if (!fstring.empty())
{
sscanf(fstring.c_str(), "%d", &score);
}
}*/
return score;
}
void PlayerStatus::SaveHighScore(int score)
{
char buf[20];
sprintf(buf, "%d", score);
// TODO: RR: Implement this
/*[[NSString stringWithUTF8String:buf] writeToFile:[NSString stringWithUTF8String:GetPreferencePath2(kHighScoreFilename).c_str()] atomically:YES
encoding:NSUTF8StringEncoding error:nil];*/
}
PlayerStatus::PlayerStatus()
{
mScore = 0;
mHighScore = LoadHighScore();
reset();
mLastTime = tTimer::getTimeMS();
}
void PlayerStatus::reset()
{
if (mScore > mHighScore)
{
mHighScore = mScore;
SaveHighScore(mHighScore);
}
mScore = 0;
mMultiplier = 1;
mLives = 4;
mScoreForExtraLife = 2000;
mMultiplierTimeLeft = 0;
}
void PlayerStatus::update()
{
if (mMultiplier > 1)
{
mMultiplierTimeLeft -= float(tTimer::getTimeMS() - mLastTime) / 1000.0f;
if (mMultiplierTimeLeft <= 0)
{
mMultiplierTimeLeft = kMultiplierExpiryTime;
resetMultiplier();
}
}
mLastTime = tTimer::getTimeMS();
}
void PlayerStatus::addPoints(int basePoints)
{
if (!PlayerShip::getInstance()->getIsDead())
{
mScore += basePoints * mMultiplier;
while (mScore >= mScoreForExtraLife)
{
mScoreForExtraLife += 2000;
mLives++;
}
}
}
void PlayerStatus::increaseMultiplier()
{
if (!PlayerShip::getInstance()->getIsDead())
{
mMultiplierTimeLeft = kMultiplierExpiryTime;
if (mMultiplier < kMaxMultiplier)
{
mMultiplier++;
}
}
}
void PlayerStatus::resetMultiplier()
{
mMultiplier = 1;
}
void PlayerStatus::removeLife()
{
mLives--;
}
int PlayerStatus::getLives() const
{
return mLives;
}
int PlayerStatus::getScore() const
{
return mScore;
}
int PlayerStatus::getHighScore() const
{
return mHighScore;
}
int PlayerStatus::getMultiplier() const
{
return mMultiplier;
}
bool PlayerStatus::getIsGameOver() const
{
return mLives == 0;
}