src/GameState.cc
author Tero Marttila <terom@fixme.fi>
Tue, 20 Jan 2009 23:30:18 +0200
changeset 408 e6cfc44266af
parent 393 5dd4d782cf3a
child 412 721c60072091
permissions -rw-r--r--
reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     1
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     2
#include "GameState.hh"
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     3
#include "Engine.hh"
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     4
#include "Config.hh"
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     5
408
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 393
diff changeset
     6
GameState::GameState (Terrain &terrain) : 
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 393
diff changeset
     7
    world(Vector(0, MAP_GRAVITY), Vector(terrain.getWidth(), terrain.getHeight()), terrain), 
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 393
diff changeset
     8
    terrain(terrain),
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 393
diff changeset
     9
    local_player(NULL), event_handler(NULL)
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
    10
{ 
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
    11
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
    12
}
393
5dd4d782cf3a a basic implementation of game messages, plus some weird GameStateEvent stuff
Tero Marttila <terom@fixme.fi>
parents: 282
diff changeset
    13
    
5dd4d782cf3a a basic implementation of game messages, plus some weird GameStateEvent stuff
Tero Marttila <terom@fixme.fi>
parents: 282
diff changeset
    14
void GameState::setEventHandler (GameStateEventHandler *handler) {
5dd4d782cf3a a basic implementation of game messages, plus some weird GameStateEvent stuff
Tero Marttila <terom@fixme.fi>
parents: 282
diff changeset
    15
    if (handler && event_handler != NULL)
5dd4d782cf3a a basic implementation of game messages, plus some weird GameStateEvent stuff
Tero Marttila <terom@fixme.fi>
parents: 282
diff changeset
    16
        throw Error("event_handler already set");
5dd4d782cf3a a basic implementation of game messages, plus some weird GameStateEvent stuff
Tero Marttila <terom@fixme.fi>
parents: 282
diff changeset
    17
5dd4d782cf3a a basic implementation of game messages, plus some weird GameStateEvent stuff
Tero Marttila <terom@fixme.fi>
parents: 282
diff changeset
    18
    event_handler = handler;
5dd4d782cf3a a basic implementation of game messages, plus some weird GameStateEvent stuff
Tero Marttila <terom@fixme.fi>
parents: 282
diff changeset
    19
}
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 196
diff changeset
    20
222
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 209
diff changeset
    21
void GameState::addProjectile (Projectile *projectile) {
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 209
diff changeset
    22
    projectiles.push_back(projectile);
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 209
diff changeset
    23
}
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 209
diff changeset
    24
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 196
diff changeset
    25
LocalPlayer *GameState::getLocalPlayer (void) {
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 196
diff changeset
    26
    return local_player;
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    27
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    28
274
c35307e8645c add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents: 266
diff changeset
    29
void GameState::setLocalPlayer (LocalPlayer *player) {
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 196
diff changeset
    30
    if (local_player)
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 196
diff changeset
    31
        throw std::logic_error("newLocalPlayer called even though we already have a local player");
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 196
diff changeset
    32
    
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 196
diff changeset
    33
    local_player = player;
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 196
diff changeset
    34
}
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 199
diff changeset
    35
    
274
c35307e8645c add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents: 266
diff changeset
    36
void GameState::addPlayer (Player *player) {
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 196
diff changeset
    37
    player_list.push_back(player);
393
5dd4d782cf3a a basic implementation of game messages, plus some weird GameStateEvent stuff
Tero Marttila <terom@fixme.fi>
parents: 282
diff changeset
    38
5dd4d782cf3a a basic implementation of game messages, plus some weird GameStateEvent stuff
Tero Marttila <terom@fixme.fi>
parents: 282
diff changeset
    39
    if (event_handler)
5dd4d782cf3a a basic implementation of game messages, plus some weird GameStateEvent stuff
Tero Marttila <terom@fixme.fi>
parents: 282
diff changeset
    40
        event_handler->on_player_joined(player);
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 196
diff changeset
    41
}
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    42
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 196
diff changeset
    43
void GameState::removePlayer (Player *player) { 
274
c35307e8645c add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents: 266
diff changeset
    44
    if (player == local_player)
c35307e8645c add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents: 266
diff changeset
    45
        local_player = NULL;
c35307e8645c add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents: 266
diff changeset
    46
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 196
diff changeset
    47
    player_list.remove(player);
393
5dd4d782cf3a a basic implementation of game messages, plus some weird GameStateEvent stuff
Tero Marttila <terom@fixme.fi>
parents: 282
diff changeset
    48
    
5dd4d782cf3a a basic implementation of game messages, plus some weird GameStateEvent stuff
Tero Marttila <terom@fixme.fi>
parents: 282
diff changeset
    49
    if (event_handler)
5dd4d782cf3a a basic implementation of game messages, plus some weird GameStateEvent stuff
Tero Marttila <terom@fixme.fi>
parents: 282
diff changeset
    50
        event_handler->on_player_left(player);
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 196
diff changeset
    51
}
222
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 209
diff changeset
    52
    
266
ad72d0a0cc02 increase the map size and control the camera to always have the screen full of terrain
terom
parents: 263
diff changeset
    53
void GameState::draw(Graphics *g, PixelCoordinate camera, bool displayWeapon) {
408
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 393
diff changeset
    54
    // Draw terrain
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 393
diff changeset
    55
    terrain.draw(g, camera);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    56
222
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 209
diff changeset
    57
    // Draw players
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 209
diff changeset
    58
    for (std::list<Player*>::iterator it = player_list.begin(); it != player_list.end(); it++) {
233
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 225
diff changeset
    59
        Player *p = *it;
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 225
diff changeset
    60
        
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 225
diff changeset
    61
        // our LocalPlayer has it's own draw method
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 225
diff changeset
    62
        if (p == local_player)
248
e40ef56dc62c scrolling looks like it works
nireco
parents: 233
diff changeset
    63
            local_player->draw(g, displayWeapon, camera);
233
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 225
diff changeset
    64
        else
248
e40ef56dc62c scrolling looks like it works
nireco
parents: 233
diff changeset
    65
            p->draw(g, camera);
222
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 209
diff changeset
    66
    }
233
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 225
diff changeset
    67
222
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 209
diff changeset
    68
    // Draw projectiles
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 209
diff changeset
    69
    for (std::list<Projectile*>::iterator it = projectiles.begin(); it != projectiles.end(); it++) {
248
e40ef56dc62c scrolling looks like it works
nireco
parents: 233
diff changeset
    70
        (*it)->draw(g, camera);
225
22ecb9cb9245 Rope can be drawn.
ekku
parents: 222
diff changeset
    71
    }
222
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 209
diff changeset
    72
}
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 209
diff changeset
    73