]> Dogcows Code - chaz/yoink/blob - src/GameLayer.cc
new level-based controllers
[chaz/yoink] / src / GameLayer.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 <Moof/Engine.hh>
30 #include <Moof/Log.hh>
31 #include <Moof/Math.hh>
32 #include <Moof/OpenGL.hh>
33
34 #include "GameLayer.hh"
35
36 #if HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40
41 GameLayer::GameLayer() :
42 music("NightFusionIntro"),
43 punchSound("Thump")
44 {
45 music.setLooping(true);
46 music.enqueue("NightFusionLoop");
47 music.stream();
48
49 heroine = Character::alloc("RobotTrooper");
50 heroine->getAnimation().startSequence("Run");
51
52 Mf::Scalar a[6] = {0.0, 1.5, -0.5, 3.0, -2.0, 1.0};
53 interp.init(a, 2.0, Mf::Interpolator::OSCILLATE);
54
55 Mf::Scalar b[2] = {1.0, 0.0};
56 fadeIn.init(b, 1.0);
57
58 octree = Mf::loadScene("Classic");
59 heroine->treeNode = octree->insert(heroine);
60
61 camera.setProjection(cml::rad(60.0), 1.33333, 32.0, 2500.0);
62 camera.uploadProjectionToGL();
63 }
64
65
66 void GameLayer::pushed(Mf::Engine& engine)
67 {
68 hud = Hud::alloc();
69 engine.pushLayer(hud);
70 }
71
72
73 void GameLayer::update(Mf::Scalar t, Mf::Scalar dt)
74 {
75 //dt *= 0.7;
76
77 fadeIn.update(dt);
78 camera.update(t, dt);
79 heroine->update(t, dt);
80
81 // reinsert heroine
82 heroine->treeNode = octree->reinsert(heroine, heroine->treeNode);
83 octree->print(heroine->treeNode);
84
85 //camera.lookAt(heroine->getSphere().point);
86 camera.setPosition(Mf::Vector3(-heroine->current.position[0],
87 -heroine->current.position[1], -256));
88
89 Mf::Vector3 heroinePosition;
90 Mf::promoteVector(heroinePosition, heroine->current.position);
91 Mf::Sound::setListenerPosition(heroinePosition);
92
93 interp.update(dt);
94 hud->setBar1Progress(interp.getState(dt));
95 hud->setBar2Progress(1.0 - interp.getState(dt));
96 }
97
98
99 void GameLayer::draw(Mf::Scalar alpha) const
100 {
101 glMatrixMode(GL_MODELVIEW);
102 glLoadMatrix(camera.getModelviewMatrix().data());
103
104 // DRAW THE SCENE
105 Mf::Texture::resetBind();
106
107 glEnableClientState(GL_VERTEX_ARRAY);
108 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
109
110 octree->drawIfVisible(alpha, camera.getFrustum());
111
112 //heroine->draw(alpha);
113 heroine->getAabb().draw();
114
115 // DRAW FADE
116 glEnable(GL_BLEND);
117 glMatrixMode(GL_PROJECTION);
118 glPushMatrix();
119 glLoadIdentity();
120 glMatrixMode(GL_MODELVIEW);
121 glPushMatrix();
122 glLoadIdentity();
123 glColor4f(0.0f, 0.0f, 0.0f, fadeIn.getState(alpha));
124 Mf::Texture::resetBind();
125
126 //glRectf(-1.0f, -1.0f, 1.0f, 1.0f);
127 glBegin(GL_QUADS);
128 glVertex3f(-1.0, -1.0, -0.1);
129 glVertex3f(1.0, -1.0, -0.1);
130 glVertex3f(1.0, 1.0, -0.1);
131 glVertex3f(-1.0, 1.0, -0.1);
132 glEnd();
133
134 glDisable(GL_BLEND);
135
136 glMatrixMode(GL_PROJECTION);
137 glPopMatrix();
138 glMatrixMode(GL_MODELVIEW);
139 glPopMatrix();
140 }
141
142 bool GameLayer::handleEvent(const Mf::Event& event)
143 {
144 switch (event.type)
145 {
146 case SDL_KEYDOWN:
147 if (event.key.keysym.sym == SDLK_SPACE)
148 {
149 heroine->getAnimation().startSequence("Punch");
150 Mf::logInfo("thump!");
151 punchSound.play();
152 return true;
153 }
154 else if (event.key.keysym.sym == SDLK_p)
155 {
156 music.toggle();
157 return true;
158 }
159 else if (event.key.keysym.sym == SDLK_y)
160 {
161 Mf::Engine::getInstance().popLayer();
162 return true;
163 }
164
165 case SDL_KEYUP:
166 heroine->handleEvent(event);
167 break;
168
169 case SDL_MOUSEMOTION:
170 case SDL_MOUSEBUTTONDOWN:
171 camera.handleEvent(event);
172 return true;
173
174 case SDL_VIDEORESIZE:
175 glViewport(0, 0, event.resize.w, event.resize.h);
176 camera.setProjection(cml::rad(60.0),
177 double(event.resize.w) / double(event.resize.h), 32.0, 2500.0);
178 camera.uploadProjectionToGL();
179 break;
180 }
181
182 return false;
183 }
184
185
186 /** vim: set ts=4 sw=4 tw=80: *************************************************/
187
This page took 0.040981 seconds and 4 git commands to generate.