/
Player.cpp
178 lines (147 loc) · 3.73 KB
/
Player.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
//
// Player.cpp
// template
//
// Created by Max Marze on 2/26/13.
//
//
#include "Player.h"
#include <iostream>
#include <fstream>
//using std::ofstream;
//using std::cout;
//using std::endl;
//#define PLAYERSTARTHEIGHT 535.911f
#define PLAYERSTARTHEIGHT 368.f
Player::Player()
{
playerState = 0;
playerScore = 0;
playerLivesLeft = 3;
}
void Player::CreatePlayer(int id)
{
playerID = id;
agk::CreateSprite(playerID, agk::LoadImage("/Users/maxmarze/Documents/AGK_BETA/AGK/IDE/templates/frogio/ship_one.png"));
agk::SetSpritePosition(playerID, 16.f, PLAYERSTARTHEIGHT);
agk::SetSpriteScale(playerID, 2.f, 2.f);
agk::SetSpritePhysicsOn(playerID);
agk::SetSpritePhysicsCanRotate(playerID, 0);
midScreenWid = agk::GetVirtualWidth() / 2.0f;
playerWid = agk::GetSpriteWidth(playerID) / 2.0f;
playerOffset = midScreenWid + playerWid;
totalPlayerScore = 20000;
//// DEBUG
std::cout << "Player mass:" << agk::GetSpritePhysicsMass(playerID) << std::endl << std::endl;
std::cout << "Player lives: " << playerLivesLeft << std::endl << std::endl;
}
void Player::SetPlayerName(string name)
{
playerName = name;
}
int Player::GetID() const
{
return playerID;
}
int Player::GetLives() const
{
return playerLivesLeft;
}
void Player::DecreaseLives()
{
playerLivesLeft--;
}
void Player::DisplayScore()
{
agk::PrintC("Total Score: ");
agk::Print(totalPlayerScore);
agk::PrintC("Level Score: ");
agk::Print(playerScore);
}
void Player::PowerUpPlayer()
{
playerState = 1;
/// Recreate sprite with appropriate state
}
void Player::RespawnPlayer()
{
agk::DeleteSprite(1);
agk::Print("YOU LOST!");
CreatePlayer(playerID);
}
void Player::CheckKeyState()
{
float speed = 10000.f;
//float jumpSpeed = 15000.f;
float jumpSpeed = 75000.f;
float x = agk::GetSpriteX(playerID);
float y = agk::GetSpriteY(playerID);
// update based on changes since last frame
float mathStuff = x - playerOffset;
float mathStuffH = y - playerOffset;
//agk::SetViewOffset(mathStuff, 0);
agk::SetViewOffset(mathStuff, mathStuffH);
// check for movement keys
if(agk::GetRawKeyState(65) == 1)
{
agk::SetSpritePhysicsImpulse(playerID, x, y, -speed, y);
} else if(agk::GetRawKeyState(68) == 1)
{
agk::SetSpritePhysicsImpulse(playerID, x, y, speed, y);
}
if(agk::GetRawKeyPressed(87) == 1)
{
agk::SetSpritePhysicsImpulse(playerID, x, y, x, -jumpSpeed);
}
}
void Player::CheckKeyRelease()
{
if(agk::GetRawKeyReleased(65) == 1)
{
agk::SetSpritePhysicsVelocity(playerID, 0, 0);
}
if(agk::GetRawKeyReleased(68) == 1)
{
agk::SetSpritePhysicsVelocity(playerID, 0, 0);
}
if(agk::GetRawKeyReleased(87) == 1)
{
agk::SetSpritePhysicsVelocity(playerID, 0, 0);
}
}
void Player::MovePlayer()
{
CheckKeyState();
CheckKeyRelease();
if(agk::GetSpriteCollision(playerID, 11) == 1)
{
RespawnPlayer();
}
}
/// data member to increase the width to check in order to move
void Player::CheckPlayerScreenBounds(int &bkg)
{
if(agk::GetSpriteX(playerID) >= agk::GetVirtualWidth())
{
}
}
void Player::LevelEndPlayer()
{
totalPlayerScore += playerScore;
playerScore = 0;
agk::DeleteSprite(playerID);
}
bool Player::LifeCheck()
{
if(playerLivesLeft == 0)
return true;
else
return false;
}
void Player::ScoresToFile()
{
std::ofstream outfile ("/Users/maxmarze/Documents/AGK_BETA/AGK/IDE/templates/frogio/leaderboard.txt", std::ios_base::app);
outfile << "Score: " << totalPlayerScore;
outfile << " of Player, " << playerName << std::endl;
outfile.close();
}