ScummVM API documentation
whackitpuzzle.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 #ifndef NANCY_ACTION_WHACKITPUZZLE_H
23 #define NANCY_ACTION_WHACKITPUZZLE_H
24 
25 #include "engines/nancy/commontypes.h"
26 #include "engines/nancy/action/puzzlerecord.h"
27 #include "engines/nancy/action/physicalobject.h"
28 
29 namespace Nancy {
30 namespace Action {
31 
32 // Nancy17 bees in the garden. The bees fly around, get angry after a while, and
33 // sting the player when they stay angry for too long. A puff of the spray can knocks
34 // down every bee it touches; a bee that is down gets back up after some time. With
35 // every bee down the puzzle sets its flag, and too many stings lose the game.
36 class WhackItPuzzle : public PuzzleRecord {
37 public:
38  // The bees and the puffs go in front of the berries, which are overlays
39  WhackItPuzzle() : PuzzleRecord(15) {}
40  virtual ~WhackItPuzzle() {}
41 
42  void init() override;
43  void registerGraphics() override;
44 
45  void readData(Common::SeekableReadStream &stream) override;
46  void execute() override;
47  void handleInput(NancyInput &input) override;
48 
49  bool isViewportRelative() const override { return true; }
50 
51 protected:
52  Common::String getRecordTypeName() const override { return "WhackItPuzzle"; }
53 
54  struct Animation {
55  uint32 frameTime = 0; // in milliseconds
57 
58  void readData(Common::SeekableReadStream &stream);
59  };
60 
61  struct AnimationPlayer {
62  const Animation *animation = nullptr;
63  uint frame = 0;
64  uint32 frameRemainder = 0;
65  bool isDone = false;
66 
67  void start(const Animation *newAnimation);
68  void advance(uint32 time, bool loops);
69  // The current frame, centered on a position
70  Common::Rect getDest(const Common::Point &pos) const;
71  const Common::Rect &getSrc() const { return animation->frames[MIN<uint>(frame, animation->frames.size() - 1)]; }
72  };
73 
74  enum BeeState {
75  kDown,
76  kReviving,
77  kFlying,
78  kAngry
79  };
80 
81  struct Bee {
82  BeeState state = kFlying;
83  uint32 stateEndTime = 0;
84  PhysicalObject body;
85  AnimationPlayer animation;
86 
87  Common::Point getPos() const { return Common::Point((int)body.getX(), (int)body.getY()); }
88  };
89 
90  // Drawn over the puzzle, so it can be see-through
92  FlashOverlay() : RenderObject(16) {}
93 
94  bool isViewportRelative() const override { return true; }
95  };
96 
97  struct Puff {
98  Common::Point pos;
99  AnimationPlayer animation;
100  };
101 
102  // A time between the two given ones, counted from now
103  uint32 getRandomTime(uint32 minTime, uint32 maxTime) const;
104  void spawnBee(Bee &bee);
105  void flyAgain(Bee &bee);
106  void knockDown(Bee &bee);
107  void updateBee(Bee &bee, uint32 numTicks);
108  void sting();
109  void updateSounds(uint numReviving, uint numFlying, uint numAngry);
110  void redraw();
111 
112  // -- File data --
113  Common::Rect _canSrc;
114  uint16 _cursor = 0;
115  Common::Point _canOffset; // from the mouse to the can's top left corner
116  Common::Point _startPos; // a negative coordinate is random
117  Common::Rect _bounds;
118  int32 _minBees = 0;
119  int32 _maxBees = 0;
120 
121  // How long the bees stay in each state
122  uint32 _downTime[2] = { 0, 0 };
123  uint32 _revivingTime[2] = { 0, 0 };
124  uint32 _flyingTime[2] = { 0, 0 };
125  uint32 _angryTime[2] = { 0, 0 };
126 
127  FlagDescription _allDownFlag; // gets the opposite value while any bee flies
128  bool _stopsWhenAllDown = false;
129  int32 _hitFrame = 0; // the frame of a bee that a puff has to touch
130 
131  Animation _puffAnimation;
132  Animation _flyAnimation;
133  Animation _downAnimation;
134 
135  RandomSoundBlock _puffSound;
136  RandomSoundBlock _revivingSound;
137  RandomSoundBlock _flyingSound;
138  RandomSoundBlock _angrySound;
139  RandomSoundBlock _stingSound;
140 
141  int32 _maxStings = 0;
142  SceneChangeWithFlag _loseScene;
143  RandomSoundBlock _loseSound;
144 
145  // -- Runtime --
146  Common::Array<Bee> _bees;
147  Common::Array<Puff> _puffs;
148  bool _isActive = true;
149  int32 _numStings = 0;
150  uint32 _lastStingTime = 0;
151  bool _hasStung = false;
152  FlashOverlay _flash;
153  uint32 _lastUpdateTime = 0;
154  Common::Point _mousePos;
155  bool _isMouseInside = false;
156  bool _isLost = false;
157  bool _exitRequested = false;
158 };
159 
160 } // End of namespace Action
161 } // End of namespace Nancy
162 
163 #endif // NANCY_ACTION_WHACKITPUZZLE_H
Definition: str.h:59
Definition: rect.h:536
Definition: stream.h:745
Definition: input.h:41
Definition: commontypes.h:206
Definition: renderobject.h:36
Definition: whackitpuzzle.h:91
Definition: commontypes.h:324
Definition: whackitpuzzle.h:81
Definition: whackitpuzzle.h:97
Definition: rect.h:144
size_type size() const
Definition: array.h:316
Definition: puzzlerecord.h:37
Definition: whackitpuzzle.h:36
Definition: commontypes.h:201
Definition: physicalobject.h:36
Definition: actionmanager.h:32
Definition: whackitpuzzle.h:61
Definition: whackitpuzzle.h:54