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