]> Dogcows Code - chaz/yoink/blob - src/Character.hh
preliminary physics, sound, hud
[chaz/yoink] / src / Character.hh
1
2 /*******************************************************************************
3
4 Copyright (c) 2009, Charles McGarvey
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 *******************************************************************************/
28
29 #ifndef _CHARACTER_HH_
30 #define _CHARACTER_HH_
31
32 #include <boost/shared_ptr.hpp>
33
34 #include <Moof/Animation.hh>
35 #include <Moof/Entity.hh>
36 #include <Moof/Event.hh>
37 #include <Moof/Math.hh>
38 #include <Moof/Octree.hh>
39 #include <Moof/Physics.hh>
40 #include <Moof/Tilemap.hh>
41
42
43 /**
44 * Parent class of animate objects with "personalities." This basically
45 * includes the heroine herself and the bad guys.
46 */
47
48 class Character : public Mf::Entity
49 {
50 public:
51
52 struct Derivative
53 {
54 Mf::Vector2 velocity;
55 Mf::Vector2 force;
56
57 Derivative operator*(Mf::Scalar dt) const
58 {
59 Derivative derivative;
60 derivative.velocity = dt * velocity;
61 derivative.force = dt * force;
62 return derivative;
63 }
64
65 Derivative operator+(const Derivative& other) const
66 {
67 Derivative derivative;
68 derivative.velocity = velocity + other.velocity;
69 derivative.force = force + other.force;
70 return derivative;
71 }
72 };
73
74 struct State
75 {
76 // primary
77
78 Mf::Vector2 position;
79 Mf::Vector2 momentum;
80 Mf::Vector2 force;
81
82 // secondary
83
84 Mf::Vector2 velocity;
85
86 // constant
87
88 Mf::Scalar mass;
89 Mf::Scalar inverseMass;
90
91
92 void getDerivative(Derivative& derivative, Mf::Scalar t) const
93 {
94 //derivative.velocity = Mf::Vector2(0.0, 0.0);
95 //derivative.force = Mf::Vector2(0.0, 0.0);
96 derivative.velocity = velocity;
97 derivative.force = force;
98 }
99
100 void recalculate()
101 {
102 velocity = momentum * inverseMass;
103 }
104
105 void applyDerivative(const Derivative& derivative, Mf::Scalar dt)
106 {
107 position += dt * derivative.velocity;
108 momentum += dt * derivative.force;
109 recalculate();
110 }
111
112 State operator*(Mf::Scalar scalar) const
113 {
114 State state = *this;
115 state.position *= scalar;
116 state.momentum *= scalar;
117 state.recalculate();
118 return state;
119 }
120
121 State operator+(State state) const
122 {
123 State newState = *this;
124 newState.position += state.position;
125 newState.momentum += state.momentum;
126 newState.recalculate();
127 return newState;
128 }
129 };
130
131
132 Character(const std::string& name);
133 virtual ~Character();
134
135 void update(Mf::Scalar t, Mf::Scalar dt);
136 void handleEvent(const Mf::Event& event);
137 void draw(Mf::Scalar alpha) const;
138
139 Mf::Tilemap& getTilemap();
140 Mf::Animation& getAnimation();
141
142 State previous;
143 State current;
144
145 stlplus::ntree<Mf::OctreeNode>::iterator treeNode;
146
147 private:
148
149 void updateContainers();
150
151 static const Mf::Scalar z = 96.0;
152
153 Mf::Tilemap tilemap_;
154 Mf::Animation animation_;
155 };
156
157 typedef boost::shared_ptr<Character> CharacterPtr;
158
159
160 inline Character::State operator*(Mf::Scalar scalar, const Character::State& state)
161 {
162 Character::State newState = state;
163 newState.position *= scalar;
164 newState.momentum *= scalar;
165 newState.recalculate();
166 return newState;
167 }
168
169
170 #endif // _CHARACTER_HH_
171
172 /** vim: set ts=4 sw=4 tw=80: *************************************************/
173
This page took 0.04222 seconds and 5 git commands to generate.