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