]> Dogcows Code - chaz/yoink/blob - src/GameLayer.cc
a80946f6f5ff68fd393ad165734871eaa7ba8cf7
[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 #include <Moof/Video.hh>
34
35 #include "GameLayer.hh"
36
37 #if HAVE_CONFIG_H
38 #include "config.h"
39 #endif
40
41
42 GameLayer::GameLayer() :
43 mMusic("BeatTheCube"),
44 mPunchSound("Thump")
45 {
46 mMusic.setLooping(true);
47 mMusic.enqueue("NightFusionLoop");
48 mMusic.stream();
49
50 mHeroine = Heroine::alloc();
51 mHeroine->animation.startSequence("FlyDiagonallyUp");
52
53 Mf::Scalar a[6] = {0.0, 1.5, -0.5, 3.0, -2.0, 1.0};
54 mInterp.init(a, 2.0, Mf::Interpolator::OSCILLATE);
55
56 mScene = Scene::alloc("Classic");
57
58 setProjection();
59
60 mHud = Hud::alloc();
61 }
62
63
64 void GameLayer::pushed(Mf::Engine& engine)
65 {
66 engine.push(mHud);
67 }
68
69
70 void GameLayer::update(Mf::Scalar t, Mf::Scalar dt)
71 {
72 mCamera.update(t, dt);
73 mHeroine->update(t, dt);
74
75 mScene->checkForCollision(*mHeroine);
76
77 mCamera.setPosition(Mf::Vector3(-mHeroine->getState().position[0],
78 -mHeroine->getState().position[1], -256));
79 //mCamera.lookAt(Mf::promote(mHeroine->getState().position));
80
81 //Mf::Vector3 heroinePosition = Mf::promote(mHeroine->getState().position);
82 //Mf::Sound::setListenerPosition(heroinePosition);
83
84 mInterp.update(t, dt);
85 mHud->setBar1Progress(mInterp.getState(dt));
86 mHud->setBar2Progress(1.0 - mInterp.getState(dt));
87 }
88
89
90 void GameLayer::draw(Mf::Scalar alpha) const
91 {
92 mCamera.uploadToGL(alpha);
93
94 // DRAW THE SCENE
95 Mf::Texture::resetBind();
96
97 glEnableClientState(GL_VERTEX_ARRAY);
98 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
99
100 mScene->drawIfVisible(alpha, mCamera.getFrustum());
101
102 mHeroine->draw(alpha);
103 }
104
105 bool GameLayer::handleEvent(const Mf::Event& event)
106 {
107 switch (event.type)
108 {
109 case SDL_KEYDOWN:
110 if (event.key.keysym.sym == SDLK_SPACE)
111 {
112 mHeroine->animation.startSequence("Flattened");
113 Mf::logInfo("thump!");
114 mPunchSound.play();
115 return true;
116 }
117 else if (event.key.keysym.sym == SDLK_p)
118 {
119 mMusic.toggle();
120 return true;
121 }
122 else if (event.key.keysym.sym == SDLK_y)
123 {
124 Mf::Engine::getInstance().pop();
125 return true;
126 }
127
128 case SDL_KEYUP:
129 return mHeroine->handleEvent(event);
130
131 case SDL_MOUSEMOTION:
132 case SDL_MOUSEBUTTONDOWN:
133 mCamera.handleEvent(event);
134 return true;
135
136 case SDL_VIDEORESIZE:
137 setProjection(Mf::Scalar(event.resize.w), Mf::Scalar(event.resize.h));
138 break;
139 }
140
141 return false;
142 }
143
144
145 void GameLayer::setProjection()
146 {
147 Mf::Video& video = Mf::Engine::getInstance().getVideo();
148 setProjection(video.getWidth(), video.getHeight());
149 }
150
151 void GameLayer::setProjection(Mf::Scalar width, Mf::Scalar height)
152 {
153 mCamera.setProjection(cml::rad(60.0), width / height, 32.0, 2500.0);
154 }
155
156
157 /** vim: set ts=4 sw=4 tw=80: *************************************************/
158
This page took 0.038788 seconds and 4 git commands to generate.