This repository was archived by the owner on Jul 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPlayer.cpp
More file actions
142 lines (114 loc) · 4.06 KB
/
CPlayer.cpp
File metadata and controls
142 lines (114 loc) · 4.06 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
//
// CPlayer.cpp
// PacMan
//
// Created by Vsevolod Klementjev on 11/18/12.
// Copyright (c) 2012 Vsevolod Klementjev. All rights reserved.
//
#include "CPlayer.h"
// drift range in pixels to make a turn before an actual turn
static const int CORNERING = 5;
CPlayer::CPlayer(CMap *map) : CEntity(map)
{
// loading pac man sprites
char directions[4] = { 'u', 'd', 'l', 'r' };
char filename[50];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 2; j++) {
sprintf(filename, "pm%c%d.png", directions[i], j + 1);
CEntity::loadSprite(filename, i, j);
}
}
_consumedFood = 0; // won't be reset in reset
Reset(); // resets values and starts movement
}
void CPlayer::Update()
{
CEntity::Update();
consumeFood(); // try to eat anything beneath us
if (!_moving) {
Move(_direction); // try to move if we stopped
}
}
Direction CPlayer::getDirection()
{
return _direction;
}
void CPlayer::Move(Direction direction)
{
if (_moving) // since player will stop only hitting a wall
{
// just reverse
if (isReverse(direction)) {
reverseDirection();
}
// current direction in grid
int currXAdd = _direction == DirectionRight ? 1 : _direction == DirectionLeft ? -1 : 0;
int currYAdd = _direction == DirectionDown ? 1 : _direction == DirectionUp ? -1 : 0;
// proposed direction in grid
int xAdd = direction == DirectionRight ? 1 : direction == DirectionLeft ? -1 : 0;
int yAdd = direction == DirectionDown ? 1 : direction == DirectionUp ? -1 : 0;
// checking every direction including cornering
bool yRight = yAdd != 0 && currXAdd == 1 && (_pixelX % TILE_SIZE - TILE_SIZE * 0.5f) <= CORNERING;
bool yLeft = yAdd != 0 && currXAdd == -1 && (_pixelX % TILE_SIZE + TILE_SIZE * 0.5f) >= TILE_SIZE - CORNERING;
bool xDown = xAdd != 0 && currYAdd == 1 && (_pixelY % TILE_SIZE - TILE_SIZE * 0.5f) <= CORNERING;
bool xUp = xAdd != 0 && currYAdd == -1 && (_pixelY % TILE_SIZE + TILE_SIZE * 0.5f) >= TILE_SIZE - CORNERING;
if ((yRight || yLeft || xDown || xUp) && ableToMove(xAdd, yAdd)) // make a turn if we can
{
_direction = direction;
_moving = true;
_targetX = CMap::getXcoordinate(CMap::getTileXcoordinate(_pixelX) + xAdd);
_targetY = CMap::getYcoordinate(CMap::getTileYcoordinate(_pixelY) + yAdd);
}
return;
}
CEntity::Move(direction); // if we stopped and player is pressing a turn
}
// used to note if player picks up energizer buff
bool CPlayer::isEnergized()
{
return _energized;
}
// used to reset energized buff when we activate fright phase in main loop
void CPlayer::resetEnergize()
{
_energized = false;
}
int CPlayer::dotsEaten()
{
return _consumedFood; // dots picked up
}
void CPlayer::resetDots()
{
_consumedFood = 0; // resetting dot tracker when new lvl comes or game is over
}
int CPlayer::sign(int v)
{
// left to change pacman speed for testing
return v > 0 ? 4 : (v < 0 ? -4 : 0);
}
void CPlayer::consumeFood()
{
int x = CMap::getTileXcoordinate(_pixelX);
int y = CMap::getTileYcoordinate(_pixelY);
if (_map->getObjectAt(x, y) & MapObjectBall) {
// if there's a ball beneath us, consume it and remove from the map
_consumedFood++;
_map->setObjectAt(_map->getObjectAt(x, y) ^ MapObjectBall, x, y);
}
if (_map->getObjectAt(x, y) & MapObjectEnergizer) {
// if there's an energizer beneath us, consume it, add energize buff and remove from the map
_consumedFood++;
_energized = true;
_map->setObjectAt(_map->getObjectAt(x, y) ^ MapObjectEnergizer, x, y);
}
}
void CPlayer::Reset()
{
// resetting positions and buffs
_energized = false;
_moving = false;
_pixelX = CMap::getXcoordinate(14) - TILE_SIZE * 0.5f;
_pixelY = CMap::getYcoordinate(26);
Move(DirectionLeft); // start moving right away
}