]> Dogcows Code - chaz/yoink/blob - src/Character.cc
more featureful sound class
[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 Character::Character(const std::string& name) :
36 tilemap_(name),
37 animation_(name)
38 {
39 current.mass = 1.0;
40 current.inverseMass = 1.0 / current.mass;
41
42 current.force = Mf::Vector2(0.0, -120.0);
43
44 current.position = Mf::Vector2(64.0, 64.0);
45 current.momentum = Mf::Vector2(0.0, 0.0);
46 current.recalculate();
47
48 previous = current;
49
50 updateContainers();
51 }
52
53 Character::~Character()
54 {
55 //delete texture;
56 //delete anim;
57 }
58
59
60 void Character::update(Mf::Scalar t, Mf::Scalar dt)
61 {
62 previous = current;
63 Mf::integrate<State,Derivative>(current, t, dt);
64
65 animation_.update(t, dt);
66
67 updateContainers();
68 }
69
70 void Character::updateContainers()
71 {
72 aabb_.init(Mf::Vector3(current.position[0]-16.0, current.position[1]-16.0, z),
73 Mf::Vector3(current.position[0]+16.0, current.position[1]+16.0, z));
74 sphere_.point = Mf::Vector3(current.position[0], current.position[1], z);
75 sphere_.radius = (aabb_.min - sphere_.point).length();
76 }
77
78 void Character::handleEvent(const Mf::Event& event)
79 {
80 // really just for heroine...
81
82 Mf::Scalar force = 500.0;
83
84 Mf::Vector2 left = Mf::Vector2(-force, 0.0);
85 Mf::Vector2 right = Mf::Vector2(force, 0.0);
86 Mf::Vector2 down = Mf::Vector2(0.0, -force);
87 Mf::Vector2 up = Mf::Vector2(0.0, force);
88
89 switch (event.type)
90 {
91 case SDL_KEYDOWN:
92 if (event.key.keysym.sym == SDLK_a)
93 {
94 current.force += left;
95 }
96 else if (event.key.keysym.sym == SDLK_d)
97 {
98 current.force += right;
99 }
100 else if (event.key.keysym.sym == SDLK_s)
101 {
102 current.force += down;
103 }
104 else if (event.key.keysym.sym == SDLK_w)
105 {
106 current.force += up;
107 }
108 break;
109
110 case SDL_KEYUP:
111 if (event.key.keysym.sym == SDLK_a)
112 {
113 current.force -= left;
114 }
115 else if (event.key.keysym.sym == SDLK_d)
116 {
117 current.force -= right;
118 }
119 else if (event.key.keysym.sym == SDLK_s)
120 {
121 current.force -= down;
122 }
123 else if (event.key.keysym.sym == SDLK_w)
124 {
125 current.force -= up;
126 }
127 break;
128 }
129
130 //Mf::logInfo("current force [%f %f]", current.force[0], current.force[1]);
131 //std::cerr << "current force: " << current.force << std::endl;
132 }
133
134
135 void Character::draw(Mf::Scalar alpha) const
136 {
137 State state = cml::lerp(previous, current, alpha);
138
139 glColor3f(1.0f, 1.0f, 1.0f);
140 tilemap_.bind();
141
142 Mf::Tilemap::Index frame = animation_.getFrame();
143
144 Mf::Scalar coords[8];
145 tilemap_.getTileCoords(frame, coords);
146
147 Mf::Scalar s = 16.0;
148
149 glBegin(GL_TRIANGLE_FAN);
150 glTexCoord2f(coords[0], coords[1]);
151 glVertex3(state.position[0]-s, state.position[1]-s, z);
152 glTexCoord2f(coords[2], coords[3]);
153 glVertex3(state.position[0]+s, state.position[1]-s, z);
154 glTexCoord2f(coords[4], coords[5]);
155 glVertex3(state.position[0]+s, state.position[1]+s, z);
156 glTexCoord2f(coords[6], coords[7]);
157 glVertex3(state.position[0]-s, state.position[1]+s, z);
158 glEnd();
159 }
160
161
162 Mf::Tilemap& Character::getTilemap()
163 {
164 return tilemap_;
165 }
166
167 Mf::Animation& Character::getAnimation()
168 {
169 return animation_;
170 }
171
172
173 /** vim: set ts=4 sw=4 tw=80: *************************************************/
174
This page took 0.038347 seconds and 4 git commands to generate.