]>
Dogcows Code - chaz/yoink/blob - src/Character.cc
4ce11052274d0a0d9a23e378e3f5d7302ebd729a
2 /*******************************************************************************
4 Copyright (c) 2009, Charles McGarvey
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
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.
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.
27 *******************************************************************************/
31 #include "Character.hh"
35 Character::Character(const std::string
& name
) :
40 current
.inverseMass
= 1.0 / current
.mass
;
43 //current.force = Mf::Vector2(0.0, -120.0);
46 current
.position
= Mf::Vector2(64.0, 64.0);
47 current
.momentum
= Mf::Vector2(0.0, 0.0);
48 current
.recalculate();
56 void Character::update(Mf::Scalar t
, Mf::Scalar dt
)
60 Mf::Scalar epsilon
= 100.0;
62 //current.momentum = Mf::Vector2(0.0, -120.0);
63 current
.momentum
= -5 * (current
.position
- Mf::Vector2(500.0, 200.0))
64 - 2.0 * current
.velocity
;
65 current
.recalculate();
66 std::cout
<< "force: " << current
.momentum
<< std::endl
;
68 //if (std::abs(current.force[0]) < epsilon && std::abs(current.force[1]) < epsilon &&
69 //std::abs(current.velocity[0]) < epsilon && std::abs(current.velocity[1]) < epsilon)
71 //current.force = Mf::Vector2(0.0, 0.0);
72 //current.velocity = Mf::Vector2(0.0, 0.0);
73 //current.momentum = Mf::Vector2(0.0, 0.0);
76 Mf::integrate
<State
,Derivative
>(current
, t
, dt
);
78 animation_
.update(t
, dt
);
83 void Character::updateContainers()
85 aabb_
.init(Mf::Vector3(current
.position
[0]-16.0, current
.position
[1]-16.0, z
),
86 Mf::Vector3(current
.position
[0]+16.0, current
.position
[1]+16.0, z
));
87 sphere_
.point
= Mf::Vector3(current
.position
[0], current
.position
[1], z
);
88 sphere_
.radius
= (aabb_
.min
- sphere_
.point
).length();
91 void Character::handleEvent(const Mf::Event
& event
)
93 // really just for heroine...
95 Mf::Scalar force
= 500.0;
97 Mf::Vector2 left
= Mf::Vector2(-force
, 0.0);
98 Mf::Vector2 right
= Mf::Vector2(force
, 0.0);
99 Mf::Vector2 down
= Mf::Vector2(0.0, -force
);
100 Mf::Vector2 up
= Mf::Vector2(0.0, force
);
105 if (event
.key
.keysym
.sym
== SDLK_a
)
107 current
.force
+= left
;
109 else if (event
.key
.keysym
.sym
== SDLK_d
)
111 current
.force
+= right
;
113 else if (event
.key
.keysym
.sym
== SDLK_s
)
115 current
.force
+= down
;
117 else if (event
.key
.keysym
.sym
== SDLK_w
)
124 if (event
.key
.keysym
.sym
== SDLK_a
)
126 current
.force
-= left
;
128 else if (event
.key
.keysym
.sym
== SDLK_d
)
130 current
.force
-= right
;
132 else if (event
.key
.keysym
.sym
== SDLK_s
)
134 current
.force
-= down
;
136 else if (event
.key
.keysym
.sym
== SDLK_w
)
143 //Mf::logInfo("current force [%f %f]", current.force[0], current.force[1]);
144 //std::cerr << "current force: " << current.force << std::endl;
148 void Character::draw(Mf::Scalar alpha
) const
150 State state
= cml::lerp(previous
, current
, alpha
);
152 glColor3f(1.0f
, 1.0f
, 1.0f
);
155 Mf::Tilemap::Index frame
= animation_
.getFrame();
157 Mf::Scalar coords
[8];
158 tilemap_
.getTileCoords(frame
, coords
);
162 glBegin(GL_TRIANGLE_FAN
);
163 glTexCoord2f(coords
[0], coords
[1]);
164 glVertex3(state
.position
[0]-s
, state
.position
[1]-s
, z
);
165 glTexCoord2f(coords
[2], coords
[3]);
166 glVertex3(state
.position
[0]+s
, state
.position
[1]-s
, z
);
167 glTexCoord2f(coords
[4], coords
[5]);
168 glVertex3(state
.position
[0]+s
, state
.position
[1]+s
, z
);
169 glTexCoord2f(coords
[6], coords
[7]);
170 glVertex3(state
.position
[0]-s
, state
.position
[1]+s
, z
);
175 Mf::Tilemap
& Character::getTilemap()
180 Mf::Animation
& Character::getAnimation()
186 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.04818 seconds and 4 git commands to generate.