Debian Codegraph @

engine/particle/chunkparticle.cpp

raw
1 // ____ _ __
2 // / __ )____ _____ | | / /___ ___________
3 // / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/
4 // / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ )
5 // /_____/\____/____/ |__/|__/\__,_/_/ /____/
6 //
7 // A futuristic real-time strategy game.
8 // This file is part of Bos Wars.
9 //
10 /**@name chunkparticle.cpp - The chunk particle. */
11 //
12 // (c) Copyright 2007-2008 by Jimmy Salmon and Francois Beerten
13 //
14 // This program is free software; you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation; only version 2 of the License.
17 //
18 // This program is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 // 02111-1307, USA.
27 //
28
29 //@{
30
31 #include <math.h>
32
33 #include "stratagus.h"
34 #include "particle.h"
35 #include "video.h"
36
37
38 static const int gravity = 32 * 12;
39
40 static inline float deg2rad(int degrees) {
41 return degrees * (3.1415926535f / 180);
42 }
43
44
45 CChunkParticle::CChunkParticle(CPosition position, Animation *smokeAnimation) :
46 CParticle(position), initialPos(position), nextSmokeTicks(0), age(0),
47 height(0.f)
48 {
49 float radians = deg2rad(MyRand() % 360);
50 direction.x = cos(radians);
51 direction.y = sin(radians);
52
53 const int maxVelocity = 400;
54 initialVelocity = MyRand() % maxVelocity;
55
56 int minTrajectoryAngle = 77;
57 trajectoryAngle = deg2rad(MyRand() % (90 - minTrajectoryAngle) + minTrajectoryAngle);
58
59 lifetime = (int)(1000 * (initialVelocity * sin(trajectoryAngle) / gravity) * 2);
60 this->smokeAnimation = smokeAnimation->clone();
61 }
62
63 CChunkParticle::~CChunkParticle()
64 {
65 delete smokeAnimation;
66 }
67
68 static float calculateScreenPos(float posy, float height)
69 {
70 return posy - height * 0.2f;
71 }
72
73 void CChunkParticle::draw()
74 {
75 CPosition screenPos = ParticleManager.getScreenPos(pos);
76 Uint32 color = ColorBlack;
77
78 Video.DrawRectangleClip(color, (int)screenPos.x - 1, (int)calculateScreenPos(screenPos.y, height) - 1, 2, 2);
79 }
80
81 static float getHorizontalPosition(int initialVelocity, float trajectoryAngle, float time)
82 {
83 return (initialVelocity * cos(trajectoryAngle)) * time;
84 }
85
86 static float getVerticalPosition(int initialVelocity, float trajectoryAngle, float time)
87 {
88 return (initialVelocity * sin(trajectoryAngle)) * time - (gravity / 2.0f) * (time * time);
89 }
90
91 void CChunkParticle::update(int ticks)
92 {
93 age += ticks;
94 if (age >= lifetime) {
95 destroy();
96 return;
97 }
98
99 const int minSmokeTicks = 150;
100 const int randSmokeTicks = 50;
101
102 if (age > nextSmokeTicks) {
103 CPosition p(pos.x, calculateScreenPos(pos.y, height));
104 Animation *animation = smokeAnimation->clone();
105 CSmokeParticle *smoke = new CSmokeParticle(p, animation);
106 ParticleManager.add(smoke);
107
108 nextSmokeTicks += MyRand() % randSmokeTicks + minSmokeTicks;
109 }
110
111 float time = age / 1000.f;
112
113 float distance = getHorizontalPosition(initialVelocity, trajectoryAngle, time);
114 pos.x = initialPos.x + distance * direction.x;
115 pos.y = initialPos.y + distance * direction.y;
116
117 height = getVerticalPosition(initialVelocity, trajectoryAngle, time);
118 }
119
120
121 CParticle* CChunkParticle::clone()
122 {
123 return new CChunkParticle(pos, smokeAnimation);
124 }
125
126 //@}