ScummVM API documentation
sprite.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 /*
23  * This code is based on the CRAB engine
24  *
25  * Copyright (c) Arvind Raja Yadav
26  *
27  * Licensed under MIT
28  *
29  */
30 
31 #ifndef CRAB_SPRITE_H
32 #define CRAB_SPRITE_H
33 
34 #include "crab/collision.h"
35 #include "crab/timer.h"
36 #include "crab/ai/spriteai.h"
37 #include "crab/ai/SpriteConstant.h"
38 #include "crab/animation/animset.h"
39 #include "crab/animation/PopUp.h"
40 #include "crab/level/LevelExit.h"
41 
42 namespace Crab {
43 
44 class PathfindingAgent;
45 
46 namespace pyrodactyl {
47 namespace anim {
48 
49 class Sprite {
50 protected:
51  // Used to sync sprite to character
52  Common::String _id;
53 
54  // The position of the sprite
55  Vector2i _pos;
56 
57  // The velocity of the sprite, target velocity is our maximum velocity
58  Vector2f _vel, _target;
59 
60  // The image of the sprite and it's dimensions
61  ImageKey _image;
62  Vector2i _imgSize;
63 
64  // Clip is the portion of the sprite map to be drawn
65  Rect _clip;
66 
67  // The hit boxes of the character - v is vulnerable hit box, d is damage hit box
68  Rect _boxV, _boxD;
69 
70  // The direction the sprite is facing
71  Direction _dir;
72 
73  // The currently playing image effect
74  ImageEffect _imgEff;
75 
76  // The complete animation set for the sprite
77  AnimSet _animSet;
78 
79  // The conditions for sprite visibility
81 
82  // Current sprite combo and input for the sprite
84 
85  // Have we done damage for this frame - used to avoid repeated damage for the same frame
86  bool _damageDone;
87 
88  // Dialog shown without events
89  PopUpCollection _popup;
90 
91 protected:
92  void resetFrame(const pyrodactyl::people::PersonState &pst);
93  bool fightCollide(Rect hitbox, Rect enemy_bounds, Range &range, const pyrodactyl::ai::SpriteConstant &sc);
94  bool damageValid(Sprite &s, const pyrodactyl::ai::SpriteConstant &sc);
95 
96  void clip(const Rect &rect) { _clip = rect; }
97  void boxV(const Rect &rect) { _boxV = rect; }
98  void boxD(const Rect &rect) { _boxD = rect; }
99 
100 public:
101  // The AI data for the sprite
103  PathfindingAgent _pathing;
104 
105  // The modifier applied to the sprite velocity
106  Vector2f _velMod;
107 
108  // The layer associated with the sprite (used to show/hide sprite according to auto hide layers)
109  int _layer;
110 
111  // Is the mouse hovering over this sprite?
112  bool _hover;
113 
114  // The list of collisions currently taking place with the sprite
115  Common::List<CollisionData> _collideData;
116 
117  Sprite();
118 
119  ~Sprite() {
120  _pathing.reset();
121  }
122 
123  void visible(bool val) {
124  _visible.result(val);
125  }
126 
127  bool visible() {
128  return _visible.result();
129  }
130 
131  void calcProperties(pyrodactyl::event::Info &info);
132 
133  void x(int X) {
134  _pos.x = X;
135  }
136 
137  void y(int Y) {
138  _pos.y = Y;
139  }
140 
141  int x() {
142  return _pos.x;
143  }
144 
145  int y() {
146  return _pos.y;
147  }
148 
149  void walkPattern(const pyrodactyl::ai::MovementSet &set) {
150  _aiData._walk = set;
151  }
152 
153  void move(const pyrodactyl::ai::SpriteConstant &sc);
154 
155  // Resolve collisions for polygons we want to be outside of
156  void resolveCollide();
157 
158  // Resolve collisions for the walk rectangle
159  void resolveInside(Rect collider);
160 
161  void stop() {
162  _vel.Set();
163  _target.Set();
164  _aiData._dest._active = false;
165  }
166  void inputStop() {
167  _input.reset();
168  }
169 
170  void xVel(const float &val) {
171  _target.x = val * _velMod.x;
172  }
173  void yVel(const float &val) {
174  _target.y = val * _velMod.y;
175  }
176 
177  float xVel() {
178  return _vel.x;
179  }
180 
181  float yVel() {
182  return _vel.y;
183  }
184 
185  Vector2f vel() {
186  return _vel;
187  }
188 
189  const Common::String &id() {
190  return _id;
191  }
192 
193  int w() {
194  return _clip.w;
195  }
196 
197  int h() {
198  return _clip.h;
199  }
200 
201  const ImageKey &img() {
202  return _image;
203  }
204 
205  Rect dialogClip(const pyrodactyl::people::PersonState &state) {
206  return _animSet._walk.dialogClip(state);
207  }
208 
209  void dialogUpdateClip(const pyrodactyl::people::PersonState &state) {
210  _animSet._walk.updateClip(state);
211  }
212 
213  bool popupShow() {
214  return _popup.show();
215  }
216 
217  Rect boundRect();
218  Rect boxV();
219  Rect boxD();
220  Rect posRect();
221  Rect rangeRect(const Rect &bounds, const Range &range);
222  Vector2i camFocus();
223 
224  double distSq(const Sprite &s);
225 
226  void effectImg(bool vis) {
227  _imgEff._visible = vis;
228  }
229 
230  bool lastFrame() {
231  return _animSet._fight.lastFrame();
232  }
233 
234  bool takingDamage(Sprite &sp, const pyrodactyl::ai::SpriteConstant &sc);
235  void takeDamage(pyrodactyl::event::Info &info, Sprite &s);
236  void exchangeDamage(pyrodactyl::event::Info &info, Sprite &s, const pyrodactyl::ai::SpriteConstant &sc);
237 
238  void load(rapidxml::xml_node<char> *node, Common::Array<Common::Path> &animations);
239  void internalEvents(pyrodactyl::event::Info &info, const Common::String &player_id,
241 
242  void draw(pyrodactyl::event::Info &info, const Rect &camera);
243  void drawPopup(pyrodactyl::ui::ParagraphData &pop, const Rect &camera);
244 
245  void walk(const bool &reset);
246  void walk(const pyrodactyl::people::PersonState &pst);
247 
248  void updateFrame(const pyrodactyl::people::PersonState &pst, const bool &repeat = false);
249  void assignFrame();
250 
251  void updateMove(const pyrodactyl::input::FightAnimationType &combo);
252  void forceUpdateMove(const pyrodactyl::input::FightAnimationType &combo);
253 
254  void updateMove(const uint &index);
255  void forceUpdateMove(const uint &index);
256 
257  // Set sprite destination
258  void setDestPathfinding(const Vector2i &dest, bool reachable = true);
259 
260  // Used for sprite movement controlled by player input (usually the player sprite)
261  void handleEvents(pyrodactyl::event::Info &info, const Rect &camera, const pyrodactyl::ai::SpriteConstant &sc, const Common::Event &event);
262 
263  // This is for sprites with valid object ids
264  void animate(pyrodactyl::event::Info &info);
265 
266  // This is for objects without valid ids - like <background> and <fly> sprites
267  void animate(const pyrodactyl::people::PersonState &pst);
268 
269  // AI behavior routine for sprites attacking the player
270  void attack(pyrodactyl::event::Info &info, Sprite &targetSp, const pyrodactyl::ai::SpriteConstant &sc);
271 
272  // AI behavior routine for sprites running away from the player
273  // Requires every exit in the level be accessible
275 
276  // Used for sprites that fly across semi randomly on the screen
277  void flyAround(const Rect &camera, const pyrodactyl::ai::SpriteConstant &sc);
278 
279  // Used for the player destination movement
280  void moveToDest(pyrodactyl::event::Info &info, const pyrodactyl::ai::SpriteConstant &sc);
281  void moveToDestPathfinding(pyrodactyl::event::Info &info, const pyrodactyl::ai::SpriteConstant &sc);
282 
283  // Used for AI movement - returns true if at the destination, false otherwise
284  bool moveToLoc(Vector2i &dest, const float &vel, const pyrodactyl::ai::SpriteConstant &sc);
285  bool moveToLocPathfinding(Vector2i &dest, const float &vel, const pyrodactyl::ai::SpriteConstant &sc);
286 
287  void saveState(rapidxml::xml_document<> &doc, rapidxml::xml_node<char> *root);
288  void loadState(rapidxml::xml_node<char> *node);
289 };
290 } // End of namespace anim
291 } // End of namespace pyrodactyl
292 
293 } // End of namespace Crab
294 
295 #endif // CRAB_SPRITE_H
Definition: sprite.h:49
Definition: str.h:59
Definition: fightinput.h:59
Definition: Rectangle.h:42
Definition: ParagraphData.h:40
Definition: animset.h:43
Definition: list.h:44
Definition: GameEventInfo.h:44
Definition: PathfindingAgent.h:113
Definition: SpriteConstant.h:57
void reset()
Resets the algorithm.
Definition: range.h:40
Definition: imageeffect.h:40
Definition: events.h:199
Definition: spriteai.h:59
Definition: triggerset.h:40
Definition: moveeffect.h:37
Definition: movement.h:42