]> Dogcows Code - chaz/yoink/blob - src/GameLayer.cc
big batch of changes
[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
38 #if HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41
42
43
44 Mf::Scalar GameLayer::getZCoord(const Mf::Vector2& position) const
45 {
46 Mf::Scalar z;
47
48 mScript.getGlobalTable().pushField("GetZCoord");
49 mScript.push(position[0]);
50 mScript.push(position[1]);
51 mScript.call(2, 1);
52 mScript.getTop().get(z);
53 mScript.pop();
54
55 return z;
56 }
57
58 void GameLayer::loadSceneLoader()
59 {
60 std::string loaderPath = Scene::getPath("loader");
61 if (loaderPath == "")
62 {
63 throw Mf::Exception(Mf::ErrorCode::RESOURCE_NOT_FOUND, "loader");
64 }
65
66 Mf::Script::Status status = mScript.doFile(loaderPath);
67 if (status != Mf::Script::SUCCESS)
68 {
69 std::string str;
70 mScript[-1].get(str);
71
72 Mf::logScript("%s", str.c_str());
73 throw Mf::Exception(Mf::ErrorCode::SCRIPT_ERROR, str.c_str());
74 }
75
76 mScript.getGlobalTable().pushField("scenes");
77 mScript.getTop().get(mSceneList);
78 if (mSceneList.size() == 0)
79 {
80 Mf::logScript("no variable `scenes' within loader");
81 throw Mf::Exception(Mf::ErrorCode::SCRIPT_ERROR, "no scenes to load");
82 }
83 }
84
85 void GameLayer::advanceScene()
86 {
87 if (mSceneList.size() != 0)
88 {
89 mScene = Scene::alloc(mSceneList[0]);
90 mSceneList.erase(mSceneList.begin());
91 mScene->load(mScript);
92 }
93 }
94
95
96 GameLayer::GameLayer() :
97 mMusic("NightFusionIntro"),
98 mPunchSound("Thump")
99 {
100 mMusic.setLooping(true);
101 mMusic.enqueue("NightFusionLoop");
102 mMusic.stream();
103
104 loadSceneLoader();
105 advanceScene(); // load the first scene
106
107 mHeroine = Heroine::alloc();
108 mHeroine->animation.startSequence("FlyDiagonallyUp");
109
110 Mf::Scalar a[6] = {0.0, 1.5, -0.5, 3.0, -2.0, 1.0};
111 mInterp.init(a, 2.0, Mf::Interpolator::OSCILLATE);
112
113 setProjection();
114
115 mHud = Hud::alloc();
116 }
117
118
119 void GameLayer::pushed(Mf::Engine& engine)
120 {
121 engine.push(mHud);
122 }
123
124
125 void GameLayer::update(Mf::Scalar t, Mf::Scalar dt)
126 {
127 mCamera.update(t, dt);
128 mHeroine->update(t, dt);
129
130 mScene->checkForCollision(*mHeroine);
131
132 mCamera.setPosition(Mf::Vector3(-mHeroine->getState().position[0],
133 -mHeroine->getState().position[1], -10));
134 //mCamera.lookAt(Mf::promote(mHeroine->getState().position));
135
136 //Mf::Vector3 heroinePosition = Mf::promote(mHeroine->getState().position);
137 //Mf::Sound::setListenerPosition(heroinePosition);
138
139 mInterp.update(t, dt);
140 mHud->setBar1Progress(mInterp.getState(dt));
141 mHud->setBar2Progress(1.0 - mInterp.getState(dt));
142 }
143
144
145 void GameLayer::draw(Mf::Scalar alpha) const
146 {
147 mCamera.uploadToGL(alpha);
148
149 // DRAW THE SCENE
150 Mf::Texture::resetBind();
151
152 glEnableClientState(GL_VERTEX_ARRAY);
153 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
154
155 mScene->drawIfVisible(alpha, mCamera.getFrustum());
156
157 mHeroine->setZCoord(getZCoord(mHeroine->getState().position));
158 mHeroine->draw(alpha);
159 }
160
161 bool GameLayer::handleEvent(const Mf::Event& event)
162 {
163 switch (event.type)
164 {
165 case SDL_KEYDOWN:
166 if (event.key.keysym.sym == SDLK_SPACE)
167 {
168 mHeroine->animation.startSequence("Flattened");
169 Mf::logInfo("thump!");
170 mPunchSound.play();
171 return true;
172 }
173 else if (event.key.keysym.sym == SDLK_p)
174 {
175 mMusic.toggle();
176 return true;
177 }
178 else if (event.key.keysym.sym == SDLK_y)
179 {
180 Mf::Engine::getInstance().pop();
181 return true;
182 }
183
184 case SDL_KEYUP:
185 return mHeroine->handleEvent(event);
186
187 case SDL_MOUSEMOTION:
188 case SDL_MOUSEBUTTONDOWN:
189 mCamera.handleEvent(event);
190 return true;
191
192 case SDL_VIDEORESIZE:
193 setProjection(Mf::Scalar(event.resize.w), Mf::Scalar(event.resize.h));
194 break;
195 }
196
197 return false;
198 }
199
200
201 void GameLayer::setProjection()
202 {
203 Mf::Video& video = Mf::Engine::getInstance().getVideo();
204 setProjection(video.getWidth(), video.getHeight());
205 }
206
207 void GameLayer::setProjection(Mf::Scalar width, Mf::Scalar height)
208 {
209 mCamera.setProjection(cml::rad(60.0), width / height, 1.0, 200.0);
210 }
211
212
213 /** vim: set ts=4 sw=4 tw=80: *************************************************/
214
This page took 0.039185 seconds and 5 git commands to generate.