starting to move to a generic state structure
[chaz/yoink] / src / Character.cc
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 #include <iostream>
30
31 #include "Character.hh"
32 #include "Log.hh"
33
34
35 struct SpringForce
36 {
37 explicit SpringForce(Mf::Vector2 x) :
38 location(x) {}
39
40 const Mf::Vector2& operator () (const Mf::LinearState<2>& state)
41 {
42 Mf::Vector2 x = state.position - location;
43 Mf::Scalar mag = x.length();
44 Mf::Scalar d = 50.0;
45
46 // spring:
47 //current.force += -15.0 * x - 1.5 * current.velocity;
48 force = -20.0 * (mag - d) * (x / mag) - 2.0 * state.velocity;
49
50 return force;
51 }
52
53 private:
54
55 Mf::Vector2 force;
56 Mf::Vector2 location;
57 };
58
59 struct WindResistenceForce
60 {
61 const Mf::Vector2& operator () (const Mf::LinearState<2>& state)
62 {
63 force = -2.0 * state.velocity;
64 return force;
65 }
66
67 private:
68
69 Mf::Vector2 force;
70 };
71
72
73 Character::Character(const std::string& name) :
74 tilemap_(name),
75 animation_(name)
76 {
77 current.init();
78
79 current.mass = 1.0;
80 current.inverseMass = 1.0 / current.mass;
81
82 // gravity
83 current.force = Mf::Vector2(0.0, 000.0);
84 current.forces.push_back(SpringForce(Mf::Vector2(500.0, 200.0)));
85 current.forces.push_back(WindResistenceForce());
86 current.forces.push_back(Mf::LinearState<2>::GravityForce(-2000.0));
87
88 // starting position
89 current.position = Mf::Vector2(64.0, 64.0);
90 current.momentum = Mf::Vector2(0.0, 0.0);
91 current.recalculate();
92
93 previous = current;
94 }
95
96
97 void Character::update(Mf::Scalar t, Mf::Scalar dt)
98 {
99 previous = current;
100
101 //Mf::Vector2 x = current.position - Mf::Vector2(500.0, 200.0);
102 //Mf::Scalar mag = x.length();
103 //Mf::Scalar d = 50.0;
104
105 //// gravity:
106 //current.force = Mf::Vector2(0.0, -2000.0);
107 //// spring:
108 ////current.force += -15.0 * x - 1.5 * current.velocity;
109 //current.force += -20.0 * (mag - d) * (x / mag) - 2.0 * current.velocity;
110 //// internal:
111 //current.force += userForce;
112 //current.recalculate();
113 //std::cout << "force: " << current.momentum << std::endl;
114
115 //Mf::euler<State,Derivative>(current, t, dt);
116
117 //current.force = Mf::Vector2(0.0, -2000.0);
118 current.force = userForce;
119 current.integrate(t, dt);
120
121 animation_.update(t, dt);
122 }
123
124
125 void Character::draw(Mf::Scalar alpha) const
126 {
127 Mf::Vector2 position = cml::lerp(previous.position, current.position, alpha);
128
129 //glColor3f(1.0f, 1.0f, 1.0f);
130 tilemap_.bind();
131
132 Mf::Tilemap::Index frame = animation_.getFrame();
133
134 Mf::Tilemap::Orientation orientation = Mf::Tilemap::NORMAL;
135
136 if (current.velocity[0] < 0.0) orientation = Mf::Tilemap::REVERSE;
137
138 Mf::Scalar coords[8];
139 tilemap_.getTileCoords(frame, coords, orientation);
140
141 Mf::Scalar s = 16.0;
142
143 glBegin(GL_TRIANGLE_FAN);
144 glTexCoord2f(coords[0], coords[1]);
145 glVertex3(position[0]-s, position[1]-s, z);
146 glTexCoord2f(coords[2], coords[3]);
147 glVertex3(position[0]+s, position[1]-s, z);
148 glTexCoord2f(coords[4], coords[5]);
149 glVertex3(position[0]+s, position[1]+s, z);
150 glTexCoord2f(coords[6], coords[7]);
151 glVertex3(position[0]-s, position[1]+s, z);
152 glEnd();
153
154 //glColor3f(0.0f, 0.0f, 0.0f);
155 Mf::Texture::resetBind();
156
157 glBegin(GL_TRIANGLES);
158 glVertex3(480.0, 190.0, 64.0);
159 glVertex3(520.0, 190.0, 64.0);
160 glVertex3(500.0, 210.0, 64.0);
161 glEnd();
162
163 //glColor3f(1.0f, 1.0f, 1.0f);
164 }
165
166
167 Mf::Tilemap& Character::getTilemap()
168 {
169 return tilemap_;
170 }
171
172 Mf::Animation& Character::getAnimation()
173 {
174 return animation_;
175 }
176
177
178 /** vim: set ts=4 sw=4 tw=80: *************************************************/
179
This page took 0.039286 seconds and 4 git commands to generate.