-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorld.cpp
More file actions
132 lines (100 loc) · 2.52 KB
/
World.cpp
File metadata and controls
132 lines (100 loc) · 2.52 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
/*
* World.cpp
*
* Created on: 10 maj 2010
* Author: meros
*/
#include "World.h"
#include <fstream>
using namespace std;
#define BLOCK_SIZE 0.20
World::World() :
myCollisionWorld(b2Vec2(0.0f, 20.0f)), myFrameCounter(0) {
// TODO Auto-generated constructor stub
ifstream level("data/level.txt");
char buf[1024];
int px = 0;
int py = 0;
int y = 0;
while (level.getline(buf, 1024))
{
int x = 0;
char* block = buf;
int startpos = -1;
while (*block) {
if (*block == 'P') {
px = x;
py = y;
}
if (*block == 'X' && startpos == -1) {
startpos = x;
} else if (*block != 'X' && startpos != -1) {
myBlocks.push_back(
new Block(myCollisionWorld, startpos * BLOCK_SIZE,
y * BLOCK_SIZE, BLOCK_SIZE * (x - startpos),
BLOCK_SIZE));
startpos = -1;
}
//Create rope
if (*block == 'R') {
myRopes.push_back(
new Rope(myCollisionWorld, (x + 0.5f) * BLOCK_SIZE,
y * BLOCK_SIZE));
}
//Create bridge
if (*block == 'B') {
myBridges.push_back(
new Bridge(myCollisionWorld, x * BLOCK_SIZE,
y * BLOCK_SIZE));
}
block++;
x++;
}
if (startpos != -1) {
myBlocks.push_back(
new Block(myCollisionWorld, startpos * BLOCK_SIZE,
y * BLOCK_SIZE, BLOCK_SIZE * (x - startpos),
BLOCK_SIZE));
}
startpos = -1;
y++;
}
myPlayer = new Player(myCollisionWorld, (px + 0.5) * BLOCK_SIZE,
(py + 0.5) * BLOCK_SIZE);
myContactListener = new ContactListener(myCollisionWorld);
}
World::~World() {
}
Point World::GetPlayerPosition() const {
return myPlayer->GetPosition();
}
void World::Update() {
myPlayer->Update();
vector<Rope*>::iterator it;
for (it = myRopes.begin(); it != myRopes.end(); it++)
(*it)->Update();
myCollisionWorld.Step(1.0f / 50.0f, 500, 50);
}
void World::Draw(sf::RenderTarget& aTarget) {
myFrameCounter++;
float xscale = 120;
float yscale = xscale;
float xtrans = myPlayer->GetPosition().myX - (400 / xscale);
float ytrans = myPlayer->GetPosition().myY - (300 / yscale);
myPlayer->Draw(aTarget, xscale, yscale, xtrans, ytrans);
{
vector<Block*>::iterator it;
for (it = myBlocks.begin(); it != myBlocks.end(); it++)
(*it)->Draw(aTarget, xscale, yscale, xtrans, ytrans);
}
{
vector<Rope*>::iterator it;
for (it = myRopes.begin(); it != myRopes.end(); it++)
(*it)->Draw(aTarget, xscale, yscale, xtrans, ytrans);
}
{
vector<Bridge*>::iterator it;
for (it = myBridges.begin(); it != myBridges.end(); it++)
(*it)->Draw(aTarget, xscale, yscale, xtrans, ytrans);
}
}