-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTerrain.cpp
More file actions
157 lines (138 loc) · 3.63 KB
/
Terrain.cpp
File metadata and controls
157 lines (138 loc) · 3.63 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
150
151
152
153
154
155
156
157
//
// Terrian.cpp
// TankGame
//
// Created by Jacob Gonzalez on 4/05/2015.
// Copyright (c) 2015 Jacob Gonzalez. All rights reserved.
//
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <climits>
#include "Terrain.h"
#include "Sprite.h"
#include "Timer.h"
void Terrain::_init()
{
_size = ConsoleTools::get_terminal_size();
_position = Point<int>::zero();
shuffle_wind_speed();
// create a array of terrain heights at every x pos
int *height_map = new int[get_size().x];
_gen_terrain(0, get_size().x, height_map);
// fill in the map with sprites for every x pos
for (int i = 0; i < get_size().x; ++i)
{
for (int h = get_size().y - height_map[i]; h < get_size().y; ++h)
{
Sprite *tile = new Sprite(" ");
tile->set_position(Point<int>(i, h));
if (h == get_size().y - 1)
{
tile->set_bcolor(ConsoleTools::white);
tile->tag = TERRAIN_ROCK;
tile->set_texture(" ");
}
else
{
tile->set_bcolor(ConsoleTools::green);
tile->tag = TERRAIN_DIRT;
}
add_child(tile);
}
}
delete[] height_map;
}
int Terrain::get_floor(int x)
{
// the minimum y value is the top of the terrian
int min = INT_MAX;
LOOPV(i, this)
{
Point<int> tile_pos = get_child(i)->get_position();
if (tile_pos.x == x && tile_pos.y < min)
{
min = tile_pos.y;
}
}
return min;
}
bool Terrain::impact(Point<int> pos, int radius)
{
int old_pos = _position.y;
bool hit = false;
// increment radius destruction in steps for animation
for (int r = 2; r < radius; r+=2)
{
// pause for 0.05 seconds
Timer timer(0.05, 1);
while (timer.tick())
{
LOOPV(i, this)
{
// get the distance and add horrizontal stretch to look nicer
Point<int> tile_pos = get_child(i)->get_position();
Point<int> dist = tile_pos - pos;
if (sqrt(pow(dist.x, 2) + pow(dist.y*2, 2)) < r)
{
if (get_child(i)->tag == TERRAIN_DIRT)
{
delete get_children()->remove(i--);
}
hit = true;
}
}
// for screen update for destruction animation
ConsoleTools::clear_screen();
_parent->draw();
}
}
if (hit == true)
{
ConsoleTools::play_music("boom.wav");
}
_position.y = old_pos;
return hit;
}
void Terrain::_gen_terrain(int start, int length, int *height_map)
{
int amp = (rand() % get_size().y/5) + 5;
int period = (rand() % 40) + 10;
int height = (rand() % get_size().y/10) + 5;
int phase = rand() % get_size().x;
for (int i = start; i < length; ++i)
{
height_map[i] = std::abs(amp*sin(((float)i/(float)period) - phase) + height) + 5;
}
}
void Terrain::shuffle_wind_speed()
{
float wind = static_cast<float>(rand())/static_cast<float>(RAND_MAX);
int dir = rand() % 2;
_wind_speed = dir == 0 ? -wind : wind;
}
float Terrain::get_wind_speed() const
{
return _wind_speed;
}
void Terrain::update(int t)
{
// update children
LOOPV(i, this)
{
get_child(i)->update(t);
if (get_child(i)->should_remove_from_parent() == true)
{
delete get_children()->remove(i);
--i;
}
}
}
void Terrain::draw()
{
// draw children
LOOPV(i, this)
{
get_child(i)->draw();
}
}