1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (c) 2013-2017, 2019-2020 Cong Xu
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
12 Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 POSSIBILITY OF SUCH DAMAGE.
27 */
28 #include "animation.h"
29
30 #include "defs.h"
31
32 static Animation animStanding = {
33 ACTORANIMATION_STAND, 0, {IDLEHEAD_NORMAL}, {1}, 0, false, true};
34 static Animation animIdling = {
35 ACTORANIMATION_IDLE,
36 0,
37 {IDLEHEAD_NORMAL, IDLEHEAD_LEFT, IDLEHEAD_RIGHT},
38 {90, 60, 60},
39 0,
40 true,
41 true};
42 static Animation animWalking = {ACTORANIMATION_WALKING,
43 0,
44 {0, 1, 2, 3, 4, 5, 6, 7},
45 {4, 4, 4, 4, 4, 4, 4, 4},
46 0,
47 false,
48 true};
49
AnimationGetActorAnimation(const ActorAnimation aa)50 Animation AnimationGetActorAnimation(const ActorAnimation aa)
51 {
52 switch (aa)
53 {
54 case ACTORANIMATION_STAND:
55 return animStanding;
56 case ACTORANIMATION_IDLE:
57 return animIdling;
58 case ACTORANIMATION_WALKING:
59 return animWalking;
60 default:
61 CASSERT(false, "Unknown actor animation");
62 return animIdling;
63 }
64 }
65
AnimationUpdate(Animation * a,const float ticks)66 void AnimationUpdate(Animation *a, const float ticks)
67 {
68 a->frameCounter += ticks;
69 a->newFrame = false;
70 if (a->frameCounter > a->ticksPerFrame[a->frame])
71 {
72 a->frameCounter -= a->ticksPerFrame[a->frame];
73 a->newFrame = true;
74 // Advance to next frame
75 if (a->randomFrames)
76 {
77 // If we're not on the first frame, return to first frame
78 // Otherwise, pick a random non-first frame
79 if (a->frame == 0)
80 {
81 // Note: -1 means frame not used, so pick another frame
82 do
83 {
84 a->frame = (rand() % (ANIMATION_MAX_FRAMES - 1)) + 1;
85 } while (a->ticksPerFrame[a->frame] < 0);
86 }
87 else
88 {
89 a->frame = 0;
90 }
91 }
92 else
93 {
94 a->frame++;
95 if (a->frame >= ANIMATION_MAX_FRAMES ||
96 a->ticksPerFrame[a->frame] < 0)
97 {
98 a->frame = 0;
99 }
100 }
101 }
102 }
AnimationGetFrame(const Animation * a)103 int AnimationGetFrame(const Animation *a)
104 {
105 return a->frames[a->frame];
106 }
107