-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecho_stage.cpp
212 lines (197 loc) · 4.76 KB
/
echo_stage.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// echo_stage.cpp
/*
This file is part of L-Echo.
L-Echo is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
L-Echo is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with L-Echo. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <typeinfo>
#include <string>
#include <cfloat>
#include "echo_platform.h"
#include "echo_debug.h"
#include "echo_error.h"
#include "grid.h"
#include "echo_stage.h"
#include "echo_gfx.h"
stage::stage()
{
init(NULL, NULL, 0);
}
stage::stage(grid* my_start, std::string* my_name, int my_num_goals)
{
init(my_start, my_name, my_num_goals);
}
void stage::init(grid* my_start, std::string* my_name, int my_num_goals)
{
farthest = 0;
lowest = FLT_MAX;
grids = new STAGE_MAP();
start = my_start;
name = my_name;
num_goals = my_num_goals;
}
stage::~stage()
{
if(name != NULL)
delete name;
STAGE_MAP::iterator it = grids->begin();
STAGE_MAP::iterator end = grids->end();
while(it != end)
{
if(it->second != NULL)
delete it->second;
++it;
}
delete grids;
}
/** Adds the grid with the id.
* @param id The id of the grid to add
* @param ptr The grid to add
*/
void stage::add(std::string id, grid* ptr)
{
grids->insert(STAGE_MAP::value_type(id, ptr));
}
/** Adds the position of the grid. It was used in the old holes and launchers
* implementation, but used right now just to calculate the lowest position.
* @param pos The position of the grid. Just has to be the position of the grid at some angle.
* @param g The grid (isn't used now)
*/
void stage::add_pos(vector3f* pos, grid* g)
{
if(pos->y < lowest)
lowest = pos->y;
}
/// Gets the lowest y-coordinate that has a grid that can be landed on
float stage::get_lowest_level()
{
return(lowest);
}
/// Gets a grid with the given string id
grid* stage::get(std::string id)
{
STAGE_MAP::iterator pos = grids->find(id);
if(pos == grids->end())
return(NULL);
return(pos->second);
}
/// Draws all the grids
void stage::draw(vector3f angle)
{
STAGE_MAP::iterator it = grids->begin();
STAGE_MAP::iterator end = grids->end();
#ifndef ECHO_NDS
gfx_outline_start();
while(it != end)
{
if(it->second->should_draw())
it->second->draw(angle);
++it;
}
gfx_outline_mid();
it = grids->begin();
while(it != end)
{
if(it->second->should_draw())
it->second->draw(angle);
++it;
}
gfx_outline_end();
#else
while(it != end)
{
if(it->second->should_draw())
{
gfx_set_polyID(it->second->get_polyID(angle));
it->second->draw(angle);
}
++it;
}
#endif
}
/// Sets the initial starting point of the stage
void stage::set_start(grid* g)
{
start = g;
}
/// Sets the name of the stage
void stage::set_name(std::string* my_name)
{
name = my_name;
}
/// Sets the number of goals of the stage
void stage::set_num_goals(int my_num_goals)
{
num_goals = my_num_goals;
}
/// Gets the initial starting point of this stage
grid* stage::get_start()
{
return(start);
}
/// Gets the name of the stage
std::string* stage::get_name()
{
return(name);
}
/// Gets the total number of goals of the stage
int stage::get_num_goals()
{
return(num_goals);
}
/** Tests if any grid is intersected by the line represented by the
* Screen Position points
* @param p1 Screen Position; point 1 of the line
* @param p2 Screen Position; point 2 of the line
* @param angle Current camera angle
*/
grid* stage::get_grid_intersection(vector3f* p1, vector3f* p2, vector3f angle)
{
grid* ret = NULL;
float shortest_dist = FLT_MAX;
STAGE_MAP::iterator it = grids->begin();
STAGE_MAP::iterator end = grids->end();
while(it != end)
{
/// Short-circuit if you can't land on the line anyways
if(it->second->should_land(angle) && it->second->projected_line_intersect(p1, p2, angle))
{
/// The line intersects, but we want the nearest grid
grid_info_t* info = it->second->get_info(angle);
if(info != NULL)
{
/// Get the distance from the first point
vector3f* rot = info->pos->rotate_xy(angle);
float dist = p1->dist(rot);
if(dist < shortest_dist)
{
ret = it->second;
shortest_dist = dist;
}
delete rot;
}
}
it++;
}
return(ret);
}
/// Set the farthest position
void stage::set_farthest(float new_far)
{
if(farthest < new_far)
farthest = new_far;
}
/// Get the farthest position
float stage::get_farthest()
{
return(farthest);
}