]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/CEntity.cpp
Update copyrights to 2011.
[quix0rs-blobwars.git] / src / CEntity.cpp
1 /*
2 Copyright (C) 2004-2011 Parallel Realities
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20
21 #include "headers.h"
22
23 Entity::Entity()
24 {
25         name[0] = 0;
26         x = y = dx = dy = tx = ty = width = height = 0;
27         id = 0;
28         health = 0;
29         immune = 120;
30         environment = ENV_AIR;
31         value = 0;
32         oxygen = 7;
33         falling = false;
34         fuel = 7;
35         thinktime = baseThink = 0;
36         next = NULL;
37         owner = this;
38         face = 0;
39         reload = 0;
40         deathSound = -1;
41         flags = 0;
42         falling = false;
43         owner = this;
44         dead = DEAD_ALIVE;
45         referenced = false;
46
47         for (int i = 0 ; i < 3 ; i++)
48         {
49                 sprite[i] = NULL;
50         }
51 }
52
53 void Entity::setName(const char *name)
54 {
55         strlcpy(this->name, name, sizeof this->name);
56 }
57
58 void Entity::setSprites(Sprite *sprite1, Sprite *sprite2, Sprite *sprite3)
59 {
60         sprite[0] = sprite1;
61         sprite[1] = sprite2;
62         sprite[2] = sprite3;
63
64         currentFrame = 0;
65
66         if (sprite[0]->maxFrames > 0)
67         {
68                 currentFrame = Math::prand() % (sprite[0]->maxFrames + 1);
69         }
70
71         width = sprite[0]->image[0]->w;
72         height = sprite[0]->image[0]->h;
73
74         currentTime = 1;
75 }
76
77 void Entity::animate()
78 {
79         currentTime--;
80
81         if (currentTime == 0)
82         {
83                 currentFrame++;
84                 sprite[face]->getNextFrame(&currentFrame, &currentTime);
85         }
86 }
87
88 SDL_Surface *Entity::getFaceImage()
89 {
90         if ((health > 0) && (immune <= 120))
91         {
92                 return sprite[face]->image[currentFrame];
93         }
94
95         return sprite[2]->getCurrentFrame();
96 }
97
98 void Entity::place(int x, int y)
99 {
100         this->x = this->tx = x;
101         this->y = this->ty = y;
102 }
103
104 void Entity::setVelocity(float dx, float dy)
105 {
106         this->dx = dx;
107         this->dy = dy;
108 }
109
110 void Entity::move()
111 {
112         x += dx;
113         y += dy;
114 }
115
116 void Entity::setRandomVelocity()
117 {
118         dx = Math::rrand(-100, 100);
119         dx /= 50;
120
121         dy = Math::rrand(-15, 0);
122 }
123
124 void Entity::applyGravity()
125 {
126         if ((environment == ENV_AIR) && (!(flags & ENT_WEIGHTLESS)))
127                 dy += 0.5;
128         else
129                 dy = 1;
130
131         Math::limitFloat(&dy, -12, 12);
132 }
133
134 void Entity::checkEnvironment()
135 {
136         switch (environment)
137         {
138                 case ENV_AIR:
139                         Math::limitChar(&(oxygen += 2), 0, 7);
140                         break;
141                 case ENV_WATER:
142                         if (!(flags & ENT_SWIMS))
143                         {
144                                 Math::limitChar(&(oxygen -= 1), 0, 7);
145                                 if (oxygen == 0)
146                                 {
147                                         health--;
148                                         thinktime = 30;
149                                 }
150                         }
151                         break;
152                 case ENV_SLIME:
153                         if (immune == 0)
154                                 health--;
155                         break;
156                 case ENV_LAVA:
157                         if (immune == 0)
158                                 health -= 2;
159                         break;
160         }
161 }
162
163 void Entity::think()
164 {
165         if (immune > 0)
166         {
167                 immune--;
168
169                 if ((falling) && (immune == 121) && (environment == ENV_AIR))
170                 {
171                         immune = 122;
172                 }
173         }
174
175         Math::wrapInt(&(--thinktime), 0, baseThink);
176         Math::limitInt(&(--reload), 0, 255);
177
178         if (flags & ENT_DYING)
179         {
180                 health--;
181         }
182                 
183         if (flags & ENT_FLIES)
184         {
185                 if (thinktime == 0)
186                 {
187                         fuel--;
188                         if (fuel == 0)
189                         {
190                                 Math::removeBit(&flags, ENT_FLIES);
191                         }
192                 }
193         }
194         else
195         {
196                 if (thinktime == 0)
197                 {
198                         fuel++;
199                 }
200         }
201
202         Math::limitChar(&fuel, 0, 7);
203
204         if ((thinktime == 0) && (!(flags & ENT_INANIMATE)))
205         {
206                 checkEnvironment();
207         }
208 }