1 /*
2 Copyright (C) 2007, 2010 - Bit-Blot
3 
4 This file is part of Aquaria.
5 
6 Aquaria is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 
15 See the GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 */
21 #include "Web.h"
22 #include "DSQ.h"
23 #include "Game.h"
24 #include "Avatar.h"
25 
26 Web::Webs Web::webs;
27 
Web()28 Web::Web() : RenderObject()
29 {
30 	addType(SCO_WEB);
31 	webs.push_back(this);
32 	cull = false;
33 	parentEntity = 0;
34 	existence = 0;
35 }
36 
setParentEntity(Entity * e)37 void Web::setParentEntity(Entity *e)
38 {
39 	parentEntity = e;
40 }
41 
killAllWebs()42 void Web::killAllWebs()
43 {
44 	std::queue<Web*>shotDeleteQueue;
45 	for (Webs::iterator i = webs.begin(); i != webs.end(); i++)
46 	{
47 		shotDeleteQueue.push(*i);
48 	}
49 	Web *s = 0;
50 	while (!shotDeleteQueue.empty())
51 	{
52 		s = shotDeleteQueue.front();
53 		if (s)
54 		{
55 			s->safeKill();
56 		}
57 		shotDeleteQueue.pop();
58 	}
59 	webs.clear();
60 }
61 
onEndOfLife()62 void Web::onEndOfLife()
63 {
64 	RenderObject::onEndOfLife();
65 	webs.remove(this);
66 }
67 
setExistence(float t)68 void Web::setExistence(float t)
69 {
70 	existence = t;
71 }
72 
addPoint(const Vector & point)73 int Web::addPoint(const Vector &point)
74 {
75 	points.push_back(point);
76 	return points.size()-1;
77 }
78 
setPoint(int pt,const Vector & v)79 void Web::setPoint(int pt, const Vector &v)
80 {
81 	if (pt < 0 || pt >= points.size()) return;
82 	points[pt] = v;
83 }
84 
getPoint(int pt) const85 Vector Web::getPoint(int pt) const
86 {
87 	Vector v;
88 	if (pt >= 0 || pt < points.size())
89 		v = points[pt];
90 	return v;
91 }
92 
getNumPoints()93 int Web::getNumPoints()
94 {
95 	return points.size();
96 }
97 
onUpdate(float dt)98 void Web::onUpdate(float dt)
99 {
100 	RenderObject::onUpdate(dt);
101 
102 	if (!dsq->game->isPaused())
103 	{
104 		if (existence > 0)
105 		{
106 			existence -= dt;
107 			if (existence <= 0)
108 			{
109 				existence = 0;
110 				setLife(1);
111 				setDecayRate(1);
112 				fadeAlphaWithLife = 1;
113 				dsq->sound->playSfx("spiderweb");
114 			}
115 		}
116 
117 		if (dsq->game->avatar && dsq->game->avatar->isInputEnabled())
118 		{
119 			FOR_ENTITIES(i)
120 			{
121 				bool hit = false;
122 				Entity *e = (*i);
123 				if ((e->getEntityType() == ET_ENEMY || e->getEntityType() == ET_AVATAR) && e->isDamageTarget(DT_ENEMY_WEB))
124 				{
125 					if (parentEntity != e)
126 					{
127 						for (int j = 0; j < points.size()-1; j++)
128 						{
129 							if (game->collideCircleVsLine(e, points[j], points[j+1], 4))
130 							{
131 								hit = true;
132 								break;
133 							}
134 						}
135 					}
136 				}
137 				if (hit)
138 				{
139 					/*
140 					if (!e->vel.isZero())
141 					{
142 						Vector n = e->vel;
143 						n.setLength2D(e->getv(EV_WEBSLOW)*dt);
144 						e->vel -= n;
145 					}
146 					*/
147 					e->vel /= float(e->getv(EV_WEBSLOW)) * dt;
148 				}
149 			}
150 		}
151 	}
152 }
153 
onRender()154 void Web::onRender()
155 {
156 #ifdef BBGE_BUILD_OPENGL
157 	glBindTexture(GL_TEXTURE_2D, 0);
158 	//glDisable(GL_BLEND);
159 
160 	glLineWidth(4);
161 	//glDisable(GL_CULL_FACE);
162 	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
163 
164 	glBegin(GL_LINES);
165 	for (int i = 0; i < points.size()-1; i++)
166 	{
167 
168 		glColor4f(1, 1, 1, 0.5f*alpha.x);
169 		glVertex3f(points[i].x, points[i].y, 0);
170 		glColor4f(1, 1, 1, 0.5f*alpha.x);
171 		glVertex3f(points[i+1].x, points[i+1].y, 0);
172 
173 	}
174 	glEnd();
175 #endif
176 }
177