src/GameState.cc
author Tero Marttila <terom@fixme.fi>
Tue, 27 Jan 2009 00:25:58 +0200
changeset 439 9823e6cd1086
parent 423 947ab54de4b7
permissions -rw-r--r--
some README text
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"
417
c503e0c6a740 support for building without Network/Graphics, although the disable-graphics case is kind of hackish still
Tero Marttila <terom@fixme.fi>
parents: 412
diff changeset
     5
#include "Error.hh"
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     6
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
     7
GameState::GameState (Terrain &terrain) : 
423
947ab54de4b7 fix worst compilation errors when using the debian lenny gcc/libc...
Tero Marttila <terom@fixme.fi>
parents: 417
diff changeset
     8
    world(Vector(0, MAP_GRAVITY), Vector((float) terrain.getWidth(), (float) terrain.getHeight()), terrain), 
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
     9
    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
    10
    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
    11
{ 
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
    12
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 248
diff changeset
    13
}
393
5dd4d782cf3a a basic implementation of game messages, plus some weird GameStateEvent stuff
Tero Marttila <terom@fixme.fi>
parents: 282
diff changeset
    14
    
5dd4d782cf3a a basic implementation of game messages, plus some weird GameStateEvent stuff
Tero Marttila <terom@fixme.fi>
parents: 282
diff changeset
    15
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
    16
    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
    17
        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
    18
5dd4d782cf3a a basic implementation of game messages, plus some weird GameStateEvent stuff
Tero Marttila <terom@fixme.fi>
parents: 282
diff changeset
    19
    event_handler = handler;
5dd4d782cf3a a basic implementation of game messages, plus some weird GameStateEvent stuff
Tero Marttila <terom@fixme.fi>
parents: 282
diff changeset
    20
}
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 196
diff changeset
    21
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
    22
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
    23
    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
    24
}
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
    25
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 196
diff changeset
    26
LocalPlayer *GameState::getLocalPlayer (void) {
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 196
diff changeset
    27
    return local_player;
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    28
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    29
274
c35307e8645c add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents: 266
diff changeset
    30
void GameState::setLocalPlayer (LocalPlayer *player) {
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 196
diff changeset
    31
    if (local_player)
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 196
diff changeset
    32
        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
    33
    
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 196
diff changeset
    34
    local_player = player;
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 196
diff changeset
    35
}
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 199
diff changeset
    36
    
274
c35307e8645c add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents: 266
diff changeset
    37
void GameState::addPlayer (Player *player) {
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 196
diff changeset
    38
    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
    39
5dd4d782cf3a a basic implementation of game messages, plus some weird GameStateEvent stuff
Tero Marttila <terom@fixme.fi>
parents: 282
diff changeset
    40
    if (event_handler)
5dd4d782cf3a a basic implementation of game messages, plus some weird GameStateEvent stuff
Tero Marttila <terom@fixme.fi>
parents: 282
diff changeset
    41
        event_handler->on_player_joined(player);
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 196
diff changeset
    42
}
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    43
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 196
diff changeset
    44
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
    45
    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
    46
        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
    47
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 196
diff changeset
    48
    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
    49
    
5dd4d782cf3a a basic implementation of game messages, plus some weird GameStateEvent stuff
Tero Marttila <terom@fixme.fi>
parents: 282
diff changeset
    50
    if (event_handler)
5dd4d782cf3a a basic implementation of game messages, plus some weird GameStateEvent stuff
Tero Marttila <terom@fixme.fi>
parents: 282
diff changeset
    51
        event_handler->on_player_left(player);
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 196
diff changeset
    52
}
417
c503e0c6a740 support for building without Network/Graphics, although the disable-graphics case is kind of hackish still
Tero Marttila <terom@fixme.fi>
parents: 412
diff changeset
    53
c503e0c6a740 support for building without Network/Graphics, although the disable-graphics case is kind of hackish still
Tero Marttila <terom@fixme.fi>
parents: 412
diff changeset
    54
#if GRAPHICS_ENABLED
412
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 408
diff changeset
    55
void GameState::draw (graphics::Display &display, 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
    56
    // Draw terrain
412
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 408
diff changeset
    57
    terrain.draw(display, camera);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    58
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
    59
    // 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
    60
    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
    61
        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
    62
        
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
    63
        // 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
    64
        if (p == local_player)
412
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 408
diff changeset
    65
            local_player->draw(display, 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
    66
        else
412
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 408
diff changeset
    67
            p->draw(display, 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
    68
    }
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
    69
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
    70
    // 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
    71
    for (std::list<Projectile*>::iterator it = projectiles.begin(); it != projectiles.end(); it++) {
412
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 408
diff changeset
    72
        (*it)->draw(display, camera);
225
22ecb9cb9245 Rope can be drawn.
ekku
parents: 222
diff changeset
    73
    }
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
    74
}
417
c503e0c6a740 support for building without Network/Graphics, although the disable-graphics case is kind of hackish still
Tero Marttila <terom@fixme.fi>
parents: 412
diff changeset
    75
#endif
c503e0c6a740 support for building without Network/Graphics, although the disable-graphics case is kind of hackish still
Tero Marttila <terom@fixme.fi>
parents: 412
diff changeset
    76