]> Dogcows Code - chaz/yoink/blob - src/GameLayer.cc
preliminary AM_SILENT_RULES support
[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/Exception.hh>
31 #include <Moof/Log.hh>
32 #include <Moof/Math.hh>
33 #include <Moof/OpenGL.hh>
34 #include <Moof/Video.hh>
35
36 #include "GameLayer.hh"
37 #include "Hud.hh"
38
39 #if HAVE_CONFIG_H
40 #include "config.h"
41 #endif
42
43
44
45 Mf::Scalar GameLayer::getZCoord(const Mf::Vector2& position) const
46 {
47 Mf::Scalar z;
48
49 mState.script.getGlobalTable().pushField("GetZCoord");
50 mState.script.push(position[0]);
51 mState.script.push(position[1]);
52 mState.script.call(2, 1);
53 mState.script.getTop().get(z);
54 mState.script.pop();
55
56 return z;
57 }
58
59 void GameLayer::loadSceneLoader()
60 {
61 mState.script.importStandardLibraries();
62 importLogPrintFunction(mState.script);
63
64 std::string loaderPath = Scene::getPath("loader");
65 if (loaderPath == "")
66 {
67 throw Mf::Exception(Mf::ErrorCode::RESOURCE_NOT_FOUND, "loader");
68 }
69
70 Mf::Script::Status status = mState.script.doFile(loaderPath);
71 if (status != Mf::Script::SUCCESS)
72 {
73 std::string str;
74 mState.script[-1].get(str);
75
76 Mf::logScript("%s", str.c_str());
77 throw Mf::Exception(Mf::ErrorCode::SCRIPT_ERROR, str);
78 }
79
80 mState.script.getGlobalTable().pushField("scenes");
81 mState.script.getTop().get(mState.sceneList);
82 if (mState.sceneList.size() == 0)
83 {
84 Mf::logScript("no variable `scenes' within loader");
85 throw Mf::Exception(Mf::ErrorCode::SCRIPT_ERROR, "no scenes to load");
86 }
87 }
88
89 void GameLayer::advanceScene()
90 {
91 if (mState.sceneList.size() != 0)
92 {
93 mState.scene = Scene::alloc(mState.sceneList[0]);
94 mState.sceneList.erase(mState.sceneList.begin());
95 mState.scene->load(mState.script);
96 }
97 }
98
99
100 GameLayer::GameLayer() :
101 mMusic("NightFusionIntro"),
102 mPunchSound("Thump")
103 {
104 mMusic.setLooping(true);
105 mMusic.enqueue("NightFusionLoop");
106 mMusic.stream();
107
108 loadSceneLoader();
109 advanceScene(); // load the first scene
110
111 mState.heroine = Heroine::alloc();
112 mState.heroine->animation.startSequence("FlyDiagonallyUp");
113
114 Mf::Scalar a[6] = {0.0, 1.5, -0.5, 3.0, -2.0, 1.0};
115 mState.interp.init(a, 2.0, Mf::Interpolator::OSCILLATE);
116
117 setProjection();
118 }
119
120
121 void GameLayer::pushed(Mf::Engine& engine)
122 {
123 engine.push(Hud::alloc(mState));
124
125 mRay.direction.set(1.0, 0.0);
126
127 mLine.a.set(20, 10);
128 mLine.b.set(19, 14);
129
130 mPlane.normal.set(-1.0, 0.0, 0.0);
131 mPlane.d = 0.0;
132
133 mSphere.point.set(22, 5);
134 mSphere.radius = 2;
135 }
136
137
138 void GameLayer::update(Mf::Engine& engine, Mf::Scalar t, Mf::Scalar dt)
139 {
140 mState.camera.update(t, dt);
141 mState.heroine->update(t, dt);
142
143 mState.scene->checkForCollision(*mState.heroine);
144
145 mState.camera.setPosition(Mf::Vector3(-mState.heroine->getState().position[0],
146 -mState.heroine->getState().position[1], -9));
147 //mState.camera.lookAt(Mf::promote(mState.heroine->getState().position));
148
149 //Mf::Vector3 heroinePosition = Mf::promote(mState.heroine->getState().position);
150 //Mf::Sound::setListenerPosition(heroinePosition);
151
152 mRay.point = mState.heroine->getState().position;
153
154 Mf::Ray<2>::Intersection meh;
155
156 Mf::Scalar d = mLine.intersectRay(mRay, meh);
157 if (d > 0.0)
158 {
159 Mf::logDebug("line: d = %f", d);
160 Mf::logDebug(" P = <%f,%f>", meh.point[0], meh.point[1]);
161 Mf::logDebug(" n = <%f,%f>", meh.normal[0], meh.normal[1]);
162 }
163 //d = mPlane.intersectRay(mRay, meh);
164 //if (d > 0.0)
165 //{
166 //Mf::logDebug("plane: d = %f", d);
167 //Mf::logDebug(" P = <%f,%f>", meh.point[0], meh.point[1]);
168 //Mf::logDebug(" n = <%f,%f>", meh.normal[0], meh.normal[1]);
169 //}
170 d = mSphere.intersectRay(mRay, meh);
171 if (d > 0.0)
172 {
173 Mf::logDebug("sphere: d = %f", d);
174 Mf::logDebug(" P = <%f,%f>", meh.point[0], meh.point[1]);
175 Mf::logDebug(" n = <%f,%f>", meh.normal[0], meh.normal[1]);
176 }
177 }
178
179
180 void GameLayer::draw(Mf::Engine& engine, Mf::Scalar alpha) const
181 {
182 mState.camera.uploadToGL(alpha);
183
184 // DRAW THE SCENE
185 Mf::Texture::resetBind();
186
187 glEnableClientState(GL_VERTEX_ARRAY);
188 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
189
190 mState.scene->drawIfVisible(alpha, mState.camera.getFrustum());
191
192 mState.heroine->setZCoord(getZCoord(mState.heroine->getState().position));
193 mState.heroine->draw(alpha);
194
195 mRay.draw();
196 mLine.draw();
197 mSphere.draw();
198 }
199
200 bool GameLayer::handleEvent(Mf::Engine& engine, const Mf::Event& event)
201 {
202 switch (event.type)
203 {
204 case SDL_KEYDOWN:
205 if (event.key.keysym.sym == SDLK_SPACE)
206 {
207 mState.heroine->animation.startSequence("Flattened");
208 Mf::logInfo("thump!");
209 mPunchSound.play();
210 return true;
211 }
212 else if (event.key.keysym.sym == SDLK_m)
213 {
214 mMusic.toggle();
215 return true;
216 }
217 else if (event.key.keysym.sym == SDLK_PAGEUP)
218 {
219 mRay.direction = cml::rotate_vector_2D(mRay.direction, cml::rad(10.0));
220 return true;
221 }
222 else if (event.key.keysym.sym == SDLK_PAGEDOWN)
223 {
224 mRay.direction = cml::rotate_vector_2D(mRay.direction, cml::rad(-10.0));
225 return true;
226 }
227 return mState.heroine->handleEvent(event);
228
229 case SDL_KEYUP:
230 if (event.key.keysym.sym == SDLK_ESCAPE)
231 {
232 engine.pop(this);
233 return true;
234 }
235 else if (event.key.keysym.sym == SDLK_h)
236 {
237 engine.push(Hud::alloc(mState));
238 return true;
239 }
240 return mState.heroine->handleEvent(event);
241
242 case SDL_MOUSEMOTION:
243 case SDL_MOUSEBUTTONDOWN:
244 mState.camera.handleEvent(event);
245 return true;
246
247 case SDL_VIDEORESIZE:
248 setProjection(event.resize.w, event.resize.h);
249 break;
250 }
251
252 return false;
253 }
254
255
256 void GameLayer::setProjection()
257 {
258 Mf::VideoP video = Mf::Engine::getInstance().getVideo();
259 setProjection(video->getWidth(), video->getHeight());
260 }
261
262 void GameLayer::setProjection(Mf::Scalar width, Mf::Scalar height)
263 {
264 mState.camera.setProjection(cml::rad(60.0), width / height, 1.0, 200.0);
265 }
266
267
268 /** vim: set ts=4 sw=4 tw=80: *************************************************/
269
This page took 0.040382 seconds and 4 git commands to generate.