-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaserGun.cpp
166 lines (148 loc) · 4.75 KB
/
laserGun.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
#include "headerFile.h"
void createGun(float x, float y, float w, float h, float angle, COLOR color) {
w = w/2;
h = h/2;
GLfloat vertex_buffer_data [] = {
-w,-h,0, // vertex 1
-w,h,0, // vertex 2
w,h,0, // vertex 3
w,h,0, // vertex 3
w,-h,0, // vertex 4
-w,-h,0 // vertex 1
};
GLfloat color_buffer_data [] = {
// color for respective vertices;
color.r, color.g, color.b,
color.r, color.g, color.b,
color.r, color.g, color.b,
color.r, color.g, color.b,
color.r, color.g, color.b,
color.r, color.g, color.b,
};
VAO * object = create3DObject(GL_TRIANGLES, 6, vertex_buffer_data, color_buffer_data, GL_FILL);
entity gun = {};
gun.x = x;
gun.y = y;
gun.object = object;
gun.width = 2*w;
gun.height = 2*h;
gun.color = color;
gun.rotation_dir = 0;
gun.angle = angle;
gun.status = 0;
gun.drag_status = 0;
gun.up_translation_status = 0;
gun.down_translation_status = 0;
gun.rotation_speed = 1;
LaserObject["gun"] = gun;
}
void createHolder (float x, float y, float r, int parts, COLOR color) {
GLfloat vertex_buffer_data[parts*9];
GLfloat color_buffer_data[parts*9];
int i,j;
float angle=(2*M_PI/parts);
float current_angle = 0;
for(i=0;i<parts;i++){
for(j=0;j<3;j++){
color_buffer_data[i*9+j*3]=color.r;
color_buffer_data[i*9+j*3+1]=color.g;
color_buffer_data[i*9+j*3+2]=color.b;
}
vertex_buffer_data[i*9]=0;
vertex_buffer_data[i*9+1]=0;
vertex_buffer_data[i*9+2]=0;
vertex_buffer_data[i*9+3]=r*cos(current_angle);
vertex_buffer_data[i*9+4]=r*sin(current_angle);
vertex_buffer_data[i*9+5]=0;
vertex_buffer_data[i*9+6]=r*cos(current_angle+angle);
vertex_buffer_data[i*9+7]=r*sin(current_angle+angle);
vertex_buffer_data[i*9+8]=0;
current_angle+=angle;
}
VAO * object = create3DObject(GL_TRIANGLES, (parts*9)/3, vertex_buffer_data, color_buffer_data, GL_FILL);
entity holder = {};
holder.x = x;
holder.y = y;
holder.radius = r;
holder.color = color;
holder.object = object;
holder.drag_status = 0;
holder.up_translation_status = 0;
holder.down_translation_status = 0;
LaserObject["holder"] = holder;
}
void createLaser(float x, float y, float w, float h, COLOR color) {
w = w/2;
h = h/2;
// float a = x-w, b = y+h, a2 = x+w, b2 = y-h;
/*GLfloat vertex_buffer_data [] = {
x, y, 0,
a, b, 0,
a2, y, 0,
a2, y, 0,
a, b2, 0,
x, y, 0
};*/
GLfloat vertex_buffer_data [] = {
-w,-h,0, // vertex 1
-w,h,0, // vertex 2
w,h,0, // vertex 3
w,h,0, // vertex 3
w,-h,0, // vertex 4
-w,-h,0 // vertex 1
};
GLfloat color_buffer_data [] = {
// color for respective vertices;
color.r, color.g, color.b,
color.r, color.g, color.b,
color.r, color.g, color.b,
color.r, color.g, color.b,
color.r, color.g, color.b,
color.r, color.g, color.b,
};
VAO * object = create3DObject(GL_TRIANGLES, 6, vertex_buffer_data, color_buffer_data, GL_FILL);
entity laser = {};
laser.x = x;
laser.y = y;
laser.width = 2*w;
laser.height = 2*h;
laser.color= color;
laser.xspeed = 0.1;
laser.yspeed = 0.1;
laser.angle = LaserObject["gun"].angle;
laser.object = object;
laser.status = 1;
laser.hit_time = glfwGetTime();
Laser.push_back(laser);
for (auto i = Laser.begin(); i != Laser.end(); i++) if (!(*i).status) Laser.erase(i);
}
void laserGunEngine() {
createGun(-4, 0.0, 1.0, 0.2, 0, grey);
createHolder(-4.0, 0.0, 0.25, 100000, grey);
}
void laserEngine() {
current_laser_time = glfwGetTime();
if (current_laser_time - prev_laser_time > 0.7) {
ISoundEngine* engine = createIrrKlangDevice();
if (!engine)
{
printf("Could not startup engine\n");
}
// To play a sound, we only to call play2D(). The second parameter
// tells the engine to play it looped.
// play some sound stream, looped
engine->play2D("irrKlang-64bit-1.5.0/media/blast.wav", false);
float x = LaserObject["gun"].x + (LaserObject["gun"].width/2.0)*cos(LaserObject["gun"].angle*M_PI/180.0f);
float y = LaserObject["gun"].y + (LaserObject["gun"].height*2.5)*sin(LaserObject["gun"].angle*M_PI/180.0f);
createLaser(x, y, 0.2, 0.05, coingold);
prev_laser_time = glfwGetTime();
}
}
void dragLaser() {
float width_diff_gun = x_mouse - LaserObject["gun"].x;
float height_diff_gun = y_mouse - LaserObject["gun"].y;
float width_diff_holder = x_mouse - LaserObject["holder"].x;
float height_diff_holder = y_mouse - LaserObject["holder"].y;
if (abs(width_diff_gun) < LaserObject["gun"].width/2.0 && abs(height_diff_gun) < LaserObject["gun"].height/2.0) LaserObject["gun"].drag_status = 1;
else if (abs(width_diff_holder) < LaserObject["holder"].width/2.0 && abs(height_diff_holder) < LaserObject["holder"].height/2.0) LaserObject["holder"].drag_status = 1;
}