-
Notifications
You must be signed in to change notification settings - Fork 0
/
SelectLevel.cpp
186 lines (148 loc) · 4.62 KB
/
SelectLevel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <time.h>
#include "SelectLevel.h"
#include "d3dUtil.h"
#include "World.h"
#include "Player.h"
#include "Object3D.h"
#include "Mesh.h"
#include "SkinnedMesh.h"
#include "Light.h"
#include "EnemyHandler.h"
#include "Enemy.h"
#include "Input.h"
#include "Camera.h"
#include "PlayState.h"
#include "MainMenu.h"
SelectLevel SelectLevel::mSelectLevel;
void SelectLevel::init(Game* game)
{
// Important to set the game for changeState() to work!
setGame(game);
mMenu = new Menu("SelectLevelMenu", NavigationType::MOUSE, HOR);
mMenu->setSize(600, 400, 256, 512);
// [NOTE] It's actually World that should deletes the spawners, but since theres no World in the menu we have to do it manually.
// Very poorly structured...
gLevelHandler->deleteSpawners();
gLevelHandler->loadLevels();
for(int i = 0; i < gLevelHandler->getNumLevels(); i++) {
string name = gLevelHandler->getLevel(i)->getName();
LevelItem* item = new LevelItem(name, "data/imgs/buttons/level_standard_text.png", "data/imgs/buttons/level_glow_text.png", "data/imgs/buttons/level_invalid_text.png");
item->waves = gLevelHandler->getLevel(i)->getNumWaves();
item->completedWaves = gLevelHandler->getLevel(i)->getCompletedWaves();
// Not the first level.
if(i != 0) {
Level* lastLevel = gLevelHandler->getLevel(i-1);
if(lastLevel->getCompletedWaves() < lastLevel->getNumWaves())
item->state = INACTIVE;
}
mMenu->addMenuItem(item);
}
mMenu->buildMenu(128*1.2, 70*1.2);
mMenu->connect(&SelectLevel::menuMessage, this);
// Side menu.
mSideMenu = new Menu("SideMenu", NavigationType::MOUSE, HOR);
mSideMenu->setSize(1000, 700, 256, 512);
MenuItem* item = new MenuItem("MainMenu", "data/imgs/buttons/back_standard.png", "data/imgs/buttons/back_glow.png");
mSideMenu->addMenuItem(item);
mSideMenu->buildMenu(116*1.5, 50*1.5);
mSideMenu->connect(&SelectLevel::menuMessage, this);
// Create the world.
mWorld = new World();
mLight = new Light(D3DXVECTOR3(0, 25, 0), D3DXVECTOR3(0, -1, 0), Material(WHITE, 0), 32, 1600.0f);
mWorld->addLight(mLight);
mWorld->addAmbientLight(D3DCOLOR_ARGB(200, 100, 100, 100));
// Set the graphics light list.
gGraphics->setLightList(mWorld->getLights());
mLogo = gGraphics->loadTexture("data/imgs/logo.png");
// Setup the camera.
gCamera->setPosition(D3DXVECTOR3(0, 5000, 0));
gCamera->setDirection(D3DXVECTOR3(0, -0.6, 0.4));
}
void SelectLevel::cleanup()
{
delete mMenu;
delete mSideMenu;
delete mWorld;
gLevelHandler->deleteSpawners();
}
void SelectLevel::update(double dt)
{
// Spot light in the looking direction.
mLight->setPosition(gCamera->getPosition() + D3DXVECTOR3(0, 5, 0));
D3DXVECTOR3 t;
mLight->setDirection(-*D3DXVec3Normalize(&t, &(gCamera->getPosition() - gCamera->getTarget())));
// Only update the other menu if the state havent changed.
if(mMenu->update(gInput->mousePosition()))
mSideMenu->update(gInput->mousePosition());
gCamera->rotate(0, 0.001);
gCamera->updateView();
// Go to main menu on ESC.
if(gInput->keyPressed(VK_ESCAPE)) {
changeState(MainMenu::Instance());
ShowCursor(false);
}
}
void SelectLevel::draw()
{
mWorld->draw();
gGraphics->drawScreenTexture(mLogo, 600, 125, 610*1.2, 93*1.2);
mMenu->draw();
mSideMenu->draw();
}
void SelectLevel::onLostDevice()
{
mWorld->onLostDevice();
}
void SelectLevel::onResetDevice()
{
mWorld->onResetDevice();
}
void SelectLevel::pause()
{
}
void SelectLevel::resume()
{
}
bool SelectLevel::menuMessage(string message)
{
if(message == "MainMenu") {
changeState(MainMenu::Instance());
ShowCursor(false);
}
else {
changeState(PlayState::Instance());
PlayState::Instance()->setLevel(message);
}
return false;
}
void SelectLevel::msgProc(UINT msg, WPARAM wParam, LPARAM lParam)
{
}
LevelItem::LevelItem(string name, string standardTextureSource, string onSelectTextureSource, string inactiveTextureSource)
: MenuItem(name, standardTextureSource, onSelectTextureSource, inactiveTextureSource)
{
checkMark = gGraphics->loadTexture("data/imgs/checkmark.png");
numLevel = 1;
waves = 1;
completedWaves = 0;
}
LevelItem::~LevelItem()
{
}
void LevelItem::draw()
{
MenuItem::draw();
char buffer[256];
sprintf(buffer, "LEVEL %s", itemName.c_str());
float size = 30;
float yoffset = -5;
if(gGame->widthRatio() != 1.0f) {
size = 45;
yoffset = 10 * gGame->heightRatio();
}
gGraphics->drawText(buffer, rect.left * gGame->widthRatio() + 5 * gGame->widthRatio(), rect.top * gGame->heightRatio() + yoffset, WHITE, size);
if(this->state == INACTIVE)
gGraphics->drawScreenTexture(inactiveTexture, rect);
if(completedWaves == waves)
gGraphics->drawScreenTexture(checkMark, rect.left + rect.getWidth()/2, rect.top + 60, 26, 26);
}