1 | |
---|
2 | |
---|
3 | /* |
---|
4 | orxonox - the future of 3D-vertical-scrollers |
---|
5 | |
---|
6 | Copyright (C) 2004 orx |
---|
7 | |
---|
8 | This program is free software; you can redistribute it and/or modify |
---|
9 | it under the terms of the GNU General Public License as published by |
---|
10 | the Free Software Foundation; either version 2, or (at your option) |
---|
11 | any later version. |
---|
12 | |
---|
13 | ### File Specific: |
---|
14 | main-programmer: Patrick Boenzli |
---|
15 | co-programmer: Benjamin Grauer |
---|
16 | */ |
---|
17 | |
---|
18 | |
---|
19 | #include "shoot_rocket.h" |
---|
20 | |
---|
21 | #include <iostream> |
---|
22 | |
---|
23 | |
---|
24 | using namespace std; |
---|
25 | |
---|
26 | |
---|
27 | |
---|
28 | ShootRocket::ShootRocket () |
---|
29 | { |
---|
30 | lastShoot = null; |
---|
31 | step = 1.0; |
---|
32 | inhibitor =0; |
---|
33 | } |
---|
34 | |
---|
35 | |
---|
36 | ShootRocket::~ShootRocket () {} |
---|
37 | |
---|
38 | |
---|
39 | void ShootRocket::drawShoot() |
---|
40 | { |
---|
41 | |
---|
42 | //cout << "ShootRocket::drawShoot" << endl; |
---|
43 | /* now draw all the shoots (many) */ |
---|
44 | shoot* tmpShoot = lastShoot; |
---|
45 | shoot* lastRef = null; |
---|
46 | while( tmpShoot != null ) |
---|
47 | { |
---|
48 | glPushMatrix(); |
---|
49 | glTranslatef(tmpShoot->xCor, tmpShoot->yCor, tmpShoot->zCor); |
---|
50 | switch (tmpShoot->type) |
---|
51 | { |
---|
52 | case BACKPARABLE: |
---|
53 | tmpShoot->xCor+=tmpShoot->xVel; |
---|
54 | tmpShoot->yCor+=tmpShoot->yVel; |
---|
55 | tmpShoot->xVel+=tmpShoot->xAcc; |
---|
56 | tmpShoot->yVel+=tmpShoot->yAcc; |
---|
57 | break; |
---|
58 | |
---|
59 | case SIDEACC: |
---|
60 | if (tmpShoot->xVel >= .01 || tmpShoot->xVel <= -.01) |
---|
61 | { |
---|
62 | tmpShoot->xCor+=tmpShoot->xVel; |
---|
63 | tmpShoot->yCor+=tmpShoot->yVel; |
---|
64 | tmpShoot->xVel*=tmpShoot->xAcc; |
---|
65 | } |
---|
66 | else |
---|
67 | { |
---|
68 | tmpShoot->xCor+=tmpShoot->xVel; |
---|
69 | tmpShoot->yCor+=tmpShoot->yVel; |
---|
70 | tmpShoot->yVel+=tmpShoot->yVel*tmpShoot->yAcc; |
---|
71 | } |
---|
72 | break; |
---|
73 | } |
---|
74 | |
---|
75 | glScalef(0.1, 0.1, 0.1); |
---|
76 | glutWireCube(1.0); |
---|
77 | glPopMatrix(); |
---|
78 | |
---|
79 | /* garbage collection: look if shoot is outside world */ |
---|
80 | /* fix1: weak boundaries check all four sides */ |
---|
81 | /* fix2: conditions, that a struct tree can be cut off */ |
---|
82 | /* fix3: more efficent and nicer please */ |
---|
83 | if (tmpShoot->yCor - DataTank::yOffset > 20) |
---|
84 | { |
---|
85 | /* normal case: delete one element, link the others */ |
---|
86 | if (lastRef != null) |
---|
87 | { |
---|
88 | //cout << "garbage collection" << endl; |
---|
89 | lastRef->next = tmpShoot->next; |
---|
90 | delete tmpShoot; |
---|
91 | tmpShoot = lastRef->next; |
---|
92 | //cout << "garbage collection left" << endl; |
---|
93 | } |
---|
94 | else |
---|
95 | { |
---|
96 | /* special case: first element to be processed */ |
---|
97 | //cout << "garbage collecton: first el in queue" << endl; |
---|
98 | lastRef = tmpShoot->next; |
---|
99 | delete tmpShoot; |
---|
100 | tmpShoot = lastRef; |
---|
101 | lastShoot = tmpShoot; |
---|
102 | if (tmpShoot != null) |
---|
103 | { |
---|
104 | tmpShoot = tmpShoot->next; |
---|
105 | //cout << "noch nich null" << endl; |
---|
106 | } |
---|
107 | else |
---|
108 | { |
---|
109 | lastRef = null; |
---|
110 | tmpShoot = null; |
---|
111 | lastShoot = null; |
---|
112 | //cout << "endl null" << endl; |
---|
113 | } |
---|
114 | |
---|
115 | //cout << "garbage collection: firtst el in queue left" << endl; |
---|
116 | } |
---|
117 | } |
---|
118 | else |
---|
119 | { |
---|
120 | lastRef = tmpShoot; |
---|
121 | tmpShoot = tmpShoot->next; |
---|
122 | } |
---|
123 | } |
---|
124 | //cout << "ShootRocket::drawShoot - finished" << endl; |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | void ShootRocket::addShoot(shoot* sh) |
---|
129 | { |
---|
130 | sh->next = null; |
---|
131 | lastShoot = sh; |
---|
132 | } |
---|
133 | |
---|
134 | void ShootRocket::addBackParable(float x, float y, float z) |
---|
135 | { |
---|
136 | inhibitor++; |
---|
137 | if (inhibitor >= 10) |
---|
138 | { |
---|
139 | //cout << "ShootRocket::addShoot" << endl; |
---|
140 | shoot* sh = new shoot; |
---|
141 | sh->type = BACKPARABLE; |
---|
142 | sh->xCor = x; sh->yCor = y; sh->zCor = z; |
---|
143 | sh->xVel = (-1+(float)random() / 1000000000/step)/5; |
---|
144 | sh->yVel = -.3; |
---|
145 | sh->zVel = (1-(float)random() / 1000000000/step)/5; |
---|
146 | sh->xAcc = 0; sh->yAcc = .01/step; sh->zAcc = 0; |
---|
147 | sh->next = lastShoot; |
---|
148 | lastShoot = sh; |
---|
149 | inhibitor =0; |
---|
150 | } |
---|
151 | } |
---|
152 | void ShootRocket::addSideAcc(float x, float y, float z, enum RocketDirection direction) |
---|
153 | { |
---|
154 | inhibitor++; |
---|
155 | if (inhibitor >= 10) |
---|
156 | { |
---|
157 | //cout << "ShootRocket::addShoot" << endl; |
---|
158 | shoot* sh = new shoot; |
---|
159 | sh->type = SIDEACC; |
---|
160 | sh->xCor = x; sh->yCor = y; sh->zCor = z; |
---|
161 | switch (direction) |
---|
162 | { |
---|
163 | case LEFT: |
---|
164 | sh->xVel = -.5; sh->yVel = .05; sh->zVel = 0; |
---|
165 | break; |
---|
166 | case RIGHT: |
---|
167 | sh->xVel = .5; sh->yVel = .05; sh->zVel = 0; |
---|
168 | break; |
---|
169 | } |
---|
170 | sh->xAcc = .9; sh->yAcc = .02; sh->zAcc = 0; |
---|
171 | sh->next = lastShoot; |
---|
172 | lastShoot = sh; |
---|
173 | inhibitor =0; |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | |
---|
178 | void ShootRocket::setShootStep(float step) |
---|
179 | { |
---|
180 | cout << "ShootRocket::setShootStep to " << step << endl; |
---|
181 | this->step = step; |
---|
182 | } |
---|
183 | |
---|
184 | |
---|
185 | /* Exterminate shoot from game, implement this method */ |
---|
186 | /* if you like to add animatiion */ |
---|
187 | void ShootRocket::removeShoot(shoot* sh) |
---|
188 | { |
---|
189 | glPushMatrix(); |
---|
190 | glTranslatef(sh->xCor, sh->yCor, sh->zCor); |
---|
191 | glutWireCube(1.0); |
---|
192 | glPopMatrix(); |
---|
193 | } |
---|