ScummVM API documentation
physicalobject.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_PHYSICALOBJECT_H
23 #define NANCY_ACTION_PHYSICALOBJECT_H
24 
25 #include "common/array.h"
26 #include "common/rect.h"
27 
28 namespace Nancy {
29 namespace Action {
30 
31 // Moves things about the way the Nancy17 arcade puzzles do. Time goes by in ticks
32 // of 20 ms, speeds are in pixels per second and angles in radians, counterclockwise
33 // with the y axis pointing down the screen. Each tick the speed goes down by the
34 // friction, a steady force (gravity, a gust) is added to the velocity, and the
35 // object moves no more than a pixel at a time, so it can't skip past its bounds.
37 public:
38  static const uint32 kTickLength = 20; // in milliseconds
39 
40  enum BoundsMode {
41  kStopAtBounds, // stands still where it would have left them
42  kBounceOffBounds
43  };
44 
45  // Something in the way of an object, inside of its bounds
46  struct Obstacle {
47  enum Type {
48  kPin, // a point the object's middle keeps the object's width away from
49  kBlock // a rect the object can't overlap
50  };
51 
52  Type type = kPin;
53  Common::Rect rect;
54  };
55 
56  void setSize(int width, int height) { _width = width; _height = height; }
57  // An empty rect means there are none
58  void setBounds(const Common::Rect &bounds) { _bounds = bounds; }
59  void setBoundsMode(BoundsMode mode) { _boundsMode = mode; }
60  // Shared by the objects of a puzzle, which keeps the list
61  void setObstacles(const Common::Array<Obstacle> *obstacles) { _obstacles = obstacles; }
62 
63  // Puts the object there, unless that is outside of its bounds
64  void moveTo(double x, double y);
65 
66  double getX() const { return _x; }
67  double getY() const { return _y; }
68  // The rect the object takes up, centered on its position
69  Common::Rect getRect() const;
70 
71  void setSpeed(double speed);
72  double getSpeed() const { return _speed; }
73  void setMaxSpeed(double maxSpeed) { _hasMaxSpeed = true; _maxSpeed = maxSpeed; setSpeed(_speed); }
74  void setHeading(double heading);
75  double getHeading() const { return _heading; }
76  void setFriction(double friction) { _friction = friction; }
77  void setForce(double x, double y) { _forceX = x; _forceY = y; }
78 
79  // Whether the object ran into its bounds since this was last cleared
80  bool hasHitBounds() const { return _hasHitBounds; }
81  void clearHitBounds() { _hasHitBounds = false; }
82 
83  void update(uint32 numTicks);
84 
85  // The direction of a vector, between 0 and 2 pi
86  static double getAngle(double x, double y);
87 
88 private:
89  Common::Rect getRectAt(double x, double y) const;
90  bool isInsideBounds(const Common::Rect &rect) const;
91  // The way out of the bounds, -1 while the rect is inside of them
92  double getBoundsNormal(const Common::Rect &rect) const;
93  bool isClearOfObstacle(const Obstacle &obstacle, const Common::Rect &rect) const;
94  bool isClearOfObstacles(const Common::Rect &rect) const;
95  // The way off the first obstacle the rect ran into, -1 when there is none
96  double getObstacleNormal(const Common::Rect &rect) const;
97 
98  void accelerate(double numTicks);
99  void applyFriction(double numTicks);
100  void move(double numTicks);
101  void stopAtBounds(double x, double y);
102  void bounceOffBounds(double x, double y);
103 
104  int _width = 1;
105  int _height = 1;
106  Common::Rect _bounds;
107  const Common::Array<Obstacle> *_obstacles = nullptr;
108  BoundsMode _boundsMode = kStopAtBounds;
109 
110  double _x = 0;
111  double _y = 0;
112  double _speed = 0;
113  double _heading = 0;
114  double _friction = 0;
115  double _forceX = 0;
116  double _forceY = 0;
117 
118  bool _hasMaxSpeed = false;
119  double _maxSpeed = 0;
120  bool _hasHitBounds = false;
121 };
122 
123 } // End of namespace Action
124 } // End of namespace Nancy
125 
126 #endif // NANCY_ACTION_PHYSICALOBJECT_H
Definition: physicalobject.h:46
Definition: rect.h:536
Definition: physicalobject.h:36
Definition: actionmanager.h:32