src/Terrain.hh
author nireco
Sun, 07 Dec 2008 13:07:58 +0000
changeset 243 25d9a0090397
parent 222 293ddf4c067d
child 248 e40ef56dc62c
permissions -rw-r--r--
some generated texture
#ifndef TERRAIN_HH
#define TERRAIN_HH

#include <vector>

#include "Vector.hh"
#include "Config.hh"

enum TerrainType {EMPTY, DIRT, ROCK};

const Vector DIRECTIONS[] = {
    Vector(0,-1),
    Vector(1,-1),
    Vector(1,0),
    Vector(1,1),
    Vector(0,1),
    Vector(-1,1),
    Vector(-1,0),
    Vector(-1,-1)
};

/**
 * 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();

private:
    // Terrain graphic
    CL_PixelBuffer pixbuf;
    std::vector<std::vector<int> > texture;

    void generate_texture();

    void noisifyPixel(CL_Color& col, int x, int y);

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

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

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

 public:

    // TODO: This should be private.
    /**
     * 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(int32_t x, int32_t y) const;
    //point is just rounded and redirected to that above
    TerrainType getType(Vector point) const;

    /**
     * Constructor.
     *
     * @param scale The "real" width and height of the terrain.
     */
    Terrain();
    /**
     * Constructor.
     *
     * @param scale The "real" width and height of the terrain.
     * @param seed Random number generator seed used to create the
     * terrain.
     */
    Terrain(const int &seed);
    /**
     * Copy constructor.
     *
     * @param t Terrain to be copied.
     */
    Terrain(const Terrain &t);
    /**
     * Destructor
     */
    ~Terrain() {}

    /**
     * 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, const 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
     */
    virtual void draw(CL_GraphicContext *gc);

    /**
     * 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;
};

Vector direction(const Vector &v);

#endif