1 /*************************************************************************
2 
3                          "I Have No Tomatoes"
4                   Copyright (c) 2004, Mika Halttunen
5 
6  This software is provided 'as-is', without any express or implied
7  warranty. In no event will the authors be held liable for any damages
8  arising from the use of this software.
9 
10  Permission is granted to anyone to use this software for any purpose,
11  including commercial applications, and to alter it and redistribute
12  it freely, subject to the following restrictions:
13 
14     1. The origin of this software must not be misrepresented; you must
15     not claim that you wrote the original software. If you use this
16     software in a product, an acknowledgment in the product documentation
17     would be appreciated but is not required.
18 
19     2. Altered source versions must be plainly marked as such, and must
20     not be misrepresented as being the original software.
21 
22     3. This notice may not be removed or altered from any source
23     distribution.
24 
25 
26  Mika Halttunen <lsoft@mbnet.fi>
27 
28 *************************************************************************/
29 
30 #include <stdio.h>
31 #include "SDL.h"
32 #include "SDL_opengl.h"
33 #include "SDL_image.h"
34 #include "texture.h"
35 #include "init.h"
36 #include "player.h"
37 #include "mymath.h"
38 #include "particle.h"
39 #include "trap.h"
40 #include "soundmusic.h"
41 
42 
43 // Trap list
44 list<TRAP> traplist;
45 
46 // Trap texture
47 GLuint trap_texture;
48 
49 // Trap particle
50 GLuint part_trap;
51 
52 // Trap time
53 const int trap_time = 900;
54 
55 
56 // Load the trap gfx
load_traps()57 void load_traps() {
58 	trap_texture = load_png("trap.png", true, false, false);
59 	part_trap = load_jpg("part_trap.jpg", false, false, true);
60 }
61 
62 
63 // Add a trap
add_trap(int x,int y,int owner)64 void add_trap(int x, int y, int owner) {
65 	TRAP nt;
66 	nt.clear();
67 	nt.x = x;
68 	nt.y = y;
69 	nt.owner = owner;
70 	nt.anim = RANDF(0,359);
71 	nt.alive = true;
72 
73 	traplist.push_back(nt);
74 }
75 
76 
77 // Animate the traps
move_traps()78 void move_traps() {
79 	if(traplist.size() == 0)
80 		return;
81 
82 	list<TRAP>::iterator i;
83 	for(i = traplist.begin(); i != traplist.end(); ++i) {
84 		(*i).move();
85 		// Remove the dead traps
86 		if((*i).alive == false) {
87 			i = traplist.erase(i);
88 		}
89 	}
90 
91 }
92 
93 
94 // Draw the traps
draw_traps()95 void draw_traps() {
96 	if(traplist.size() == 0)
97 		return;
98 
99 	glDepthMask(GL_FALSE);
100 	BIND_TEXTURE(trap_texture);
101 
102 	list<TRAP>::iterator i;
103 	for(i = traplist.begin(); i != traplist.end(); ++i) {
104 		(*i).draw();
105 	}
106 
107 	glDepthMask(GL_TRUE);
108 }
109 
110 
111 // Clear the traps
clear_traps()112 void clear_traps() {
113 	traplist.clear();
114 }
115 
116 
117 // Animate the trap
move()118 void TRAP::move() {
119 	// Animate
120 	anim += 0.1f;
121 	if((int)anim > 3)
122 		anim = 0.0f;
123 
124 	// Advance the time counter
125 	counter++;
126 	if(counter >= trap_time) {
127 		alive = false;
128 
129 		// Create a explosion
130 		for(int f=0; f<RAND(30,40); f++) {
131 			VECT pos(x + 0.5f, 0.1f, y + 0.5f);
132 			VECT dir;
133 			pos += VECT(RANDF(-0.35f,0.35f),0,RANDF(-0.35f,0.35f));
134 			dir.x = RANDF(-0.05f, 0.05f);
135 			dir.y = RANDF(-0.05f, 0.05f);
136 			dir.y = RANDF(0.04f, 0.08f);
137 			float c1[4] = { 1, 0.1f, 0.1f, 1 };
138 			float c2[4] = { 1, 0, 0, 0 };
139 			add_particle(pos, dir, RAND(20,80), 0.08f, 0.4f, c1, c2, part_trap);
140 		}
141 
142 		// Play the sound
143 		play_sound(SND_TRAP, false);
144 	}
145 
146 
147 	// Add some particles
148 	VECT pos(x + 0.5f, 0.1f, y + 0.5f);
149 	VECT dir;
150 	pos += VECT(RANDF(-0.35f,0.35f),0,RANDF(-0.35f,0.35f));
151 	dir.x = dir.z = 0.0f;
152 	dir.y = RANDF(0.04f, 0.07f);
153 	float c1[4] = { 1, 0.1f, 0.1f, 1 };
154 	float c2[4] = { 1, 0, 0, 0 };
155 	add_particle(pos, dir, RAND(10,60), 0.08f, 0.4f, c1, c2, part_trap);
156 
157 }
158 
159 
160 // Draw the trap
draw()161 void TRAP::draw() {
162 	// Translate to the position
163 	glPushMatrix();
164 	glTranslatef(x + 0.56f, 0.25f, y + 0.53f);
165 
166 	// Negate the camera rotation
167 	glMultMatrixf(cam_neg_matrix);
168 //	glRotatef(45.0f, 0,1,0);
169 //	glRotatef(-30.0f, 1,0,0);
170 
171 	// Draw the sprite
172 	int frame = (int)anim;
173 	const float siz = 0.45f;
174 
175 	glBegin(GL_TRIANGLE_STRIP);
176 		glTexCoord2f(0.25f * frame + 0.25f, 1);	glVertex3f( siz,  siz,  siz);
177 		glTexCoord2f(0.25f * frame, 1);			glVertex3f(-siz,  siz,  siz);
178 		glTexCoord2f(0.25f * frame + 0.25f, 0);	glVertex3f( siz, -siz, -siz);
179 		glTexCoord2f(0.25f * frame, 0);			glVertex3f(-siz, -siz, -siz);
180 	glEnd();
181 
182 	glPopMatrix();
183 }
184 
185 
186 // Clear the trap
clear()187 void TRAP::clear() {
188 	x = y = 0;
189 	owner = 0;
190 	anim = RANDF(0,3);
191 	alive = false;
192 	counter = 0;
193 }
194