src/Terrain.hh
author terom
Mon, 15 Dec 2008 14:24:38 +0000
changeset 370 39e59dd36b6e
parent 296 4d3ebaa29430
child 406 a2e35ca66c74
permissions -rw-r--r--
clean up Vector a bit, remove unused Terrain -> direction function
#ifndef TERRAIN_HH
#define TERRAIN_HH

#include <vector>

#include "Vector.hh"
#include "GraphicsPointer.hh"
#include "Types.hh"
#include "Config.hh"

enum TerrainType {
    TERRAIN_EMPTY, 
    TERRAIN_DIRT, 
    TERRAIN_ROCK
};

/**
 * Terrain class. Represents game terrain and contains member
 * functions to manipulate terrain and get info about it.
 * 
 * Terrain resolution is a constant that is defined in
 * configuration. Terrain has a scale (i.e. the width and height in
 * "real" units. Scaling is needed because physics simulation uses
 * "real" units. The idea is that this class is used with "real" units
 * and it uses pixelcoordinates internally.
 */ 
class Terrain {
public: // XXX: until we fix Network's access to this
    std::vector<std::vector<TerrainType> > terrain;    
    
    /**
     * Generates pixelbuffer from terrain. Should be used only on
     * constructors because this is expected to be slow.
     */
    void generatePixelBuffer();

protected:
    // terrain dimensions
    PixelDimension map_width, map_height;

    // We can pre-render the terrain as a pixel buffer and then just modify this
    CL_PixelBuffer pixbuf;

    // terrain texture
    std::vector<std::vector<int> > texture;

    /**
     * Default constructor. The width/height are set to zero and the terrain is invalid until it gets updated
     *
     * @param scale The "real" width and height of the terrain.
     */
    Terrain (void);

    /**
     * Constructor.
     *
     * @param scale The "real" width and height of the terrain.
     * @param seed Random number generator seed used to create the
     * terrain.
     */
    Terrain (PixelDimension map_width, PixelDimension map_height, int seed);

    /**
     * Copy constructor.
     *
     * @param t Terrain to be copied.
     */
    Terrain (const Terrain &t);

    /**
     * Destructor
     */
    ~Terrain (void) {}

protected:
    void generate_texture();
    void noisifyPixel(CL_Color& col, PixelCoordinate pc);


    /**
     * Scale parameter to "pixels"
     *
     * @param x Scaled value
     * @return Corresponding value in pixels
     */
    PixelDimension scale (float x) const;

    /**
     * Sets to color the correct color of pixel in (x,y)
     */
    void loadPixelColor (CL_Color& color, PixelCoordinate pc);

 public:
    /**
     * Get pixel location of a point that is in "real" units.
     *
     * @param point Point in "real" units
     * @return Int vector
     */
    PixelCoordinate getPixelCoordinate (Vector point) const;

    /**
     * Return the terrain dimensions à la a PixelCoordinate
     */
    PixelCoordinate getDimensions (void) const;

    /**
     * Return the type of terrain at given position. Returns ROCK if
     * given point is not inside terrain area.
     *
     * @param x X coordinate
     * @param y Y coordinate
     * @return Terrain type
     */
    TerrainType getType (PixelDimension px, PixelDimension py) const;
    TerrainType getType (PixelCoordinate pc) const;
    TerrainType getType (Vector point) const;

    /**
     * Check if given point has some terrain.
     *
     * @param point Point that is in scaled units.
     */
    bool collides (const Vector &point) const;
    /**
     * Check if given line collides with terrain.
     *
     * @param begin Line begin point in scaled units.
     * @param end Line end point in scaled units.
     */
    bool collides (const Vector &begin, const Vector &end) const;
    
    /**
     * Remove a circular area from terrain.
     *
     * @param pos Circle center
     * @param r Circle radius
     */ 
    void removeGround (const Vector &pos, float r);

    /**
     * Return normal for the given point.
     *
     * @param point Point for which the normal is calculated.
     * @return Normal vector ((0,0) if there's no terrain)
     */
    Vector getNormal (Vector point, Vector prevPoint) const;

    /**
     * Generate random terrain.
     *
     * @param seed Seed for the randomnumber generator.
     */
    void generateTerrain (int seed);

    /**
     * Draw the terrain for given graphicscontext.
     *
     * @param gc CL_GraphicContext
     * @param camera - upper left corner of screen
     */
    virtual void draw (Graphics *g, PixelCoordinate camera = PixelCoordinate(0, 0));

    /**
     * Draw part of the terrain for given graphiscontext.
     *
     * @param gc CL_GraphicContext
     * @param center Center of the rectangle drawn.
     * @param dimension Dimensions of the rectangle.
     */
    //void draw(CL_GraphicContext &gc, Vector center, Vector dimensions);

    /**
     * Set terrain.
     *
     * @param terrain Terrain.
     */
    void setTerrain (const std::vector<std::vector<TerrainType> > &terrain);
    
    /**
     * Get terrain.
     *
     * @return Terrain.
     */
    std::vector<std::vector<TerrainType> > getTerrain() const;
};

#endif