Jeejee, hirvee hinaus ohi toistaseks.
authorsaiam
Thu, 04 Dec 2008 21:10:41 +0000
changeset 199 f5c86420facd
parent 198 8698cbb101df
child 200 2dbf40661580
Jeejee, hirvee hinaus ohi toistaseks.
src/GameState.cc
src/GameState.hh
src/Graphics.cc
src/PhysicsWorld.hh
src/Player.cc
src/Player.hh
src/Projectile.cc
--- a/src/GameState.cc	Thu Dec 04 19:54:51 2008 +0000
+++ b/src/GameState.cc	Thu Dec 04 21:10:41 2008 +0000
@@ -3,116 +3,29 @@
 #include "Engine.hh"
 #include "Config.hh"
 
-/**
- * shoots the selected weapon.
- * TODO: selection and weapon information
- */
-void Player::shoot (void) {
-    // here should be somehow considered which projectile it is
-    if(!canShoot())
-        return;
-    reloadTimer += 0;
-    Vector unitVectorAim = facingRight ? Vector(std::cos(aim), -std::sin(aim)) : 
-            Vector(-std::cos(aim), -std::sin(aim));
-    float shotspeed = 100*PHYSICS_TICK_MS;
-    Vector shotRelativeVelocity = unitVectorAim * shotspeed;
-    Vector shotVelocity = this->velocity + shotRelativeVelocity;
-    this->state.addProjectile(new Shot(this->state, this->position, shotVelocity, true));
+GameState::GameState (void) : local_player(NULL), world(Vector(0, MAP_GRAVITY), Vector(MAP_WIDTH, MAP_HEIGHT)) { }
+
+LocalPlayer *GameState::getLocalPlayer (void) {
+    return local_player;
 }
 
-void Player::handleMove (PlayerInput_Move input) {
-    float fx = 0; // Force in x-direction
-    float da = 0; // Crosshair angle
-
-    // handle left/right
-    if ((input & INPUT_MOVE_LEFT) && (velocity.x > -PLAYER_MAX_SPEED))
-        fx -= PLAYER_MOVE_FORCE;
-
-    if ((input & INPUT_MOVE_RIGHT) && (velocity.x < PLAYER_MAX_SPEED))
-        fx += PLAYER_MOVE_FORCE;
-
-    if (input & INPUT_MOVE_UP)
-        da += CROSSHAIR_ANGLE_SPEED;
-
-    if (input & INPUT_MOVE_DOWN)
-        da -= CROSSHAIR_ANGLE_SPEED;
+void GameState::newLocalPlayer (LocalPlayer *player) {
+    if (local_player)
+        throw std::logic_error("newLocalPlayer called even though we already have a local player");
+    
+    player_list.push_back(player);
+    
+    local_player = player;
+}
 
-    if (input & INPUT_MOVE_JUMP) {
-        if ((input & INPUT_MOVE_LEFT))
-            jump(-1);
-        else if ((input & INPUT_MOVE_RIGHT))
-            jump(1);
-        else
-            jump(0);
-    }
+void GameState::newRemotePlayer (RemotePlayer *player) {
+    player_list.push_back(player);
+}
 
-    if (input & INPUT_MOVE_DIG) {
-        // Should create Shot which destroys ground, but also should be destroyed then,
-        // but it doesn't.
-        // But this now just segfaults
-//        world.addObject(new Shot(state, position, true));
-
-        world.removeGround(position, 15);
-    }
-
-    if (input & INPUT_SHOOT) {
-        this->shoot();
-    }
+void GameState::removePlayer (Player *player) { 
+    player_list.remove(player);
+}
 
 
 
-    // Player facing
-    if (fx < 0) setFacing(false);
-    else if (fx > 0) setFacing(true);
-
-
-    this->changeAim(da); // Move crosshair
-
-    // Apply force
-    applyForce(Vector(fx, 0));
-
-}
-
-void Player::debugInfo (void) {
-    Engine::log(DEBUG, "Player.debugInfo") << "In air: " << this->inAir;
-}
-
-void Shot::onCollision() {
-    world.removeGround(position, 20);
-    this->destroyed = true;
-}
-
-bool Shot::isDestroyed (void) {
-    return this->destroyed;
-}
-
-void Shot::draw(CL_GraphicContext *gc) {
 
-
-    CL_Quad player(
-                   (int)((position).x+1), (int)((position).y+1),
-                   (int)((position).x-1), (int)((position).y+1),
-                   (int)((position).x+1), (int)((position).y-1),
-                   (int)((position).x-1), (int)((position).y-1)
-                   );
-
-    gc->fill_quad(player, CL_Color::green);
-
-    const uint16_t chlen = 10;
-    uint16_t x = player.center().x;
-    uint16_t y = player.center().y;
-    if(target_visible) {
-        if (facingRight) {
-            gc->draw_line(x, y,
-                          x + std::cos(aim)*chlen,
-                          y - std::sin(aim)*chlen,
-                          CL_Color::black);
-        } else {
-            gc->draw_line(x, y,
-                          x - std::cos(aim)*chlen,
-                          y - std::sin(aim)*chlen,
-                          CL_Color::black);
-        }
-    }
-}
-
--- a/src/GameState.hh	Thu Dec 04 19:54:51 2008 +0000
+++ b/src/GameState.hh	Thu Dec 04 21:10:41 2008 +0000
@@ -4,7 +4,6 @@
 class GameState;
 
 #include "PhysicsWorld.hh"
-#include "PhysicsObject.hh"
 #include "Player.hh"
 #include "Projectile.hh"
 #include "Input.hh"
@@ -14,40 +13,26 @@
 #include <stdexcept>
 #include <cmath>
 
-class GameState : public PhysicsWorld {
+class GameState {
 public:
     std::list<Player*> player_list;
+    PhysicsWorld world;
 
     // only one local player is supported
     LocalPlayer *local_player;
 
-    GameState (void) : PhysicsWorld(Vector(0, MAP_GRAVITY), Vector(MAP_WIDTH, MAP_HEIGHT)), local_player(NULL) {
+    GameState (void);
 
-    }
-       
     /*
      * This will return NULL if we don't have a local player - yet
      */
-    LocalPlayer *getLocalPlayer (void) {
-        return local_player;
-    }
-        
-    void newLocalPlayer (LocalPlayer *player) {
-        if (local_player)
-            throw std::logic_error("newLocalPlayer called even though we already have a local player");
-
-        player_list.push_back(player);
+    LocalPlayer *getLocalPlayer (void);
 
-        local_player = player;
-    }
+    void newLocalPlayer (LocalPlayer *player);        
 
-    void newRemotePlayer (RemotePlayer *player) {
-        player_list.push_back(player);
-    }
+    void newRemotePlayer (RemotePlayer *player);
 
-    void removePlayer (Player *player) {
-        player_list.remove(player);
-    }
+    void removePlayer (Player *player);
 };
 
 #endif
--- a/src/Graphics.cc	Thu Dec 04 19:54:51 2008 +0000
+++ b/src/Graphics.cc	Thu Dec 04 21:10:41 2008 +0000
@@ -71,7 +71,7 @@
     gc->clear(CL_Color::white);
 
     // Draw terrain
-    state.draw(gc);
+    state.world.draw(gc);
 
     // Flip window buffer, sync
     win.flip(1);
--- a/src/PhysicsWorld.hh	Thu Dec 04 19:54:51 2008 +0000
+++ b/src/PhysicsWorld.hh	Thu Dec 04 21:10:41 2008 +0000
@@ -7,7 +7,8 @@
 #include <algorithm>
 #include <functional>
 #include <cmath>
-           
+
+
 #include "Terrain.hh"          
 class PhysicsWorld;
 
@@ -37,8 +38,6 @@
     // Contains connections between signals and slots
     CL_SlotContainer slots;
 
-    PhysicsWorld(Vector gravity, Vector dimensions);
-
     // TODO: Should these be somewhere else?
     Vector dimensions;
     Vector gravity;
@@ -46,6 +45,9 @@
 
 
 public:
+
+    PhysicsWorld(Vector gravity, Vector dimensions);
+
     // TODO: Replace addObject with these?
     //void addPlayerObject(PlayerObject *object);
     //void addProjectileObject(ProjectileObject *object);
--- a/src/Player.cc	Thu Dec 04 19:54:51 2008 +0000
+++ b/src/Player.cc	Thu Dec 04 21:10:41 2008 +0000
@@ -1,7 +1,8 @@
+#include "Engine.hh"
 #include "Player.hh" 
 
 Player::Player(GameState &state, Vector position, bool visible) : 
-    PhysicsObject((PhysicsWorld &) state, PLAYER_MASS, position, Vector(0, 0)), state(state), visible(visible) {
+    PhysicsObject(state.world, PLAYER_MASS, position, Vector(0, 0)), state(state), visible(visible) {
 
     std::vector<Vector> shape(4);
     shape[0] = Vector(0,-9);
@@ -14,3 +15,76 @@
     world.addPlayerObject(this);
 }
  
+/**
+ * shoots the selected weapon.
+ * TODO: selection and weapon information
+ */
+void Player::shoot (void) {
+    // here should be somehow considered which projectile it is
+    if(!canShoot())
+        return;
+    reloadTimer += 0;
+    Vector unitVectorAim = facingRight ? Vector(std::cos(aim), -std::sin(aim)) : 
+            Vector(-std::cos(aim), -std::sin(aim));
+    float shotspeed = 100*PHYSICS_TICK_MS;
+    Vector shotRelativeVelocity = unitVectorAim * shotspeed;
+    Vector shotVelocity = this->velocity + shotRelativeVelocity;
+    world.addProjectile(new Shot(this->state, this->position, shotVelocity, true));
+}
+
+void Player::handleMove (PlayerInput_Move input) {
+    float fx = 0; // Force in x-direction
+    float da = 0; // Crosshair angle
+
+    // handle left/right
+    if ((input & INPUT_MOVE_LEFT) && (velocity.x > -PLAYER_MAX_SPEED))
+        fx -= PLAYER_MOVE_FORCE;
+
+    if ((input & INPUT_MOVE_RIGHT) && (velocity.x < PLAYER_MAX_SPEED))
+        fx += PLAYER_MOVE_FORCE;
+
+    if (input & INPUT_MOVE_UP)
+        da += CROSSHAIR_ANGLE_SPEED;
+
+    if (input & INPUT_MOVE_DOWN)
+        da -= CROSSHAIR_ANGLE_SPEED;
+
+    if (input & INPUT_MOVE_JUMP) {
+        if ((input & INPUT_MOVE_LEFT))
+            jump(-1);
+        else if ((input & INPUT_MOVE_RIGHT))
+            jump(1);
+        else
+            jump(0);
+    }
+
+    if (input & INPUT_MOVE_DIG) {
+        // Should create Shot which destroys ground, but also should be destroyed then,
+        // but it doesn't.
+        // But this now just segfaults
+//        world.addObject(new Shot(state, position, true));
+
+        world.removeGround(position, 15);
+    }
+
+    if (input & INPUT_SHOOT) {
+        this->shoot();
+    }
+
+
+
+    // Player facing
+    if (fx < 0) setFacing(false);
+    else if (fx > 0) setFacing(true);
+
+
+    this->changeAim(da); // Move crosshair
+
+    // Apply force
+    applyForce(Vector(fx, 0));
+
+}
+
+void Player::debugInfo (void) {
+    Engine::log(DEBUG, "Player.debugInfo") << "In air: " << this->inAir;
+}
--- a/src/Player.hh	Thu Dec 04 19:54:51 2008 +0000
+++ b/src/Player.hh	Thu Dec 04 21:10:41 2008 +0000
@@ -2,7 +2,10 @@
 #define PLAYER_HH
 
 class Player;
+class LocalPlayer;
+class RemotePlayer;
 
+#include "GameState.hh"
 #include "PhysicsObject.hh"
 
 class Player : public PhysicsObject {
--- a/src/Projectile.cc	Thu Dec 04 19:54:51 2008 +0000
+++ b/src/Projectile.cc	Thu Dec 04 21:10:41 2008 +0000
@@ -13,4 +13,43 @@
     collision_elasticity = 0.9; // = shotType.elasticity
     world.addProjectile(this);
 }
+
  
+void Shot::onCollision() {
+    world.removeGround(position, 20);
+    this->destroyed = true;
+}
+
+bool Shot::isDestroyed (void) {
+    return this->destroyed;
+}
+
+void Shot::draw(CL_GraphicContext *gc) {
+
+
+    CL_Quad player(
+                   (int)((position).x+1), (int)((position).y+1),
+                   (int)((position).x-1), (int)((position).y+1),
+                   (int)((position).x+1), (int)((position).y-1),
+                   (int)((position).x-1), (int)((position).y-1)
+                   );
+
+    gc->fill_quad(player, CL_Color::green);
+
+    const uint16_t chlen = 10;
+    uint16_t x = player.center().x;
+    uint16_t y = player.center().y;
+    if(target_visible) {
+        if (facingRight) {
+            gc->draw_line(x, y,
+                          x + std::cos(aim)*chlen,
+                          y - std::sin(aim)*chlen,
+                          CL_Color::black);
+        } else {
+            gc->draw_line(x, y,
+                          x - std::cos(aim)*chlen,
+                          y - std::sin(aim)*chlen,
+                          CL_Color::black);
+        }
+    }
+}