int App::main(const std::vector<std::string>& args) { clan::DisplayWindowDescription desc; desc.set_position(clan::Rect(-800, 100, clan::Size(800, 600)), false); desc.set_title("ClanLib 2D Dijkstra Test"); desc.set_allow_resize(false); clan::DisplayWindow win(desc); clan::Canvas canvas(win); m_canvas = canvas; Tilemap map; map.build_map(31); clan::GameTime game_time; while(!win.get_ic().get_keyboard().get_keycode(clan::keycode_escape)) { game_time.update(); canvas.clear(); map.draw(canvas); map.update(win.get_ic(), game_time.get_time_elapsed()); win.flip(); clan::KeepAlive::process(); } map.reset(); return 0; }
void Snake::Game::init() { if (SDL_Init(SDL_INIT_EVERYTHING) >= 0) { window = SDL_CreateWindow("SDL Snake", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE); if (window != 0) { renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); } } else { Snake::Utils::logSDLError(std::cout, "Failed initializing SDL"); is_running = false; } Tilemap* tilemap = Tilemap::instance(); tilemap->init(renderer, 32, 32); tilemap->addTile("../resources/images/grass.png", "grass"); tilemap->addTile("../resources/images/snake_head.png", "snake_head"); tilemap->addTile("../resources/images/snake_body.png", "snake_body"); player = new Player(); player->load(100, 100); for (int i = 0; i < 100; i++) { player->growTail(); } is_running = true; }
TilesArray sideTiles( int side, Tilemap& oTilemap ) { int mapSize = oTilemap.size(); switch( side % 4 ) { case 0: return oTilemap.getArea( TilePos( 0, 0), TilePos( 0, mapSize-1 ) ); case 1: return oTilemap.getArea( TilePos( 0, mapSize-1), TilePos( mapSize-1, mapSize-1 ) ); case 2: return oTilemap.getArea( TilePos( mapSize-1, mapSize-1), TilePos( mapSize-1, 0 ) ); case 3: return oTilemap.getArea( TilePos( mapSize-1, 0), TilePos( 0, 0 ) ); } return TilesArray(); }
void Pathfinder::update( const Tilemap& tilemap ) { _d->tilemap = const_cast< Tilemap* >( &tilemap ); _d->grid.resize( tilemap.getSize() ); for( int k=0; k < tilemap.getSize(); k++) { _d->grid[ k ].resize( tilemap.getSize()); } TilemapTiles tiles = _d->tilemap->getArea( TilePos( 0, 0 ), Size( tilemap.getSize() ) ); foreach( Tile* tile, tiles ) { _d->grid[ tile->getI() ][ tile->getJ() ] = new AStarPoint( tile ); }
void GameLoader::Impl::initWaterTileAnimation( Tilemap& tmap ) { TilemapArea area = tmap.getArea( TilePos( 0, 0 ), Size( tmap.getSize() ) ); Animation water; water.setFrameDelay( 12 ); water.load( ResourceGroup::land1a, 121, 7 ); water.load( ResourceGroup::land1a, 127, 7, true ); foreach( Tile* tile, area ) { int rId = tile->getOriginalImgId() - 364; if( rId >= 0 && rId < 8 ) { water.setCurrentIndex( rId ); tile->setAnimation( water ); } }
bool Prekazitor::tajna_chodba() { bool sm = !(nahodne(2)); if (!uprav_smer(sm, 4)) { return false; } int delka = exp_rand(6, max_x-(*X)-4) + 3; if (delka < 3) { return false; } Tilemap* tm = new Tilemap(delka, 4, false); tm->pojmenuj("secretTM",s->tajnych_chodeb); tm->z_pos = 101 + s->tajnych_chodeb; tm->cesta = new Cesta(); float x, y; if (sm) { dolovak(4); stredovak(delka); s->intact2->obdelnik(K[1] , (*X)-delka-1, (*X)-3, (*Y)-5, (*Y)-5); s->intact2->obdelnik(K[4] , (*X)-delka-1, (*X)-3, (*Y)-4, (*Y)-4); s->intact2->obdelnik(K[10], (*X)-delka-1, (*X)-3, (*Y)-3, (*Y)-3); s->intact2->poloz_blok(K[2] , (*X)-2, (*Y)-5); s->intact2->poloz_blok(K[5] , (*X)-2, (*Y)-4); s->intact2->poloz_blok(K[11], (*X)-2, (*Y)-3); s->intact2->poloz_blok(V[0], (*X)-delka-1, (*Y)-3); tm->obdelnik(S, 0, tm->sirka-2, 0, tm->vyska-1); tm->obdelnik(K[8], tm->sirka-1, tm->sirka-1, 0, tm->vyska-3); tm->poloz_blok(V[2], tm->sirka-1, tm->vyska-2); tm->poloz_blok(V[4], tm->sirka-1, tm->vyska-1); x = (*X)-delka-1; y = (*Y)-3; } else { stredovak(delka); nahorovak(4); s->intact2->poloz_blok(K[0], (*X)-delka, (*Y)-1); s->intact2->poloz_blok(K[3], (*X)-delka, *Y); s->intact2->poloz_blok(K[9], (*X)-delka, (*Y)+1); s->intact2->obdelnik(K[1] , (*X)-delka+1, *X, (*Y)-1, (*Y)-1); s->intact2->obdelnik(K[4] , (*X)-delka+1, *X, *Y, *Y); s->intact2->obdelnik(K[10], (*X)-delka+1, *X, (*Y)+1, (*Y)+1); s->intact2->poloz_blok(V[1], (*X)-1, (*Y)+1); tm->obdelnik(S, 1, tm->sirka-1, 0, tm->vyska-1); tm->obdelnik(K[6], 0, 0, 0, tm->vyska-3); tm->poloz_blok(V[3], 0, tm->vyska-2); tm->poloz_blok(V[5], 0, tm->vyska-1); x = (*X)-delka; y = (*Y)+1; } secretarea(x, y, tm); stredovak(1); s->tilemapy.push_back(tm); s->tajnych_chodeb++; return true; }
double Character::scanDistance(double edge, const Tilemap& map, Direction direction, intVector firstTile, intVector lastTile) { double distance; //indices of tile to be checked int xi; int yi; //to keep track of smallest value int minDist = 1000000; int distIndex; //for each occupied tile, shoot a ray in desired direction //insert smallest value in distance for (int i = firstTile.y; i <= lastTile.y; i++) { for (int j = firstTile.x; j <= lastTile.x; j++) { yi = i; xi = j; distIndex = 0; while ( distIndex < minDist && xi >= 0 && yi >= 0 && xi < map.horiTiles && yi < map.vertiTiles && map.getTile(xi, yi) != 1 ) { switch (direction) { case LEFT: xi--; break; case RIGHT: xi++; break; case UP: yi--; break; case DOWN: yi++; break; } distIndex++; } minDist = min(minDist, distIndex); } } switch (direction) { case LEFT: distance = edge - (xi + 1)*map.tileRes; break; case RIGHT: distance = xi*map.tileRes - edge; break; case UP: distance = edge - (yi + 1)*map.tileRes; break; case DOWN: distance = yi*map.tileRes - edge; break; } return signbit(distance) ? 0.0 : distance; }
TilesArea::TilesArea(const Tilemap& tmap, int distance, OverlayPtr overlay) { if( overlay.isNull() ) return; TilePos offset( distance, distance ); TilePos size( overlay->size().width(), overlay->size().height() ); TilePos start = overlay->tile().epos(); _size = overlay->size(); append( tmap.area( start - offset, start + size + offset ) ); }
Builder::Builder(Map& map, const TileDatabase& tileTemplates, uint32_t roomLimit) : _map(map), _tileTemplates(tileTemplates) { _regions.reserve(roomLimit); _segments.reserve(roomLimit*8); _connections.reserve(roomLimit*8); // clear all tilemaps const cinekine::overview::MapBounds& bounds = _map.bounds(); for (uint32_t z = 0; z < bounds.zUnits; ++z) { Tilemap* tilemap = _map.tilemapAtZ(z); Tile zeroTile = { 0, 0 }; tilemap->fillWithValue(zeroTile, 0, 0, bounds.yUnits, bounds.xUnits); } }
Tilemap* TilemapLoader::load(QString path) { QImage *image = new QImage(path); if(image->isNull()) { QMessageBox::information(nullptr, "Erreur", "L'image n'est pas trouvée!"); return nullptr; } Tilemap *tilemap = new Tilemap(); tilemap->setHeight(image->height()); tilemap->setWidth(image->width()); for(int heightCount = 0;heightCount<image->height(); heightCount++) { for(int widthCount = 0;widthCount<image->width();widthCount++) { QRgb color = image->pixel(widthCount,heightCount); switch(color) { case ASPHALT: tilemap->setTile(widthCount,heightCount,GroundType::Asphalt); break; case MUD: tilemap->setTile(widthCount,heightCount,GroundType::Mud); break; default : tilemap->setTile(widthCount,heightCount,GroundType::Grass); } } } return tilemap; }
void Pathway::setNextDirection( const Tilemap& tmap, Direction direction) { switch (direction) { case direction::north : _d->endPos += TilePos( 0, 1 ); break; case direction::northEast : _d->endPos += TilePos( 1, 1 ); break; case direction::east : _d->endPos += TilePos( 1, 0 ); break; case direction::southEast : _d->endPos += TilePos( 1, -1 ); break; case direction::south : _d->endPos += TilePos( 0, -1 ); break; case direction::southWest : _d->endPos += TilePos( -1, -1 ); break; case direction::west : _d->endPos += TilePos( -1, 0 ); break; case direction::northWest : _d->endPos += TilePos( -1, 1 ); break; default: _d->endPos += TilePos( 0, 1 ); break; Logger::warning( "Unexpected Direction:%d", direction); break; } if( tmap.isInside( _d->endPos ) ) { _d->tiles.push_back( const_cast<Tile*>( &tmap.at( _d->endPos )) ); } }
void Pathway::load(const Tilemap& tmap, const VariantMap& stream ) { if( stream.empty() ) { return; } VARIANT_LOAD_ANY_D( _d, startPos, stream ) VARIANT_LOAD_ANY_D( _d, endPos, stream ) VariantList vmTiles = stream.get( literals::tiles ).toList(); foreach( it, vmTiles ) { _d->tiles.push_back( const_cast<Tile*>( &tmap.at( it->toTilePos() )) ); }
void GameLoader::Impl::initEntryExitTile( const TilePos& tlPos, Tilemap& tileMap, const unsigned int picIdStart, bool exit ) { unsigned int idOffset = 0; TilePos tlOffset; if( tlPos.getI() == 0 || tlPos.getI() == tileMap.getSize() - 1 ) { tlOffset = TilePos( 0, 1 ); idOffset = (tlPos.getI() == 0 ? 1 : 3 ); } else if( tlPos.getJ() == 0 || tlPos.getJ() == tileMap.getSize() - 1 ) { tlOffset = TilePos( 1, 0 ); idOffset = (tlPos.getJ() == 0 ? 2 : 0 ); } Tile& signTile = tileMap.at( tlPos + tlOffset ); StringHelper::debug( 0xff, "(%d, %d)", tlPos.getI(), tlPos.getJ() ); StringHelper::debug( 0xff, "(%d, %d)", tlOffset.getI(), tlOffset.getJ() ); signTile.setPicture( ResourceGroup::land3a, picIdStart + idOffset ); signTile.setFlag( Tile::tlRock, true ); }
bool checkMapCollision(Character& scanner, const Tilemap& map) { int x1 = scanner.rect.x / map.tileRes; int x2 = (scanner.rect.x + scanner.rect.w - 1) / map.tileRes; int y1 = scanner.rect.y / map.tileRes; int y2 = (scanner.rect.y + scanner.rect.h - 1) / map.tileRes; for (int x = x1; x <= x2; x++) { for (int y = y1; y <= y2; y++) { if (x < 0 || x >= map.horiTiles || y < 0 || y >= map.vertiTiles) continue; if (map.getTile(x, y) == 1) return true; } } return false; }
TilesArea::TilesArea(const Tilemap &tmap, int distance, const TilePos& center) { TilePos offset( distance, distance ); _size = Size::square( distance ) * 2; append( tmap.area( center - offset, center + offset ) ); }
void Pacman::Update(const SDLInput& input, Tilemap& tilemap, float deltaTime, const std::shared_ptr<SDLRenderTarget>& tileSurface, const std::vector<SDLSurface>& gridSurf) { if (m_State == PacState::PowerUp) // if powered up { float currentTime = Timer::GetTime(); if ((currentTime - m_powerDuration) > 5.f) // if powered up for more tha 5 seconds { m_State = PacState::Normal;// return to normal } } //set our speed float speed = 5.5f * deltaTime; // clamp position size_t cX = (size_t)(clamp(m_Position.x, 0.f, 18.f) + 0.5f); size_t cY = (size_t)(clamp(m_Position.y, 0.f, 22.f) + 0.5f); switch (m_CurrentDirection) // move in direction { case Direction::Left: m_Position.x -= speed; break; case Direction::Right: m_Position.x += speed; break; case Direction::Up: m_Position.y -= speed; break; case Direction::Down: m_Position.y += speed; break; } //make the side wall let us trough if (m_Position.x < 0.f) { m_Position.x = 19.f; } if (m_Position.x > 19.0f) { m_Position.x = 0.f; } if (m_CurrentDirection == Direction::None && m_NextDirection != Direction::None && tilemap.GetDirectionFlag(cX, cY, (size_t)m_NextDirection)) {// if the current direction is None and the next input direction is not none and we can go in that direction m_CurrentDirection = m_NextDirection; // switch directions m_NextDirection = Direction::None; // set next to none } if (m_CurrentDirection != Direction::None) // if our current direction is not None ( we are not at base) { m_DistanceLeft -= speed; // reduce the distance of travel if (m_DistanceLeft <= 0.f) //if we have traveled 1 tile { if (!tilemap.GetDirectionFlag(cX, cY, (size_t)m_CurrentDirection)) // if the tile we are on dosent have the current direction direction flag set { m_CurrentDirection = Direction::None;// make curent dir none } if (m_NextDirection != Direction::None) // if we have a pending direction command { if (tilemap.GetDirectionFlag(cX, cY, (size_t)m_NextDirection)) // and if the tile we are one has the next direction tag set { m_CurrentDirection = m_NextDirection;// go in that direction } } m_DistanceLeft += 1.0f;// add distace to travel } } Rect rect(m_Position.x + 0.05f, m_Position.y + 0.05f, 0.9f, 0.9f);//construct a collision rect; //get all intersections auto intersections = tilemap.Intersect(rect); for (auto &i : intersections) // go trough them { if (i.type == Coin) // if its a coin { m_Points += 1; // increase points tilemap.Set(i.x, i.y, Empty); //remove coin tileSurface->Clear(); // clear tilemap render tileSurface->RenderTileset(gridSurf, tilemap.GetGrid(), 19, 32.0f);// re - render the grid without the coin. } } //set directions accortind to input keys if (input.GetKey(Left)) { m_NextDirection = Direction::Left; } if (input.GetKey(Right)) { m_NextDirection = Direction::Right; } if (input.GetKey(Up)) { m_NextDirection = Direction::Up; } if (input.GetKey(Down)) { m_NextDirection = Direction::Down; } }
BackgroundGenerator::BackgroundGenerator(Tilemap tm): m_tm(tm), m_currLine(tm.getHeight()), m_isWindowBeingCreated(true), m_windowLine(1), m_windowPos(10), m_lineSinceLastWindow(0) { }
void Builder::paintTileWallCorners(Tilemap& tileMap, uint32_t tileY, uint32_t tileX, const TileBrush& brush) { uint16_t cornerMask = kTileDirection_N | kTileDirection_W | kTileDirection_S | kTileDirection_E; Tile& thisTile = tileMap.at(tileY, tileX); if (thisTile.wall) { // this tile already has a wall - no need to run tests return; } //printf("at (%u,%u) => ", tileX, tileY); uint16_t wallRoleFlags = 0; if (tileY > 0) { const Tile& north = tileMap.at(tileY-1, tileX); //printf("north:[%u,%u], ", north.floor, north.wall); if (!tileWallsEqual(north, kTileDirection_W, brush.tileClassId) && !tileWallsEqual(north, kTileDirection_E, brush.tileClassId) && !tileWallsEqual(north, kTileDirection_NW, brush.tileClassId) && !tileWallsEqual(north, kTileDirection_NE, brush.tileClassId)) { cornerMask &= ~(kTileDirection_N); } } if (tileX > 0) { const Tile& west = tileMap.at(tileY, tileX-1); //printf("west:[%u,%u], ", west.floor, west.wall); if (!tileWallsEqual(west, kTileDirection_N, brush.tileClassId) && !tileWallsEqual(west, kTileDirection_S, brush.tileClassId) && !tileWallsEqual(west, kTileDirection_NW, brush.tileClassId) && !tileWallsEqual(west, kTileDirection_SW, brush.tileClassId)) { cornerMask &= ~(kTileDirection_W); } } if (tileY < tileMap.rowCount()-1) { const Tile& south = tileMap.at(tileY+1, tileX); //printf("south:[%u,%u], ", south.floor, south.wall); if (!tileWallsEqual(south, kTileDirection_W, brush.tileClassId) && !tileWallsEqual(south, kTileDirection_E, brush.tileClassId) && !tileWallsEqual(south, kTileDirection_SW, brush.tileClassId) && !tileWallsEqual(south, kTileDirection_SE, brush.tileClassId)) { cornerMask &= ~(kTileDirection_S); } } if (tileX < tileMap.columnCount()-1) { const Tile& east = tileMap.at(tileY, tileX+1); //printf("east:[%u,%u], ", east.floor, east.wall); if (!tileWallsEqual(east, kTileDirection_S, brush.tileClassId) && !tileWallsEqual(east, kTileDirection_N, brush.tileClassId) && !tileWallsEqual(east, kTileDirection_NE, brush.tileClassId) && !tileWallsEqual(east, kTileDirection_SE, brush.tileClassId)) { cornerMask &= ~(kTileDirection_E); } } if (cornerMask & kTileDirection_W) { if (cornerMask & kTileDirection_N) wallRoleFlags |= kTileDirection_NW; else if (cornerMask & kTileDirection_S) wallRoleFlags |= kTileDirection_SW; } if (!wallRoleFlags && (cornerMask & kTileDirection_N)) { if (cornerMask & kTileDirection_E) wallRoleFlags |= kTileDirection_NE; } if (!wallRoleFlags && (cornerMask & kTileDirection_E)) { if (cornerMask & kTileDirection_S) wallRoleFlags |= kTileDirection_SE; } //printf("cornerFlags, wallRoleFlags (%04x,%04x)\n", // cornerMask, wallRoleFlags); wallRoleFlags |= (kTileRole_Wall+kTileRole_Corner); TileHandle wallTileHandle = _tileTemplates.tileHandleFromDescriptor(brush.tileCategoryId, brush.tileClassId, wallRoleFlags); thisTile.wall = wallTileHandle; }
TilesArea::TilesArea(const Tilemap &tmap, const TilePos& leftup, const Size& size) { _size = size; append( tmap.area( leftup, size ) ); }
void Player::Update(Tilemap &t, sf::RenderWindow &window, BackgroundProcesses &b) { m_top = m_rect.getPosition().y; m_bottom = m_rect.getPosition().y + m_rect.getSize().y; m_left = m_rect.getPosition().x; m_right = m_rect.getPosition().x + m_rect.getSize().x; //collision with portal if(t.getLevelMap(m_bottom/64, m_left/64) == 999 || t.getLevelMap(m_bottom/64, m_right/64) == 999 || t.getLevelMap(m_top/64, m_left/64) == 999 || t.getLevelMap(m_top/64, m_right/64) == 999) { setPosition(sf::Vector2f(0, 0)); t.setMap(window); } //respawning when falling through the bottom of the map!! if(m_bottom >= 30*64) { setPosition(sf::Vector2f(0, 0)); t.setMap(t.getLevel(), window); } //downward collision on tilemap if(m_bottom / 64 < 0 || m_right < 0) m_onGround = false; else if(int(m_bottom) % 64 != 0) m_onGround = false; else if( (t.getLevelMap(m_bottom/64, m_left/64) >= 100 && t.getLevelMap(m_bottom/64, m_left/64) < 200) || (t.getLevelMap(m_bottom/64, m_left/64) >= 500 && t.getLevelMap(m_bottom/64, m_left/64) < 600) || (t.getLevelMap(m_bottom/64, m_right/64) >= 100 && t.getLevelMap(m_bottom/64, m_right/64) < 200) || (t.getLevelMap(m_bottom/64, m_right/64) >= 500 && t.getLevelMap(m_bottom/64, m_right/64) < 600) ) m_onGround = true; else m_onGround = false; //downward collision on platform for(int i = 0; i < t.getPlatformSize(); i++) { if(m_right < t.getPlatformLeft(i) || m_left > t.getPlatformRight(i) || m_top > t.getPlatformBottom(i) || m_bottom < t.getPlatformTop(i)) {} else if(int(m_bottom) <= t.getPlatformTop(i)) m_onGround = true; } int whichCrate = -1; for(int i = 0; i < t.getCrateSize(); i++) { //downward collision on crate if(m_right < t.getCrateLeft(i) || m_left > t.getCrateRight(i) || m_top > t.getCrateBottom(i) || m_bottom < t.getCrateTop(i)) {} else if(m_bottom >= t.getCrateTop(i) && m_bottom - 2 <= t.getCrateTop(i) && m_right - 2 > t.getCrateLeft(i) && m_left + 2 < t.getCrateRight(i)) { m_onGround = true; m_onCrate = true; whichCrate = i; break; } else { m_onCrate = false; } } for(int i = 0; i < t.getCrateSize(); i++) { //downward collision on crate /*if(m_right < t.getCrateLeft(i) || m_left > t.getCrateRight(i) || m_top > t.getCrateBottom(i) || m_bottom < t.getCrateTop(i)) {} else if(m_bottom >= t.getCrateTop(i) && m_bottom - 2 <= t.getCrateTop(i) && m_right - 2 > t.getCrateLeft(i) && m_left + 2 < t.getCrateRight(i)) { m_onGround = true; m_onCrate = true; } else { m_onCrate = false; } */ if(i != whichCrate) { //not colliding with crate if(m_right < t.getCrateLeft(i) || m_left > t.getCrateRight(i) || m_top > t.getCrateBottom(i) || m_bottom < t.getCrateTop(i)) {} //on top of crate else if(m_top <= t.getCrateBottom(i) && m_top >= (t.getCrateBottom(i) - 2)) { t.setCratePos(i, sf::Vector2f(t.getCrateLeft(i), m_top - (t.getCrateBottom(i) - t.getCrateTop(i)))); } //pushing crate right else if(m_right >= t.getCrateLeft(i) && m_left < t.getCrateLeft(i)) { if(m_movement.x > 0) t.setCratePos(i, sf::Vector2f(m_right, t.getCrateTop(i))); } //pushing crate left else if(m_left <= t.getCrateRight(i) && m_left > t.getCrateLeft(i)) { if(m_movement.x < 0) t.setCratePos(i, sf::Vector2f(m_left - (t.getCrateRight(i) - t.getCrateLeft(i)), t.getCrateTop(i))); } } } }
void Level::Load(const std::string &filename, Scene* scene) { // load from an xml file, into the scene if (scene != NULL) { instance->scene = scene; } if (instance->scene) { // unload tilemaps... (need to destroy them?) instance->tilemaps.clear(); //clearfringeTileset TiXmlDocument xml(Assets::GetContentPath() + filename); instance->filename = filename; bool isLoaded = xml.LoadFile(); if (isLoaded) { TiXmlElement* eLevel = xml.FirstChildElement("Level"); if (eLevel) { instance->width = XMLReadInt(eLevel, "width"); instance->height = XMLReadInt(eLevel, "height"); TiXmlElement *eTilemap = eLevel->FirstChildElement("Tilemap"); while (eTilemap) { Entity *entity = new Entity(); Tilemap *tilemap = new Tilemap(instance->GetTilesetByName(XMLReadString(eTilemap, "set")), instance->width, instance->height, XMLReadInt(eTilemap, "tileWidth"), XMLReadInt(eTilemap, "tileHeight")); instance->tilemaps.push_back(tilemap); entity->SetGraphic(tilemap); instance->scene->Add(entity); TiXmlElement *eTile = eTilemap->FirstChildElement("Tile"); while (eTile) { tilemap->SetTile(XMLReadInt(eTile, "x"), XMLReadInt(eTile, "y"), XMLReadInt(eTile, "tileID")); eTile = eTile->NextSiblingElement("Tile"); } eTilemap = eTilemap->NextSiblingElement("Tilemap"); } TiXmlElement *eFringeTiles = eLevel->FirstChildElement("FringeTiles"); while (eFringeTiles) { FringeTileset *fringeTileset = instance->GetFringeTilesetByName(XMLReadString(eFringeTiles, "set")); /* Entity *entity = new Entity(); Tilemap *tilemap = new Tilemap(instance->GetTileset(XMLString(eTilemap, "set")), instance->width, instance->height, XMLInt(eTilemap, "tileWidth"), XMLInt(eTilemap, "tileHeight")); instance->tilemaps.push_back(tilemap); entity->SetGraphic(tilemap); instance->scene->Add(entity); */ if (fringeTileset) { TiXmlElement *eFringeTile = eFringeTiles->FirstChildElement("FringeTile"); while (eFringeTile) { int tileID = XMLReadInt(eFringeTile, "id"); int layer = XMLReadInt(eFringeTile, "layer"); Vector2 position = Vector2(XMLReadFloat(eFringeTile, "x"), XMLReadFloat(eFringeTile, "y")); Vector2 scale = Vector2::one; if (eFringeTile->Attribute("scaleX") != NULL && eFringeTile->Attribute("scaleY") != NULL) scale = Vector2(XMLReadFloat(eFringeTile, "scaleX"), XMLReadFloat(eFringeTile, "scaleY")); int rotation = XMLReadFloat(eFringeTile, "rotation"); Color color = Color::white; if (eFringeTile->Attribute("ca")) { color.a = XMLReadFloat(eFringeTile, "ca"); } AddFringeTile(fringeTileset, tileID, layer, position, scale, rotation, color); eFringeTile = eFringeTile->NextSiblingElement("FringeTile"); } } eFringeTiles = eFringeTiles->NextSiblingElement("FringeTiles"); } } } } }
void Builder::paintTileWalls(Tilemap& tileMap, uint32_t tileY, uint32_t tileX, const TileBrush& brush) { const TileTemplate& thisFloorTemplate = _tileTemplates.tile(tileMap.at(tileY, tileX).floor); // calculate wall masks, which are used to determine what wall tiles // to display. uint16_t wallMask = kTileDirection_N | kTileDirection_E | kTileDirection_W | kTileDirection_S; if (tileY > 0 && tileFloorsClassIdEqual(tileMap.at(tileY-1, tileX), thisFloorTemplate.classId)) { wallMask &= ~(kTileDirection_N); } if (tileX > 0 && tileFloorsClassIdEqual(tileMap.at(tileY, tileX-1), thisFloorTemplate.classId)) { wallMask &= ~(kTileDirection_W); } if (tileY < tileMap.rowCount()-1 && tileFloorsClassIdEqual(tileMap.at(tileY+1, tileX), thisFloorTemplate.classId)) { wallMask &= ~(kTileDirection_S); } if (tileX < tileMap.columnCount()-1 && tileFloorsClassIdEqual(tileMap.at(tileY, tileX+1), thisFloorTemplate.classId)) { wallMask &= ~(kTileDirection_E); } uint16_t wallRoleFlags = 0; if (wallMask) { if (wallMask & kTileDirection_W) { if (wallMask & kTileDirection_N) wallRoleFlags |= kTileDirection_NW; else if (wallMask & kTileDirection_S) wallRoleFlags |= kTileDirection_SW; else wallRoleFlags |= kTileDirection_W; } if (!wallRoleFlags && (wallMask & kTileDirection_N)) { // we've already evaluated for West, so only need to eval East if (wallMask & kTileDirection_E) wallRoleFlags |= kTileDirection_NE; else wallRoleFlags |= kTileDirection_N; } if (!wallRoleFlags && (wallMask & kTileDirection_E)) { // we've already evaluated North, so only care about South if (wallMask & kTileDirection_S) wallRoleFlags |= kTileDirection_SE; else wallRoleFlags |= kTileDirection_E; } if (!wallRoleFlags && (wallMask & kTileDirection_S)) { // we've already evaluated East and West, so... wallRoleFlags |= kTileDirection_S; } wallRoleFlags |= kTileRole_Wall; } TileHandle wallTileHandle = _tileTemplates.tileHandleFromDescriptor(brush.tileCategoryId, brush.tileClassId, wallRoleFlags); Tile& thisTile = tileMap.at(tileY, tileX); thisTile.wall = wallTileHandle; }
TilesArea::TilesArea(const Tilemap &tmap, const TilePos& leftup, const TilePos& rightdown) { _size = Size( abs( rightdown.i() - leftup.i() ), abs( rightdown.j() - leftup.j() ) ); append( tmap.area( leftup, rightdown ) ); }
bool haveReservoirWater( const Tilemap& tm, const TilePos& pos ) { TilesArray possiblePlace = tm.getArea( pos, pos + TilePos(1,1) ); return !possiblePlace.select( Tile::pReservoirWater ).empty(); }
int main() { init(); Spritesheet levelSprites("testpic.png", 32, &mainWindow); gameMap.sprites = &levelSprites; gameMap.loadFile("testmap.map"); gameMap.update(&mainWindow); Character Player; Player.position.x = 100; Player.position.y = SCREEN_HEIGHT - 90; Player.gravity = 5000.0; Player.runSpeed = 500.0; Player.jumpVelocity = 800.0; Player.jumpHeightMax = 128.0; Player.terminalVelocity = 1024.0; Player.rect.w = 32; Player.rect.h = 64; Player.rect.x = int(Player.position.x); Player.rect.y = int(Player.position.y); Player.origin.x = (double)(Player.rect.w / 2); Player.origin.y = (double)Player.rect.h; const Uint8 *keystate = SDL_GetKeyboardState(NULL); SDL_Event e; bool quit = false; double frameTime = 0.0; system_clock::time_point lastTime = system_clock::now(); while (!quit) { frameTime = duration_cast<microseconds>(system_clock::now() - lastTime).count() / 1000000.0; if (frameTime > 0.1) frameTime = 0.1; //at low framerates game becomes frame dependent to avoid collision errors etc. lastTime = system_clock::now(); //event block SDL_PumpEvents(); while (SDL_PollEvent(&e)) { if (e.type == SDL_WINDOWEVENT) { mainWindow.handleEvents(&e); } if (e.type == SDL_QUIT) { quit = true; } if (e.type == SDL_KEYDOWN) { switch (e.key.keysym.sym) { case SDLK_a: case SDLK_LEFT: { Player.velocity.x = -Player.runSpeed; break; } case SDLK_d: case SDLK_RIGHT: { Player.velocity.x = Player.runSpeed; break; } default: break; } } else if (e.type == SDL_KEYUP) { switch (e.key.keysym.sym) { case SDLK_a: case SDLK_LEFT: { if (keystate[SDL_SCANCODE_RIGHT] || keystate[SDL_SCANCODE_D]) Player.velocity.x = Player.runSpeed; else Player.velocity.x = 0.0; break; } case SDLK_d: case SDLK_RIGHT: { if (keystate[SDL_SCANCODE_LEFT] || keystate[SDL_SCANCODE_A]) Player.velocity.x = -Player.runSpeed; else Player.velocity.x = 0.0; break; } default: break; } } } if (!Player.freeFall && (keystate[SDL_SCANCODE_UP] || keystate[SDL_SCANCODE_W])) { Player.jump(); } else if (Player.airBorne) { Player.freeFall = true; } Player.move(frameTime); //rendering block SDL_SetRenderDrawColor(mainWindow.ren, 0, 0, 0, 255); SDL_RenderClear(mainWindow.ren); gameMap.render(&mainWindow); if (checkMapCollision(Player, gameMap)) SDL_SetRenderDrawColor(mainWindow.ren, 0, 0, 255, 255); else SDL_SetRenderDrawColor(mainWindow.ren, 255, 0, 0, 255); SDL_RenderFillRect(mainWindow.ren, &Player.rect); SDL_RenderPresent(mainWindow.ren); SDL_Delay(1); } close(); }
void Player::Move(sf::Time &deltaTime, Tilemap &t) { //moving right and slowing down if(m_isMovingRight == true) m_movement.x += m_movSpeedConst * deltaTime.asMilliseconds(); else if(m_isMovingRight == false && m_movement.x > 0) { m_movement.x -= m_movSpeedConst * 1.5 * deltaTime.asMilliseconds(); if(m_movement.x < 0) m_movement.x = 0; } //moving left and slowing down if(m_isMovingLeft == true) m_movement.x -= m_movSpeedConst * deltaTime.asMilliseconds(); else if(m_isMovingLeft == false && m_movement.x < 0) { m_movement.x += m_movSpeedConst* 1.5 * deltaTime.asMilliseconds(); if(m_movement.x > 0) m_movement.x = 0; } //not over max speed if(m_movement.x >= m_movSpeedMax) m_movement.x = m_movSpeedMax; else if(m_movement.x <= -m_movSpeedMax) m_movement.x = -m_movSpeedMax; //don't move left right if colliding with wall if(int(m_bottom) % 64 == 0 && (t.getLevelMap((m_bottom + 2)/64, m_left/64) == 192 || t.getLevelMap((m_bottom + 2)/64, m_left/64) == 191 || t.getLevelMap((m_bottom + 2)/64, m_right/64) == 191 || t.getLevelMap((m_bottom + 2)/64, m_right/64) == 190 || t.getLevelMap((m_bottom + 2)/64, m_left/64) == 192 || t.getLevelMap((m_bottom + 2)/64, m_left/64) == 592 || t.getLevelMap((m_bottom + 2)/64, m_left/64) == 591 || t.getLevelMap((m_bottom + 2)/64, m_right/64) == 591 || t.getLevelMap((m_bottom + 2)/64, m_right/64) == 590)) {} else if((t.getLevelMap(m_bottom/64, m_left/64) == 192 || t.getLevelMap(m_bottom/64, m_left/64) == 292 || t.getLevelMap(m_bottom/64, m_left/64) == 392 || t.getLevelMap(m_bottom/64, m_left/64) == 490 || t.getLevelMap(m_bottom/64, m_left/64) == 592 || t.getLevelMap(m_bottom/64, m_left/64) == 191 || t.getLevelMap(m_bottom/64, m_left/64) == 291 || t.getLevelMap(m_bottom/64, m_left/64) == 391 || t.getLevelMap(m_bottom/64, m_left/64) == 491 || t.getLevelMap(m_bottom/64, m_left/64) == 591) && m_movement.x < 0) m_movement.x = 0; else if((t.getLevelMap(m_bottom/64, m_right/64) == 190 || t.getLevelMap(m_bottom/64, m_right/64) == 290 || t.getLevelMap(m_bottom/64, m_right/64) == 390 || t.getLevelMap(m_bottom/64, m_right/64) == 492 || t.getLevelMap(m_bottom/64, m_right/64) == 590 || t.getLevelMap(m_bottom/64, m_right/64) == 191 || t.getLevelMap(m_bottom/64, m_right/64) == 291 || t.getLevelMap(m_bottom/64, m_right/64) == 391 || t.getLevelMap(m_bottom/64, m_right/64) == 491 || t.getLevelMap(m_bottom/64, m_right/64) == 591) && m_movement.x > 0) m_movement.x = 0; else if((t.getLevelMap((m_top + 2)/64, m_left/64) == 192 || t.getLevelMap((m_top + 2)/64, m_left/64) == 292 || t.getLevelMap((m_top + 2)/64, m_left/64) == 392 || t.getLevelMap((m_top + 2)/64, m_left/64) == 490 || t.getLevelMap((m_top + 2)/64, m_left/64) == 592 || t.getLevelMap((m_top + 2)/64, m_left/64) == 191 || t.getLevelMap((m_top + 2)/64, m_left/64) == 291 || t.getLevelMap((m_top + 2)/64, m_left/64) == 391 || t.getLevelMap((m_top + 2)/64, m_left/64) == 491 || t.getLevelMap((m_top + 2)/64, m_left/64) == 591) && m_movement.x < 0) m_movement.x = 0; else if((t.getLevelMap((m_top + 2)/64, m_right/64) == 190 || t.getLevelMap((m_top + 2)/64, m_right/64) == 290 || t.getLevelMap((m_top + 2)/64, m_right/64) == 390 || t.getLevelMap((m_top + 2)/64, m_right/64) == 492 || t.getLevelMap((m_top + 2)/64, m_right/64) == 590 || t.getLevelMap((m_top + 2)/64, m_right/64) == 191 || t.getLevelMap((m_top + 2)/64, m_right/64) == 291 || t.getLevelMap((m_top + 2)/64, m_right/64) == 391 || t.getLevelMap((m_top + 2)/64, m_right/64) == 491 || t.getLevelMap((m_top + 2)/64, m_right/64) == 591) && m_movement.x > 0) m_movement.x = 0; //don't move if colliding with a colliding crate! for(int i = 0; i < t.getCrateSize(); i++) { if(t.getCrateOnLeft(i) == true || t.getCrateOnRight(i) == true) { if(m_right < t.getCrateLeft(i) || m_left > t.getCrateRight(i) || m_top > t.getCrateBottom(i) || m_bottom < t.getCrateTop(i)) {} else if(m_right >= t.getCrateLeft(i) && (m_right + m_left)/2 <= t.getCrateLeft(i) && m_movement.x > 0 &&(m_bottom - 2) >= t.getCrateTop(i)) m_movement.x = 0; else if(m_left <= t.getCrateRight(i) && (m_right + m_left)/2 >= t.getCrateLeft(i) && m_movement.x < 0 && (m_bottom - 2) >= t.getCrateTop(i)) m_movement.x = 0; } } //jumping if(sf::Keyboard::isKeyPressed(sf::Keyboard::W) || sf::Keyboard::isKeyPressed(sf::Keyboard::Space) || sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) m_jumped = true; //if hit top of block, fall if((t.getLevelMap(m_top/64, (m_left + 2)/64) == 190 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 190 || t.getLevelMap(m_top/64, (m_left + 2)/64) == 191 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 191 || t.getLevelMap(m_top/64, (m_left + 2)/64) == 192 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 192 || t.getLevelMap(m_top/64, (m_left + 2)/64) == 290 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 290 || t.getLevelMap(m_top/64, (m_left + 2)/64) == 291 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 291 || t.getLevelMap(m_top/64, (m_left + 2)/64) == 292 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 292 || t.getLevelMap(m_top/64, (m_left + 2)/64) == 390 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 390 || t.getLevelMap(m_top/64, (m_left + 2)/64) == 391 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 391 || t.getLevelMap(m_top/64, (m_left + 2)/64) == 392 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 392 || t.getLevelMap(m_top/64, (m_left + 2)/64) == 490 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 490 || t.getLevelMap(m_top/64, (m_left + 2)/64) == 491 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 491 || t.getLevelMap(m_top/64, (m_left + 2)/64) == 492 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 492 || t.getLevelMap(m_top/64, (m_left + 2)/64) == 590 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 590 || t.getLevelMap(m_top/64, (m_left + 2)/64) == 591 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 591 || t.getLevelMap(m_top/64, (m_left + 2)/64) == 592 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 592) && m_movement.y < 0) m_movement.y = 0; //setting jump stuff if(m_jumped == true && m_onGround == true) { m_movement.y = -m_gravity; m_jumped = false; } else { m_jumped = false; if(!m_onGround) m_movement.y += m_gravity * 0.003 * deltaTime.asMilliseconds(); else m_movement.y = 0; if(m_movement.y > 0.5) m_movement.y = 0.5; } //move if on a platform for(int i = 0; i < t.getPlatformSize(); i++) { //not touching if(m_right < t.getPlatformLeft(i) || m_left > t.getPlatformRight(i) || m_top > t.getPlatformBottom(i) || m_bottom < t.getPlatformTop(i)) {} else if(m_bottom - 2 > t.getPlatformTop(i)) {} else if(m_left > t.getPlatformLeft(i) && m_right < t.getPlatformRight(i)) m_movement.x += t.getPlatformMovement(i).x; else if( m_left < t.getPlatformLeft(i) && (m_left + m_right)/2 > t.getPlatformLeft(i) && int(m_movement.x) == 0) m_movement.x += t.getPlatformMovement(i).x; else if( m_right > t.getPlatformRight(i) && (m_left + m_right)/2 < t.getPlatformRight(i) && int(m_movement.x) == 0) m_movement.x += t.getPlatformMovement(i).x; } //move if on a crate (movement will be non-zero if crate is on a platform so...) /*for(int i = 0; i < t.getCrateSize(); i++) { if(m_right < t.getCrateLeft(i) || m_left > t.getCrateRight(i) || m_top > t.getCrateBottom(i) || m_bottom < t.getCrateTop(i)) {} else if(int(m_bottom) <= t.getCrateTop(i)) m_movement.x += t.getCrateMovement(i).x; }*/ //the actual move part m_rect.move(m_movement.x, m_movement.y); m_sprite.move(m_movement.x, m_movement.y); //change the movement after actually moving so it doesn't mess up earlier processes for(int i = 0; i < t.getPlatformSize(); i++) { //not touching if(m_right < t.getPlatformLeft(i) || m_left > t.getPlatformRight(i) || m_top > t.getPlatformBottom(i) || m_bottom < t.getPlatformTop(i)) {} else if(m_bottom - 2 > t.getPlatformTop(i)) {} else if(m_left > t.getPlatformLeft(i) && m_right < t.getPlatformRight(i)) m_movement.x -= t.getPlatformMovement(i).x; else if( m_left < t.getPlatformLeft(i) && (m_left + m_right)/2 > t.getPlatformLeft(i) && int(m_movement.x) == 0) m_movement.x -= t.getPlatformMovement(i).x; else if( m_right > t.getPlatformRight(i) && (m_left + m_right)/2 < t.getPlatformRight(i) && int(m_movement.x) == 0) m_movement.x -= t.getPlatformMovement(i).x; } //change the movement after actually moving the crate so it doesn't mess up earlier processes /*for(int i = 0; i < t.getCrateSize(); i++) { if(m_right < t.getCrateLeft(i) || m_left > t.getCrateRight(i) || m_top > t.getCrateBottom(i) || m_bottom < t.getCrateTop(i)) {} else if(int(m_bottom) <= t.getCrateTop(i)) m_movement.x -= t.getCrateMovement(i).x; }*/ /*if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W)) //FOR GOD MODE!! m_movement.y = -0.25; else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S)) m_movement.y = 0.25; else m_movement.y = 0; if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) m_movement.x = -0.25; if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)) m_movement.x = 0.25; */ }
RoadPropagator::RoadPropagator( const Tilemap& tileMap, const Tile& startTile ) : _d( new Impl( tileMap, startTile ) ) { _d->mapSize = tileMap.getSize(); }