Arkanoid Remake  1.0
DynObj.h
Go to the documentation of this file.
1 /*
2 ArkanoidRemakeSDL
3 a sample of object-oriented game programming with C++.
4 version 1.0, May 2016
5 
6 Copyright (c) 2016 Giovanni Paolo Vigano'
7 
8 The source code of the SDL library used by ArkanoidRemakeSDL can be found here:
9 https://www.libsdl.org/
10 ---
11 ArkanoidRemakeSDL source code is licensed with the same zlib license:
12 (http://www.gzip.org/zlib/zlib_license.html)
13 
14 This software is provided 'as-is', without any express or implied warranty. In
15 no event will the authors be held liable for any damages arising from the use of
16 this software.
17 
18 Permission is granted to anyone to use this software for any purpose, including
19 commercial applications, and to alter it and redistribute it freely, subject to
20 the following restrictions:
21 
22  1. The origin of this software must not be misrepresented; you must not
23  claim that you wrote the original software. If you use this software in a
24  product, an acknowledgment in the product documentation would be appreciated
25  but is not required.
26 
27  2. Altered source versions must be plainly marked as such, and must not be
28  misrepresented as being the original software.
29 
30  3. This notice may not be removed or altered from any source distribution.
31 */
32 
33 
37 
38 #pragma once
39 
40 #ifndef DYNOBJ_H
41 #define DYNOBJ_H
42 
43 #include <BoxObj.h>
44 #include <cmath> //sqrt
45 
48 
52 class DynObj : public BoxObj
53 {
54 public:
56  DynObj();
57 
59  DynObj(const DynObj& obj);
60 
62  void SetVel(double x, double y)
63  {
64  mVel.X=x;
65  mVel.Y=y;
66  mSpeedChanged = true;
67  }
68 
70  void SetVelX(double x)
71  {
72  mVel.X=x;
73  mSpeedChanged = true;
74  }
75 
77  void SetVelY(double y)
78  {
79  mVel.Y=y;
80  mSpeedChanged = true;
81  }
82 
84  const VecXy& Vel() const
85  {
86  if (mSpeedChanged) UpdateSpeed();
87  return mVel;
88  }
89 
91  const double VelX() const { return mVel.X; }
92 
94  const double VelY() const { return mVel.Y; }
95 
97  void InvertVelX() { mVel.X = -mVel.X; }
98 
100  void InvertVelY() { mVel.Y = -mVel.Y; }
101 
103  const double Speed() const
104  {
105  if (mSpeedChanged) UpdateSpeed();
106  return mSpeed;
107  }
108 
110  const double SquareSpeed() const
111  {
112  if (mSpeedChanged) UpdateSpeed();
113  return mSquareSpeed;
114  }
115 
118  const VecXy& PrevPos() const { return mPrevPos; }
119  VecXy& PrevPos() { return mPrevPos; }
121 
123  void Move( double delta_time );
124 
126  void Stop() { SetVel(0.0,0.0); }
127 
128 protected:
129 
130  VecXy mVel;
131  VecXy mPrevPos;
132 
134  void UpdateSpeed() const
135  {
136  mSquareSpeed = mVel.X*mVel.X + mVel.Y*mVel.Y;
137  mSpeed = sqrt(mSquareSpeed);
138  }
139 
140 private:
141 
142  // mutable attributes do not affect the state of this object,
143  // they are just computed on demand based on that state
144 
145  mutable double mSquareSpeed;
146  mutable double mSpeed;
147  mutable bool mSpeedChanged;
148 };
149 
151 
152 #endif//DYNOBJ_H
153 
void InvertVelX()
Invert the velocity component along the X axis.
Definition: DynObj.h:97
void SetVelY(double y)
Set the velocity along the Y axis.
Definition: DynObj.h:77
const double VelY() const
Get the velocity component along the Y axis.
Definition: DynObj.h:94
void SetVel(double x, double y)
Set the velocity along the axes.
Definition: DynObj.h:62
const double Speed() const
Get the speed (recalculate it if needed)
Definition: DynObj.h:103
const double SquareSpeed() const
Get the square speed (recalculate it if needed)
Definition: DynObj.h:110
void SetVelX(double x)
Set the velocity along the X axis.
Definition: DynObj.h:70
Vector with two (double precision) coordinate values.
Definition: VecXy.h:59
A box-shaped object.
Definition: BoxObj.h:51
const VecXy & PrevPos() const
Definition: DynObj.h:118
const VecXy & Vel() const
Get the velocity vector.
Definition: DynObj.h:84
const double VelX() const
Get the velocity component along the X axis.
Definition: DynObj.h:91
double Y
Vertical coordinate.
Definition: VecXy.h:62
double X
Horizontal coordinate.
Definition: VecXy.h:61
void Stop()
Stop: reset the velocity.
Definition: DynObj.h:126
void UpdateSpeed() const
Update the scala speed values based on the velocity.
Definition: DynObj.h:134
DynObj()
Default constructor.
void InvertVelY()
Invert the velocity component along the Y axis.
Definition: DynObj.h:100
BoxObj class definition.
void Move(double delta_time)
Move according to the velocity.
Dynamic object with velocity control.
Definition: DynObj.h:52