ScummVM API documentation
fishingpuzzle.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_FISHINGPUZZLE_H
23 #define NANCY_ACTION_FISHINGPUZZLE_H
24 
25 #include "engines/nancy/commontypes.h"
26 #include "engines/nancy/action/puzzlerecord.h"
27 
28 namespace Nancy {
29 namespace Action {
30 
31 // Nancy16 ice fishing puzzle (AR 193). Fish and junk swim across the lanes of the lake
32 // below the ice hole, and clicking moves the hook to the clicked depth. Whatever touches
33 // the hook first is caught, and each catch leads to its own scene. Large catches also
34 // get snagged on the line itself. Pulling the hook out of the water ends the puzzle.
35 class FishingPuzzle : public PuzzleRecord {
36 public:
37  // Drawn over the scene's movies, which include a water animation covering the viewport
38  FishingPuzzle() : PuzzleRecord(15) {}
39  virtual ~FishingPuzzle();
40 
41  void init() override;
42 
43  void readData(Common::SeekableReadStream &stream) override;
44  void execute() override;
45  void handleInput(NancyInput &input) override;
46 
47  bool isViewportRelative() const override { return true; }
48 
49 protected:
50  Common::String getRecordTypeName() const override { return "FishingPuzzle"; }
51 
52  struct Vec2 {
53  float x = 0;
54  float y = 0;
55 
56  Vec2() {}
57  Vec2(float x_, float y_) : x(x_), y(y_) {}
58 
59  Vec2 operator+(const Vec2 &v) const { return Vec2(x + v.x, y + v.y); }
60  Vec2 operator-(const Vec2 &v) const { return Vec2(x - v.x, y - v.y); }
61  Vec2 operator*(float f) const { return Vec2(x * f, y * f); }
62  float length() const;
63  };
64 
65  struct Ending {
66  SceneChangeWithFlag scene;
67  RandomSoundBlock sound;
68  };
69 
70  struct Catchable {
71  Common::String name;
72  bool snagsLine = false; // caught by touching any part of the line, not just the hook
73  float minSpeed = 0; // pixels per second
74  float maxSpeed = 0;
75  Common::Path imageName;
76  uint imageID = 0;
78  Common::Rect hitbox; // relative to the frame origin
79  Ending ending;
80  };
81 
82  struct LaneEntry {
83  uint catchable = 0;
84  int32 percent = 0;
85  };
86 
87  struct Lane {
88  Common::Rect bounds;
89  float minInterval = 0; // seconds between catchables
90  float maxInterval = 0;
92 
93  float ticks = 0;
94  float interval = 0;
95  };
96 
97  struct Waypoint {
98  Vec2 pos;
99  Vec2 tangent;
100  float ticks = 0; // time taken to get here from the previous waypoint
101  };
102 
103  // A catchable swimming across a lane
104  struct Swimmer {
105  uint catchable = 0;
106  bool isMirrored = false; // swims right to left
108  float segmentTicks = 0;
109  Vec2 pos;
110  float animPhase = 0; // 0 to 1, over all frames
111  float animFPS = 0;
112  };
113 
114  static void readEnding(Common::SeekableReadStream &stream, Ending &ending);
115  static Vec2 hermite(const Vec2 &p1, const Vec2 &t1, const Vec2 &p2, const Vec2 &t2, float s);
116  static bool segmentsIntersect(const Vec2 &a1, const Vec2 &a2, const Vec2 &b1, const Vec2 &b2);
117 
118  void pickLaneInterval(Lane &lane);
119  void spawn(const Lane &lane, uint catchable);
120  bool moveSwimmer(Swimmer &swimmer, float ticks);
121  const Common::Rect &getFrame(const Swimmer &swimmer) const;
122  bool isCaught(const Swimmer &swimmer, const Vec2 &hook, const Vec2 &lineTop, const Vec2 &lineBottom) const;
123 
124  void moveHookTo(const Common::Point &pos);
125  bool moveLine(int32 ticks);
126  void update(int32 lineTicks, float ticks);
127  void startEnding(const Ending &ending);
128 
129  void redraw();
130 
131  // -- File data --
132  Common::Array<Catchable> _catchables;
133  Common::Array<Lane> _lanes;
134  uint16 _cursor = 0;
135 
136  // The line image is _image
137  float _lineAcceleration = 0; // pixels per tick squared
138  Common::Rect _lineSrc;
139  Common::Point _hookPoint; // all relative to the line's top left
140  Common::Point _lineTop;
141  Common::Point _lineBottom;
142  Common::Rect _lineBounds;
143  Common::Point _lineStart; // left and bottom
144  Common::Point _clickOffset;
145  RandomSoundBlock _reelSound;
146  int32 _exitHookY = 0; // pulling the hook above this ends the puzzle
147  Ending _exitEnding;
148 
149  // -- Runtime state --
151  Common::Array<uint> _drawOrder;
152  Common::Array<Swimmer> _swimmers;
153 
154  Vec2 _linePos;
155  Vec2 _lineVelocity;
156  Vec2 _lineAccel;
157  Common::Point _lineTarget;
158 
159  uint32 _lastTickTime = 0; // the line moves in whole ticks
160  uint32 _lastFrameTime = 0; // everything else moves smoothly
161  bool _isEnding = false;
162  bool _exitRequested = false;
163  SceneChangeWithFlag _endScene;
164  SoundDescription _endSound;
165 };
166 
167 } // End of namespace Action
168 } // End of namespace Nancy
169 
170 #endif // NANCY_ACTION_FISHINGPUZZLE_H
Definition: fishingpuzzle.h:65
Definition: str.h:59
Definition: fishingpuzzle.h:87
Definition: fishingpuzzle.h:35
Definition: fishingpuzzle.h:52
Definition: rect.h:536
Definition: path.h:52
Definition: fishingpuzzle.h:70
Definition: stream.h:745
Definition: input.h:41
Definition: commontypes.h:206
Definition: fishingpuzzle.h:97
Definition: commontypes.h:324
Definition: fishingpuzzle.h:82
Definition: rect.h:144
Definition: puzzlerecord.h:37
Definition: commontypes.h:306
Definition: fishingpuzzle.h:104
Definition: actionmanager.h:32