direction normalization functions in vector, change rope color, misc comments+whitespace
authorterom
Mon, 15 Dec 2008 16:41:00 +0000
changeset 377 01d3c340b372
parent 376 2fd7fd2ee1a1
child 378 5589abf5e61b
direction normalization functions in vector, change rope color, misc comments+whitespace
src/Config.hh
src/Graphics.cc
src/Rope.cc
src/Terrain.cc
src/Vector.hh
--- a/src/Config.hh	Mon Dec 15 15:36:17 2008 +0000
+++ b/src/Config.hh	Mon Dec 15 16:41:00 2008 +0000
@@ -77,24 +77,33 @@
 const float PROJECTILE_START_DISTANCE = 10.0;
 
 /*
- * Rope
+ * @name Rope
+ * @{
  */
 
-// how much to grow the rope length by on every ROPE_UP/DOWN
+/** How much to grow the rope length by on every ROPE_UP/DOWN */
 const float ROPE_GROWTH_RATE = 5;
 
-// the force that the rope exerts on the player when the length is too long
+/** The force that the rope exerts on the player when the length is too long */
 const float ROPE_FORCE = 3500;
 
-// same as player mass...?
+/** Same as player mass...? */
 const float ROPE_MASS = 10.0;
 
-// initial length... this should probably be a bit more dynamic?
+/** Initial rope length... this should probably be a bit more dynamic? */
 const float ROPE_LENGTH = 100.0;
 
-// initial velocity of the rope when thrown
+/** Initial velocity of the rope when thrown */
 const float ROPE_VELOCITY = 500;
 
+/**
+ * Rope colors, it should alternate between these two
+ */
+const CL_Color ROPE_COLOR_LIGHT = CL_Color(198, 100, 2);
+const CL_Color ROPE_COLOR_DARK = CL_Color(159, 79, 1);
+
+// @}
+
 // Graphical properties
 const CL_Color COLOR_EMPTY(86, 41, 0);
 const CL_Color COLOR_DIRT(144, 82, 23);
--- a/src/Graphics.cc	Mon Dec 15 15:36:17 2008 +0000
+++ b/src/Graphics.cc	Mon Dec 15 16:41:00 2008 +0000
@@ -75,10 +75,13 @@
     
     // ...to track our local player
     if ((player = state.getLocalPlayer()) != NULL) {
+        // try and center the screen on the player
         PixelCoordinate target = player->getCoordinate() - PixelCoordinate(resolution.x / 2, (resolution.y - 100) / 2);
+
+        // ...but keep the world in view
         PixelCoordinate max = state.world.getDimensions() - resolution + PixelCoordinate(0, 100);
         
-        // keep the terrain in view
+        // ...by limiting the value to 0...max
         camera = PixelCoordinate(
             value_between(0, target.x, max.x),
             value_between(0, target.y, max.y)
@@ -91,9 +94,9 @@
     // Draw the game
     state.draw(this, camera, flags & GUI_INPUT_DISPLAY_WEAPON);
 
-    // Draw box for player information
     if (player != NULL) {
-        draw_player_info(gc, (Player*)player);
+        // draw player info box
+        draw_player_info(gc, player);
     }
 
     // Flip window buffer, sync
@@ -133,7 +136,7 @@
         )
     );
     
-    // Lifebar
+    // Health
     gc->draw_rect(
         CL_Rect(
             box_left + 9,
@@ -158,6 +161,8 @@
             CL_Color(200 - (int)(p->getHealthPercent() * 2), (int)(p->getHealthPercent() * 2), 0)
         )
     );
+
+    // stats - kills
     std::stringstream sskills;
     sskills << "Kills:  " << p->getKills();
     getSimpleFont().draw(
@@ -166,7 +171,8 @@
         sskills.str(),
         get_gc()
     );
-
+    
+    // stats - deaths
     std::stringstream ssdeaths;
     ssdeaths << "Deaths:  " << p->getDeaths();
     getSimpleFont().draw(
@@ -175,7 +181,8 @@
         ssdeaths.str(),
         get_gc()
     );
-
+    
+    // stats - ratio
     std::stringstream ssratio;
     ssratio << "Ratio:  " << (p->getKills()+1) / (p->getDeaths()+1);
     getSimpleFont().draw(
@@ -186,7 +193,7 @@
     );
     
 
-    // Ammobar
+    // Weapon clip / reloading
     gc->draw_rect(
         CL_Rect(
             box_left + 9,
@@ -211,7 +218,8 @@
             CL_Color(100, 100, 100)
         )
     );
-
+   
+    // current weapon name
     getSimpleFont().draw(
         box_left + 20 + 100 * bar_length,
         box_top + 70,
--- a/src/Rope.cc	Mon Dec 15 15:36:17 2008 +0000
+++ b/src/Rope.cc	Mon Dec 15 16:41:00 2008 +0000
@@ -153,7 +153,8 @@
 void Rope::draw (Graphics *g, PixelCoordinate camera) {
     PixelCoordinate player_pos = player.getCoordinate() - camera;
     PixelCoordinate target_pos;
-
+    
+    // figure out what target is
     if (state == ROPE_FOLDED) {
         return;
 
@@ -169,7 +170,7 @@
         // target is our pivot
         target_pos = player.getPivot()->getCoordinate();
     }
-    
+ 
     // align with camera
     target_pos -= camera;
     
@@ -177,7 +178,7 @@
     g->get_gc()->draw_line(
         player_pos.x, player_pos.y,
         target_pos.x, target_pos.y,
-        CL_Color::black
+        ROPE_COLOR_DARK
     );
 }
 
--- a/src/Terrain.cc	Mon Dec 15 15:36:17 2008 +0000
+++ b/src/Terrain.cc	Mon Dec 15 16:41:00 2008 +0000
@@ -349,7 +349,7 @@
 
     assert(point != prevPoint);
 
-    Vector normal(0,0);
+    Vector normal(0, 0);
 
     // These two must be rounded separately
     int dirIdx = getDirectionIndex(prevPoint.roundToInt() - point.roundToInt());
@@ -357,14 +357,14 @@
     normal += DIRECTIONS[dirIdx];
 
     for (int i = 1; i <= 2; i++) {
-        if (getType(point + DIRECTIONS[(dirIdx+i+8)%8]) == TERRAIN_EMPTY) {
-            normal += DIRECTIONS[(dirIdx+i+8)%8];
+        if (getType(point + DIRECTIONS[(dirIdx + i + 8) % 8]) == TERRAIN_EMPTY) {
+            normal += DIRECTIONS[(dirIdx + i + 8) % 8];
         }
     }
 
     for (int i = 1; i <= 2; i++) {
-        if (getType(point + DIRECTIONS[(dirIdx-i+8)%8]) == TERRAIN_EMPTY) {
-            normal += DIRECTIONS[(dirIdx-i+8)%8];
+        if (getType(point + DIRECTIONS[(dirIdx - i + 8) % 8]) == TERRAIN_EMPTY) {
+            normal += DIRECTIONS[(dirIdx - i + 8) % 8];
         }
     }
 
--- a/src/Vector.hh	Mon Dec 15 15:36:17 2008 +0000
+++ b/src/Vector.hh	Mon Dec 15 16:41:00 2008 +0000
@@ -155,6 +155,32 @@
         return x == 0 && y == 0;
     }
 
+    /**
+     * Returns the corresponding unit vector, i.e. the vector divided by its own length
+     *
+     * @return Vector with length() == 1
+     */
+    VectorType unitVector (void) const {
+        T len = length();
+
+        return VectorType(x / len, y / len);
+    }
+
+    /**
+     * Returns a normalized vector such that the x/y coordinates are both either -1, 0 or 1, to indicate which
+     * direction this vector points in.
+     *
+     * @return normalized Vector
+     */
+    VectorType normalizeDirection (void) const;
+
+    /**
+     * Returns a vector's normal (one of the two)
+     */
+    VectorType normal (void) const {
+        return VectorType(y, -x);
+    }
+
 };
 
 /**
@@ -174,4 +200,39 @@
     return s << "(" << v.x << ", " << v.y << ")";
 }
 
+// Templates function implementations
+#include <cmath>
+#include <cstdlib>
+
+/**
+ * Float/int absolute value
+ */
+template<typename T> static T absType (T value);
+template<> static float absType<float> (float value) { return fabs(value); }
+template<> static long absType<long> (long value) { return labs(value); }
+
+/**
+ * Direction-normalize the given coordinate against the tangent coordinate.
+ *
+ * Returns 1 if the coordinate is positive and greater than the tangent, -1 if the coordinate is less than and
+ * negative, else zero.
+ */
+template<typename T> static T normalizeCoordinate (T coord, T tangent) {
+    if (coord > absType(tangent))
+        return 1;
+
+    else if (coord < -absType(tangent))
+        return -1;
+
+    else
+        return 0;
+}
+
+template<typename T> VectorType<T> VectorType<T>::normalizeDirection (void) const {
+    return VectorType<T>(
+        normalizeCoordinate(x, y),
+        normalizeCoordinate(y, x)
+    );
+}
+
 #endif