Debian Codegraph @

src/cdogs/thing.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4
5 You should have received a copy of the GNU General Public License
6 along with this program; if not, write to the Free Software
7 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
8
9 This file incorporates work covered by the following copyright and
10 permission notice:
11
12 Copyright (c) 2013-2014, 2016-2019 Cong Xu
13 All rights reserved.
14
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions are met:
17
18 Redistributions of source code must retain the above copyright notice, this
19 list of conditions and the following disclaimer.
20 Redistributions in binary form must reproduce the above copyright notice,
21 this list of conditions and the following disclaimer in the documentation
22 and/or other materials provided with the distribution.
23
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
28 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 POSSIBILITY OF SUCH DAMAGE.
35 */
36 #include "thing.h"
37
38 #include "actors.h"
39 #include "net_util.h"
40 #include "objs.h"
41 #include "pickup.h"
42 #include "tile.h"
43
44 #define DRAW_SHAKE_MAX 2.0f
45 #define DRAW_SHAKE_FACTOR 0.3f
46 #define DRAW_SHAKE_DECAY 0.8f
47 #define ZERO_DRAW_SHAKE svec2(\
48 RAND_FLOAT(-DRAW_SHAKE_MAX, DRAW_SHAKE_MAX) * 0.7f,\
49 RAND_FLOAT(-DRAW_SHAKE_MAX, DRAW_SHAKE_MAX) * 0.7f)
50
51
52 bool IsThingInsideTile(const Thing *i, const struct vec2i tilePos)
53 {
54 return
55 i->Pos.x - i->size.x / 2 >= tilePos.x * TILE_WIDTH &&
56 i->Pos.x + i->size.x / 2 < (tilePos.x + 1) * TILE_WIDTH &&
57 i->Pos.y - i->size.y / 2 >= tilePos.y * TILE_HEIGHT &&
58 i->Pos.y + i->size.y / 2 < (tilePos.y + 1) * TILE_HEIGHT;
59 }
60
61
62 void ThingInit(
63 Thing *t, const int id, const ThingKind kind, const struct vec2i size,
64 const int flags)
65 {
66 memset(t, 0, sizeof *t);
67 t->id = id;
68 t->kind = kind;
69 t->size = size;
70 t->flags = flags;
71 // Ininitalise pos
72 t->Pos = svec2(-1, -1);
73 }
74
75 void ThingUpdate(Thing *t, const int ticks)
76 {
77 t->SoundLock = MAX(0, t->SoundLock - ticks);
78 for (int i = 0; i < ticks; i++)
79 {
80 t->drawShake = svec2_scale(t->drawShake, -DRAW_SHAKE_DECAY);
81 if (svec2_length_squared(t->drawShake) < 1.0f)
82 {
83 t->drawShake = svec2_zero();
84 break;
85 }
86 }
87 CPicUpdate(&t->CPic, ticks);
88 }
89
90 void ThingAddDrawShake(Thing *t, const struct vec2 shake)
91 {
92 if (svec2_is_zero(shake))
93 {
94 t->drawShake = ZERO_DRAW_SHAKE;
95 }
96 else
97 {
98 t->drawShake = svec2_clamp(
99 svec2_add(t->drawShake, svec2_scale(shake, DRAW_SHAKE_FACTOR)),
100 svec2(-DRAW_SHAKE_MAX, -DRAW_SHAKE_MAX),
101 svec2(DRAW_SHAKE_MAX, DRAW_SHAKE_MAX));
102 }
103 }
104
105 void ThingDamage(const NThingDamage d)
106 {
107 Thing *ti = ThingGetByUID(d.Kind, d.UID);
108 switch (d.Kind)
109 {
110 case KIND_CHARACTER:
111 ActorHit(d);
112 break;
113 case KIND_OBJECT:
114 DamageObject(d);
115 break;
116 default:
117 // do nothing
118 break;
119 }
120 if (ti != NULL && d.Power > 0)
121 {
122 ThingAddDrawShake(ti, svec2_scale(NetToVec2(d.Vel), d.Mass));
123 }
124 }
125
126
127 Thing *ThingGetByUID(const ThingKind kind, const int uid)
128 {
129 switch (kind)
130 {
131 case KIND_CHARACTER:
132 return &ActorGetByUID(uid)->thing;
133 case KIND_OBJECT:
134 return &ObjGetByUID(uid)->thing;
135 default:
136 return NULL;
137 }
138 }
139
140
141 Thing *ThingIdGetThing(const ThingId *tid)
142 {
143 Thing *ti = NULL;
144 switch (tid->Kind)
145 {
146 case KIND_CHARACTER:
147 ti = &((TActor *)CArrayGet(&gActors, tid->Id))->thing;
148 break;
149 case KIND_PARTICLE:
150 ti = &((Particle *)CArrayGet(&gParticles, tid->Id))->thing;
151 break;
152 case KIND_MOBILEOBJECT:
153 ti = &((TMobileObject *)CArrayGet(
154 &gMobObjs, tid->Id))->thing;
155 break;
156 case KIND_OBJECT:
157 ti = &((TObject *)CArrayGet(&gObjs, tid->Id))->thing;
158 break;
159 case KIND_PICKUP:
160 ti = &((Pickup *)CArrayGet(&gPickups, tid->Id))->thing;
161 break;
162 default:
163 CASSERT(false, "unknown tile item to get");
164 break;
165 }
166 return ti;
167 }
168
169 bool ThingDrawBelow(const Thing *t)
170 {
171 return t->flags & THING_DRAW_BELOW;
172 }
173 bool ThingDrawAbove(const Thing *t)
174 {
175 return t->flags & THING_DRAW_ABOVE;
176 }