8e5a4c65e7437ca4f80a8614a8b17c5f1c087454
[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 // gravity
43 current.force = Mf::Vector2(0.0, -120.0);
44
45 // starting position
46 current.position = Mf::Vector2(64.0, 64.0);
47 current.momentum = Mf::Vector2(0.0, 0.0);
48 current.recalculate();
49
50 previous = current;
51
52 //updateContainers();
53 }
54
55
56 void Character::update(Mf::Scalar t, Mf::Scalar dt)
57 {
58 previous = current;
59
60 Mf::Vector2 x = current.position - Mf::Vector2(500.0, 200.0);
61 Mf::Scalar mag = x.length();
62 Mf::Scalar d = 50.0;
63
64 current.force = Mf::Vector2(0.0, -2000.0);
65 //current.force += -15.0 * x - 1.5 * current.velocity;
66 current.force += -20.0 * (mag - d) * (x / mag) - 2.0 * current.velocity;
67 current.force += userForce;
68 current.recalculate();
69 //std::cout << "force: " << current.momentum << std::endl;
70
71 Mf::integrate<State,Derivative>(current, t, dt);
72
73 animation_.update(t, dt);
74
75 //updateContainers();
76 }
77
78 //void Character::updateContainers()
79 //{
80 //aabb_.init(Mf::Vector3(current.position[0]-16.0, current.position[1]-16.0, z),
81 //Mf::Vector3(current.position[0]+16.0, current.position[1]+16.0, z));
82 //sphere_.point = Mf::Vector3(current.position[0], current.position[1], z);
83 //sphere_.radius = (aabb_.min - sphere_.point).length();
84 //}
85
86
87 void Character::draw(Mf::Scalar alpha) const
88 {
89 State state = cml::lerp(previous, current, alpha);
90
91 //glColor3f(1.0f, 1.0f, 1.0f);
92 tilemap_.bind();
93
94 Mf::Tilemap::Index frame = animation_.getFrame();
95
96 Mf::Tilemap::Orientation orientation = Mf::Tilemap::NORMAL;
97
98 if (current.velocity[0] < 0.0) orientation = Mf::Tilemap::REVERSE;
99
100 Mf::Scalar coords[8];
101 tilemap_.getTileCoords(frame, coords, orientation);
102
103 Mf::Scalar s = 16.0;
104
105 glBegin(GL_TRIANGLE_FAN);
106 glTexCoord2f(coords[0], coords[1]);
107 glVertex3(state.position[0]-s, state.position[1]-s, z);
108 glTexCoord2f(coords[2], coords[3]);
109 glVertex3(state.position[0]+s, state.position[1]-s, z);
110 glTexCoord2f(coords[4], coords[5]);
111 glVertex3(state.position[0]+s, state.position[1]+s, z);
112 glTexCoord2f(coords[6], coords[7]);
113 glVertex3(state.position[0]-s, state.position[1]+s, z);
114 glEnd();
115
116 //glColor3f(0.0f, 0.0f, 0.0f);
117 Mf::Texture::resetBind();
118
119 glBegin(GL_TRIANGLES);
120 glVertex3(480.0, 190.0, 64.0);
121 glVertex3(520.0, 190.0, 64.0);
122 glVertex3(500.0, 210.0, 64.0);
123 glEnd();
124
125 //glColor3f(1.0f, 1.0f, 1.0f);
126 }
127
128
129 Mf::Tilemap& Character::getTilemap()
130 {
131 return tilemap_;
132 }
133
134 Mf::Animation& Character::getAnimation()
135 {
136 return animation_;
137 }
138
139
140 /** vim: set ts=4 sw=4 tw=80: *************************************************/
141
This page took 0.034397 seconds and 3 git commands to generate.