- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# C:/Documents and Settings/jegan/Desktop/projectX/Tile.cpp

Go to the documentation of this file.

    00001 #include "Tile.h"
    00002 #include "CollisionHandler.h"
    00003 #include "Level.h"
    00004
    00005 #include <iostream>
    00006 #include <sstream>
    00007
    00008 using namespace std;
    00009
    00010 Tile::Tile()
    00011 :Object()
    00012 {
    00013 }
    00014
    00015 Tile::Tile(Sprite* sprite, bool collidable, int x, int y, double depth)
    00016 :Object(),_sprite(sprite),_draw(true),_existDBox(false)
    00017 {
    00018         _type = TILE;
    00019         // FIXME  make this general
    00020         stringstream id;
    00021         id <<this;
    00022         _id = id.str();
    00023
    00024         _collidable = collidable;
    00025         _box.x = x;
    00026         _box.y = y;
    00027
    00028         _box.w = _sprite->width();
    00029         _box.h = _sprite->height();
    00030
    00031         _drawBox = _box;
    00032
    00033         _depth = depth;
    00034 }
    00035
    00036 Object *Tile::copy()
    00037 {
    00038         return new Tile(*this);
    00039 }
    00040
    00041 void Tile::collide( Object* o )
    00042 {
    00043          //does nothing
    00044 }
    00045 void Tile::draw()
    00046 {
    00047         if(_draw) {
    00048                 if(_existDBox)
    00049                         _sprite->drawBox(_box.x,_box.y,_drawBox);
    00050                 else
    00051                         _sprite->draw(_box.x,_box.y);
    00052         }
    00053 }
    00054
    00055 void Tile::update()
    00056 {
    00057         _sprite->update();
    00058 }
    00059
    00060 void Tile::calcDrawBox()
    00061 {
    00062         if(_draw) {
    00063                 _drawBox = getSquare(_sprite, _depth);
    00064
    00065                 for(int i = _box.x; i < _box.x + _drawBox.w; i += Level::tileWidth) {
    00066                         for(int j = _box.y; j < _box.y + _drawBox.h; j += Level::tileHeight) {
    00067                                 Tile *t = Level::instance()->getTileFromPixels(i,j);
    00068                                 if(t && t->_sprite == _sprite && t->_depth == _depth) {
    00069                                         t->_draw = false;
    00070                                 }
    00071                         }
    00072                 }
    00073
    00074                 _draw = true;
    00075                 _existDBox = true;
    00076         }
    00077 }
    00078
    00079 SDL_Rect Tile::getSquare(Sprite *sprite, double depth)
    00080 {
    00081         SDL_Rect square = _box;
    00082         Tile *diag = Level::instance()->getTileFromPixels(_box.x+Level::tileWidth,_box.y+Level::tileHeight);
    00083         if(diag == NULL || (sprite != diag->_sprite || diag->_depth <= depth)) {
    00084                 int i = _box.y + Level::tileHeight;
    00085                 Tile *t1 = Level::instance()->getTileFromPixels(_box.x,i);
    00086
    00087                 int j = _box.x + Level::tileWidth;
    00088                 Tile *t2 = Level::instance()->getTileFromPixels(j,_box.y);
    00089
    00090
    00091                 if(t1 != NULL) {
    00092                         while(t1 != NULL && (sprite == t1->_sprite && t1->_depth > depth)) {
    00093                                 square.h += Level::tileHeight;
    00094
    00095                                 i += Level::tileHeight;
    00096                                 t1 = Level::instance()->getTileFromPixels(_box.x,i);
    00097                         }
    00098                 }
    00099
    00100                 else if(t2 != NULL) {
    00101                         while(t2 != NULL && (sprite == t2->_sprite && t2->_depth > depth)) {
    00102                                 square.w += Level::tileWidth;
    00103
    00104                                 j += Level::tileWidth;
    00105                                 t2 = Level::instance()->getTileFromPixels(j,_box.y);
    00106                         }
    00107                 }
    00108         }
    00109         else {
    00110                 SDL_Rect diagBox = diag->getSquare(sprite, depth);
    00111
    00112                 for(int i = _box.x + Level::tileWidth; i < _box.x + diagBox.w + Level::tileWidth; i += Level::tileWidth) {
    00113                         Tile *t = Level::instance()->getTileFromPixels(i,_box.y);
    00114
    00115                         if(t == NULL || (sprite != t->_sprite || t->_depth <= depth))
    00116                                 break;
    00117                         else
    00118                                 square.w += Level::tileWidth;
    00119                 }
    00120
    00121                 for(int i = _box.y + Level::tileHeight; i < _box.y + diagBox.h + Level::tileHeight; i += Level::tileHeight) {
    00122                         Tile *t = Level::instance()->getTileFromPixels(_box.x,i);
    00123                         if(t == NULL || (sprite != t->_sprite || t->_depth <= depth))
    00124                                 break;
    00125                         else
    00126                                 square.h += Level::tileHeight;
    00127                 }
    00128         }
    00129
    00130         return square;
    00131 }