00001 #include <stdlib.h>
00002 #include <vector>
00003 #include <iostream>
00004 #include <assert.h>
00005
00006 #include "input/Input.h"
00007 #include "resources/resources.h"
00008
00009 #include "client.h"
00010 #include "textures.h"
00011
00012 #include "net/MMNet.h"
00013 #include "LoadingGS.h"
00014 #include "LobbyGS.h"
00015
00016 using namespace MMGUI;
00017 using namespace MMNet;
00018 using namespace TNL;
00019 using namespace std;
00020
00021 static GameState *thisGS;
00022
00023 static void returnToMainMenu(const char *str) {
00024 setGameState(NULL);
00025
00026 thisGS->client->disconnectFromServer();
00027 }
00028
00029 static void toggleReady(const char *str) {
00030 thisGS->client->toggleReady();
00031 }
00032
00033 static void changeModelRobin(const char *str) {
00034 thisGS->client->changeModel(PlayerModelRobinHood);
00035 }
00036
00037 static void changeModelLittleJohn(const char *str) {
00038 thisGS->client->changeModel(PlayerModelLittleJohn);
00039 }
00040
00041 static void changeModelSheriff(const char *str) {
00042 thisGS->client->changeModel(PlayerModelSheriff);
00043 }
00044
00045 static void changeModelGuard(const char *str) {
00046 thisGS->client->changeModel(PlayerModelGuard);
00047 }
00048
00049 static void changeModelNone(const char *str) {
00050 thisGS->client->changeModel(PlayerModelNone);
00051 }
00052
00053
00054 LobbyGS::LobbyGS(NetClient *client) : GameState(client) {
00055 assert(client != NULL);
00056
00057 for(int i = 0; i < MAX_PLAYERS; i++) {
00058 slots[i] = NULL;
00059 }
00060 }
00061
00062 void LobbyGS::init() {
00063 thisGS = this;
00064
00065 menuInit();
00066
00067 initSlots();
00068
00069 callback = new LobbyCallback(this);
00070 client->setGameCallback(callback);
00071
00072 Frame *pane = new Frame(WidgetAlignCenter, 800, 600);
00073 getRoot()->addChild(pane);
00074
00075 exitButton = new Button(WidgetAlignBottomLeft, 100, 50);
00076 exitButton->setUpImage(TEXTURE_EXIT_BUTTON);
00077 exitButton->setDownImage(TEXTURE_EXIT_BUTTON_ON);
00078 exitButton->setOverImage(TEXTURE_EXIT_BUTTON_OVER);
00079 exitButton->setClickFunction(returnToMainMenu);
00080 pane->addChild(exitButton);
00081
00082 readyButton = new Button(WidgetAlignBottom, 140, 70);
00083 readyButton->setUpImage(TEXTURE_READY_BUTTON);
00084 readyButton->setDownImage(TEXTURE_READY_BUTTON_ON);
00085 readyButton->setOverImage(TEXTURE_READY_BUTTON_OVER);
00086 readyButton->setClickFunction(toggleReady);
00087 pane->addChild(readyButton);
00088
00089 pane->addChild(initModelButtons());
00090
00091 glutPostRedisplay();
00092 }
00093
00094
00095 Frame *LobbyGS::initModelButtons() {
00096
00097 Frame *topFrame = new Frame(WidgetAlignTop, 300, 110);
00098 topFrame->setBorder(1);
00099 topFrame->setMargin(1);
00100 topFrame->setBorderColor(1,1,0);
00101
00102 Textfield *label = new Textfield(WidgetAlignTop, 300, 20);
00103 label->setText("Choose your character:");
00104 label->alignCenter();
00105 label->setFontColor(1,0,0);
00106 topFrame->addChild(label);
00107
00108 charName = new Textfield(WidgetAlignBottom, 300, 20);
00109 charName->clearText();
00110 charName->alignCenter();
00111 charName->setFontColor(1,0,0);
00112 topFrame->addChild(charName);
00113
00114
00115 Frame *modelButtons = new Frame(WidgetAlignCenter, 270, 56);
00116 topFrame->addChild(modelButtons);
00117
00118 robinButton = new Button(0, 0, 50, 50);
00119 robinButton->setUpImage(TEXTURE_HEADSHOT_ROBIN);
00120 robinButton->setDownImage(TEXTURE_HEADSHOT_ROBIN);
00121 robinButton->setOverBorderColor(0,1,0);
00122 robinButton->setOverBorder(2);
00123 robinButton->setClickFunction(changeModelRobin);
00124 modelButtons->addChild(robinButton);
00125
00126 littleJohnButton = new Button(55, 0, 50, 50);
00127 littleJohnButton->setUpImage(TEXTURE_HEADSHOT_LITTLEJOHN);
00128 littleJohnButton->setDownImage(TEXTURE_HEADSHOT_LITTLEJOHN);
00129 littleJohnButton->setOverBorderColor(0,1,0);
00130 littleJohnButton->setOverBorder(2);
00131 littleJohnButton->setClickFunction(changeModelLittleJohn);
00132 modelButtons->addChild(littleJohnButton);
00133
00134 noneButton = new Button(110, 0, 50, 50);
00135 noneButton->setUpImage(TEXTURE_WHITE_BUTTON);
00136 noneButton->setDownImage(TEXTURE_WHITE_BUTTON);
00137 noneButton->setOverBorderColor(1,0,0);
00138 noneButton->setOverBorder(2);
00139 noneButton->setClickFunction(changeModelNone);
00140 modelButtons->addChild(noneButton);
00141
00142 guardButton = new Button(165, 0, 50, 50);
00143 guardButton->setUpImage(TEXTURE_HEADSHOT_GUARD);
00144 guardButton->setDownImage(TEXTURE_HEADSHOT_GUARD);
00145 guardButton->setOverBorderColor(0.5, 0.2, 0);
00146 guardButton->setOverBorder(2);
00147 guardButton->setClickFunction(changeModelGuard);
00148 modelButtons->addChild(guardButton);
00149
00150 sheriffButton = new Button(220, 0, 50, 50);
00151 sheriffButton->setUpImage(TEXTURE_HEADSHOT_SHERIFF);
00152 sheriffButton->setDownImage(TEXTURE_HEADSHOT_SHERIFF);
00153 sheriffButton->setOverBorderColor(0.5, 0.2, 0);
00154 sheriffButton->setOverBorder(2);
00155 sheriffButton->setClickFunction(changeModelSheriff);
00156 modelButtons->addChild(sheriffButton);
00157
00158 return topFrame;
00159 }
00160
00161 void LobbyGS::disableModelButtons() {
00162 sheriffButton->setDisabled(true);
00163 robinButton->setDisabled(true);
00164 guardButton->setDisabled(true);
00165 littleJohnButton->setDisabled(true);
00166 noneButton->setDisabled(true);
00167 }
00168
00169 void LobbyGS::enableModelButtons() {
00170 sheriffButton->setDisabled(false);
00171 robinButton->setDisabled(false);
00172 guardButton->setDisabled(false);
00173 littleJohnButton->setDisabled(false);
00174 noneButton->setDisabled(false);
00175 }
00176
00177
00178 void LobbyGS::updateModelButtons() {
00179
00180
00181 if(client->game->myPlayer && client->game->myPlayer->isReady()) {
00182 return;
00183 }
00184
00185 #if 0
00186
00187
00188
00189
00190 enum PlayerModel model;
00191
00192
00193 enableModelButtons();
00194 for(int i = 0; i < MAX_PLAYERS; i++) {
00195 if(slots[i] != NULL && slots[i]->isValid() == true) {
00196 model = slots[i]->getModel();
00197
00198 switch(model) {
00199 case PlayerModelSheriff:
00200 sheriffButton->setDisabled(true); break;
00201 case PlayerModelGuard:
00202 guardButton->setDisabled(true); break;
00203 case PlayerModelRobinHood:
00204 robinButton->setDisabled(true); break;
00205 case PlayerModelLittleJohn:
00206 littleJohnButton->setDisabled(true); break;
00207 default:
00208 break;
00209 }
00210 }
00211 }
00212
00213 #endif
00214
00215
00216 if(lastOver != NULL && lastOver->isMouseOver()) {
00217 return;
00218 }
00219
00220
00221 if(robinButton->isMouseOver()) {
00222 charName->setText("Robin Hood");
00223 lastOver = robinButton;
00224 } else if(littleJohnButton->isMouseOver()) {
00225 charName->setText("Little John");
00226 lastOver = littleJohnButton;
00227 } else if(guardButton->isMouseOver()) {
00228 charName->setText("Sheriff's Guard");
00229 lastOver = guardButton;
00230 } else if(sheriffButton->isMouseOver()) {
00231 charName->setText("The Sheriff");
00232 lastOver = sheriffButton;
00233 } else if(noneButton->isMouseOver()) {
00234 charName->clearText();
00235 lastOver = noneButton;
00236 } else {
00237
00238
00239 if(lastOver == NULL) {
00240 return;
00241 }
00242 lastOver = NULL;
00243 charName->clearText();
00244 }
00245 glutPostRedisplay();
00246 }
00247
00248 void LobbyGS::display(void) {
00249 glClearColor(0,0,0,0);
00250 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00251
00252 menuDraw();
00253
00254 glutSwapBuffers();
00255 }
00256
00257 void LobbyGS::exit() {
00258 for(int i = 0; i < MAX_PLAYERS; i++) {
00259 delete slots[i];
00260 }
00261
00262 client->setGameCallback(NULL);
00263 delete callback;
00264 menuClear();
00265 }
00266
00267 void LobbyGS::update(void) {
00268 updateSlots();
00269 updateModelButtons();
00270 }
00271
00272 void LobbyGS::setupGame(TNL::U32 seed) {
00273 this->seed = seed;
00274
00275 client->game->levelSeed = seed;
00276
00277 setGameState(new LoadingGS(client));
00278 }
00279
00280 void LobbyGS::updateSlots() {
00281 for(int i = 0; i < MAX_PLAYERS; i++) {
00282 if(slots[i] != NULL) {
00283 slots[i]->update();
00284 }
00285 }
00286 }
00287
00288 void LobbyGS::initSlots() {
00289
00290 Frame *slotFrame = new Frame(WidgetAlignCenter, (SLOT_WIDTH * 4) + (SLOT_MARGIN * 3), SLOT_HEIGHT);
00291 getRoot()->addChild(slotFrame);
00292
00293
00294 for(int i = 0; i < MAX_PLAYERS; i++) {
00295
00296 if(slots[i] != NULL) {
00297 slotFrame->removeChild(slots[i]);
00298 delete slots[i];
00299 }
00300 slots[i] = new PlayerSlot(client->game, this, i, (i * (SLOT_WIDTH + SLOT_MARGIN)), 0);
00301 slotFrame->addChild(slots[i]);
00302 }
00303 }
00304
00305 bool LobbyGS::allReady() {
00306 for(int i = 0; i < MAX_PLAYERS; i++) {
00307 if(slots[i]->isValid() == true && slots[i]->isReady() == false) {
00308 return false;
00309 }
00310 }
00311 return true;
00312 }
00313