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 187 188 189 190 191 192 193 194 195
|
#include "gamemgr.h"
#include "gameobj.h"
#include "scriptmgr.h"
#include <string.h> // strcpy
#include <stdio.h> // rand
#include <stdlib.h> // rand
#include <iostream> // cout
using namespace std;
CGameMgr::CGameMgr()
{
gameOn = false;
}
CGameMgr::~CGameMgr()
{
for( unsigned int n = 0; n < gameObjects.size(); n++ )
gameObjects[n]->DestroyAndRelease();
}
int CGameMgr::StartGame()
{
// Set up the game level
// In a real game this would probably be loaded from a file, that would define
// the position of each object and other things that would be placed in the game world.
// The properties for each object type would probably also be loaded from a file.
// The map file, would only have to specify the position and the type of the object.
// Based on the type, the game manager would retrieve the graphics object and script
// controller that should be used.
// Create some stones
for( unsigned int n = 0; n < 10; n++ )
SpawnObject("stone", '0', rand()%10, rand()%10);
// Create some zombies
for( unsigned int n = 0; n < 3; n++ )
SpawnObject("zombie", 'z', rand()%10, rand()%10);
// Create the player
CGameObj *obj = SpawnObject("player", 'p', rand()%10, rand()%10);
if( obj )
obj->name = "player";
// Check if there were any compilation errors during the script loading
if( scriptMgr->hasCompileErrors )
return -1;
return 0;
}
CGameObj *CGameMgr::SpawnObject(const std::string &type, char dispChar, int x, int y)
{
CGameObj *obj = new CGameObj(dispChar, x, y);
gameObjects.push_back(obj);
// Set the controller based on type
obj->controller = scriptMgr->CreateController(type, obj);
return obj;
}
void CGameMgr::Run()
{
gameOn = true;
while( gameOn )
{
// Render the frame
Render();
// Get input from user
GetInput();
// Call the onThink method on each game object
for( unsigned int n = 0; n < gameObjects.size(); n++ )
gameObjects[n]->OnThink();
// Kill the objects that have been queued for killing
for( unsigned int n = 0; n < gameObjects.size(); n++ )
{
if( gameObjects[n]->isDead )
{
// We won't actually delete the memory here, as we do not know
// exactly who might still be referencing the object, but we
// make sure to destroy the internals in order to avoid
// circular references.
gameObjects[n]->DestroyAndRelease();
gameObjects.erase(gameObjects.begin()+n);
n--;
}
}
}
}
void CGameMgr::EndGame(bool win)
{
gameOn = false;
if( win )
cout << "Congratulations, you've defeated the zombies!" << endl;
else
cout << "Too bad, the zombies ate your brain!" << endl;
// Get something to let the player see the message before exiting
char buf[2];
cin.getline(buf, 1);
}
void CGameMgr::Render()
{
// Clear the buffer
char buf[10][11];
for( int y = 0; y < 10; y++ )
memcpy(buf[y], "..........\0", 11);
// Render each object into the buffer
for( unsigned int n = 0; n < gameObjects.size(); n++ )
buf[gameObjects[n]->y][gameObjects[n]->x] = gameObjects[n]->displayCharacter;
// Clear the screen
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
// Print some useful information and start the input loop
cout << "Sample game using AngelScript " << asGetLibraryVersion() << "." << endl;
cout << "Type u(p), d(own), l(eft), r(ight) to move the player." << endl;
cout << "Type q(uit) to exit the game." << endl;
cout << "Try to avoid getting eaten by the zombies, hah hah." << endl;
cout << endl;
// Present the buffer
for( int y = 0; y < 10; y++ )
cout << buf[y] << endl;
}
void CGameMgr::GetInput()
{
cout << "> ";
char buf[10];
cin.getline(buf, 10);
memset(actionStates, 0, sizeof(actionStates));
switch( buf[0] )
{
case 'u':
actionStates[0] = true;
break;
case 'd':
actionStates[1] = true;
break;
case 'l':
actionStates[2] = true;
break;
case 'r':
actionStates[3] = true;
break;
case 'q':
gameOn = false;
break;
}
}
bool CGameMgr::GetActionState(int action)
{
if( action < 0 || action >= 4 ) return false;
return actionStates[action];
}
CGameObj *CGameMgr::GetGameObjAt(int x, int y)
{
for( unsigned int n = 0; n < gameObjects.size(); n++ )
if( gameObjects[n]->x == x && gameObjects[n]->y == y )
return gameObjects[n];
return 0;
}
CGameObj *CGameMgr::FindGameObjByName(const string &name)
{
for( unsigned int n = 0; n < gameObjects.size(); n++ )
{
if( gameObjects[n]->name == name )
return gameObjects[n];
}
return 0;
}
|