forked from snoutmate/Sea-Defender
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboat.cpp
More file actions
161 lines (130 loc) · 3.7 KB
/
boat.cpp
File metadata and controls
161 lines (130 loc) · 3.7 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
158
159
160
161
#ifdef WIN32
# include <windows.h>
#endif
#include <GL/glfw.h>
#include "boat.h"
#include "background.h"
#include "resources.h"
#include "snoutlib/mfont.h"
#include "snoutlib/timer.h"
#include "game.h"
#include "fx_ship_expl.h"
#include "data/text/shipnames.h"
#include "botinterface.h"
extern BotInterface *g_bot_interface;
int Boat::m_names_used[];
Boat::Boat(vec2 pos,int index,float scale) :
m_pos(pos),m_scale(scale),
m_pos_shift(0.0),m_rot(0.0),
m_damaged(false),m_sinking(false), m_obb(AABB(0,0,0,0))
{
m_model = &g_resources.mesh_tanker;
m_name = randname(index);
m_smoke = NULL;
for(int i=0;i<m_num_bubble_streams;++i)
m_bbl[i] = NULL;
}
Boat::~Boat()
{
delete m_smoke;
for(int i=0;i<m_num_bubble_streams;++i)
delete m_bbl[i];
}
string Boat::randname(int idx) {
if(idx == 0) {
int max_idx = 0;
while(ship_names[max_idx])
max_idx++;
m_names_used[0] = m_names_used[1] = m_names_used[2] = rand() % max_idx;
do {
m_names_used[1] = rand() % max_idx;
} while(m_names_used[0] == m_names_used[1]);
do {
m_names_used[2] = rand() % max_idx;
} while(m_names_used[2] == m_names_used[0] || m_names_used[2] == m_names_used[1]);
}
return string(ship_names[m_names_used[idx]]);
}
void Boat::hit(float x)
{
float hit_pos = x - m_pos[0];
hit_pos = clamp<float>(hit_pos,-0.15,+0.15);
if (!m_damaged) {
m_damaged = true;
m_smoke = new PE_Smoke(vec2(hit_pos,0.0));
m_hit_pos = hit_pos;
g_bot_interface->async_send(g_timer->now(), "boat_damaged", m_pos);
} else if (!m_sinking) {
m_sinking = true;
m_sinking_start_time = g_timer->now();
m_sinking_start_pos = m_pos_shift;
m_sinking_start_rot = m_rot;
for(int i=0;i<m_num_bubble_streams;++i) {
m_bbl[i] = new PE_Bubbles(vec2(0.0,0.0));
m_bbl_emitpos_x[i] = ((float)i/m_num_bubble_streams)+0.1-0.5 + (RAND_0_1 - 0.5)*0.175;
}
g_bot_interface->async_send(g_timer->now(), "boat_sunk", m_pos);
}
vec2 expl_pos = vec2(m_pos[0]+hit_pos,m_pos[1]+m_pos_shift-0.00);
g_current_game->add_effect(new FX_Ship_Explosion(expl_pos),0);
}
void Boat::draw(void)
{
if (m_sinking) {
float delta_t = g_timer->now() - m_sinking_start_time;
if (delta_t>25.0) // fully sunk
return;
// slow start
if (delta_t<1.0) {
m_pos_shift = m_sinking_start_pos + 0.03*delta_t*delta_t;
m_rot = m_sinking_start_rot + m_sinking_start_rot*5.0*delta_t*delta_t;
} else {
m_pos_shift = m_sinking_start_pos + 0.05*delta_t + 0.03 - 0.05;
m_rot = m_sinking_start_rot + m_sinking_start_rot*5.0*delta_t;
}
if (delta_t>0.5)
m_smoke->stop_emitting();
} else {
m_pos_shift = Sea::sea_func(m_pos[0]-0.05f) * 0.35f; // pos
m_rot = Sea::sea_func(m_pos[0]) * 0.2f * 333; // rot
}
// draw smoke
if (m_damaged) {
m_smoke->set_pos(vec2(m_pos[0]+m_hit_pos,m_pos[1]+m_pos_shift-0.01));
m_smoke->update();
m_smoke->draw();
}
glPushMatrix();
glTranslatef(m_pos[0],m_pos[1]+m_pos_shift,0.0);
glScalef(m_scale,m_scale,1.0f);
glRotatef(m_rot,0,0,1);
// draw bubbles
if (m_sinking) {
mat4 mt = glGetCurrentMatrix(GL_MODELVIEW_MATRIX);
for(int i=0;i<m_num_bubble_streams;++i) {
vec2 pos = vec2(transformPoint(mt,vec3(m_bbl_emitpos_x[i],0.0,0.0)));
m_bbl[i]->set_pos(pos);
m_bbl[i]->update();
m_bbl[i]->draw();
}
}
// update boundingbox
m_obb = OBB((*m_model)->m_aabb);
// draw model
glDisable(GL_TEXTURE_2D);
glColor3f(0,0,0);
(*m_model)->draw();
// draw name
float text_scale = 0.8f;
float text_pos = 0.4f - g_resources.font->size_of_text(m_name.c_str(),text_scale);
g_resources.font->print_text(m_name.c_str(),vec2(text_pos,0.0),text_scale,false,vec4(1.0f,1.0f,1.0f,1.0f));
glPopMatrix();
}
bool Boat::is_sinking(void)
{
return m_sinking;
}
bool Boat::is_alive(void)
{
return !m_sinking;
}