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 pathCMap.cpp
More file actions
149 lines (129 loc) · 6.81 KB
/
CMap.cpp
File metadata and controls
149 lines (129 loc) · 6.81 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
//
// CMap.cpp
// PacMan
//
// Created by Vsevolod Klementjev on 11/17/12.
// Copyright (c) 2012 Vsevolod Klementjev. All rights reserved.
//
#include "CMap.h"
#include <assert.h>
// default level made up from MapObject
static const int defaultLevel[MAP_WIDTH][MAP_HEIGHT] =
{ { 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 5, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 },
{ 0, 0, 0, 2, 17, 17, 33, 17, 17, 17, 17, 17, 2, 0, 0, 0, 2, 5, 2, 0, 0, 0, 2, 17, 17, 17, 33, 2, 2, 17, 17, 17, 17, 2, 0, 0 },
{ 0, 0, 0, 2, 17, 2, 2, 2, 17, 2, 2, 17, 2, 0, 0, 0, 2, 5, 2, 0, 0, 0, 2, 17, 2, 2, 17, 2, 2, 17, 2, 2, 17, 2, 0, 0 },
{ 0, 0, 0, 2, 17, 2, 2, 2, 17, 2, 2, 17, 2, 0, 0, 0, 2, 5, 2, 0, 0, 0, 2, 17, 2, 2, 17, 17, 17, 17, 2, 2, 17, 2, 0, 0 },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, 2, 17, 2, 2, 2, 2, 2, 5, 2, 2, 2, 2, 2, 17, 2, 2, 2, 2, 2, 17, 2, 2, 17, 2, 0, 0 },
{ 0, 0, 0, 2, 17, 2, 2, 2, 17, 2, 2, 17, 2, 0, 0, 0, 2, 5, 2, 0, 0, 0, 2, 17, 2, 2, 2, 2, 2, 17, 2, 2, 17, 2, 0, 0 },
{ 0, 0, 0, 2, 17, 2, 2, 2, 17, 2, 2, 17, 2, 0, 0, 0, 2, 5, 2, 0, 0, 0, 2, 17, 2, 2, 17, 17, 17, 17, 2, 2, 17, 2, 0, 0 },
{ 0, 0, 0, 2, 17, 2, 2, 2, 17, 2, 2, 17, 2, 0, 0, 0, 2, 5, 2, 0, 0, 0, 2, 17, 2, 2, 17, 2, 2, 17, 2, 2, 17, 2, 0, 0 },
{ 0, 0, 0, 2, 17, 17, 33, 17, 17, 17, 17, 17, 2, 0, 0, 0, 2, 5, 2, 0, 0, 0, 2, 17, 17, 17, 33, 2, 2, 17, 17, 17, 17, 2, 0, 0 },
{ 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 5, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 } };
// our current - active map (so balls and energizers can be removed)
static int activeLevel[MAP_WIDTH][MAP_HEIGHT];
int CMap::getXcoordinate(int xtile)
{
// simple conversion and adding offset to center the coord
return xtile * TILE_SIZE - OBJECT_OFFSET;
}
int CMap::getYcoordinate(int ytile)
{
// simple conversion and adding offset to center the coord
return ytile * TILE_SIZE - OBJECT_OFFSET;
}
int CMap::getTileXcoordinate(int x)
{
// simple conversion and adding offset to center the coord
return (x + OBJECT_OFFSET) / TILE_SIZE;
}
int CMap::getTileYcoordinate(int y)
{
// simple conversion and adding offset to center the coord
return (y + OBJECT_OFFSET) / TILE_SIZE;
}
void CMap::Draw(SDL_Surface *dest)
{
_background->Draw(dest);
for (int i = 0; i < MAP_WIDTH; i++) {
for (int j = 0; j < MAP_HEIGHT; j++) {
// checking every tile of the world and drawing a ball or energizer if we find them
if (activeLevel[i][j] & MapObjectBall) {
_dot->setPosition(getXcoordinate(i), getYcoordinate(j));
_dot->Draw(dest);
}
else if (activeLevel[i][j] & MapObjectEnergizer) {
_energizer->setPosition(getXcoordinate(i), getYcoordinate(j));
_energizer->Draw(dest);
}
}
}
}
int CMap::getObjectAt(int x, int y)
{
// bounds hack for tunnel traveling
if (x < 0) {
x = MAP_WIDTH + x;
}
else if (x >= MAP_WIDTH) {
x = x - MAP_WIDTH;
}
if (y < 0) {
y = MAP_HEIGHT + y;
}
else if (y >= MAP_HEIGHT) {
y = y - MAP_HEIGHT;
}
assert(x >= 0 && x < MAP_WIDTH && y >= 0 && y < MAP_HEIGHT);
return activeLevel[x][y];
}
void CMap::setObjectAt(int object, int x, int y)
{
// used to remove balls and energizers
assert(x >= 0 && x < MAP_WIDTH && y >= 0 && y < MAP_HEIGHT);
activeLevel[x][y] = object;
}
void CMap::Reset()
{
// initializing our level with default values
for (int i = 0; i < MAP_WIDTH; i++) {
for (int j = 0; j < MAP_HEIGHT; j++) {
activeLevel[i][j] = defaultLevel[i][j];
}
}
}
CMap::CMap()
{
// just a background image of the maze, enough for pacman
_background = new CSprite("background.png");
_background->setPosition(0, 0);
_dot = new CSprite("dot.png");
_energizer = new CSprite("energizer.png");
Reset();
}
CMap::~CMap()
{
delete _background;
_background = NULL;
delete _dot;
_dot = NULL;
delete _energizer;
_energizer = NULL;
}