public CCTileMap(CCTileMapInfo mapInfo) : base(CCCameraProjection.Projection2D) { Type = mapInfo.MapType; MapDimensions = mapInfo.MapDimensions; MapInfo = mapInfo; TileTexelSize = mapInfo.TileTexelSize; ObjectGroups = mapInfo.ObjectGroups; MapProperties = mapInfo.MapProperties; TileProperties = mapInfo.TileProperties; TileLayersContainer = new CCNode(MapDimensions.Size * TileTexelSize * CCTileMapLayer.DefaultTexelToContentSizeRatios); AddChild(TileLayersContainer); int idx = 0; List<CCTileLayerInfo> layers = mapInfo.Layers; if (layers != null) { foreach (CCTileLayerInfo layerInfo in layers) { if (layerInfo.Visible) { CCTileSetInfo[] tilesets = TilesetsForLayer(layerInfo); CCTileMapLayer child = new CCTileMapLayer(tilesets, layerInfo, mapInfo); TileLayersContainer.AddChild(child, idx, idx); idx++; } } } }
private void createBox2DWorldByLayer(CCTileMapLayer physicsLayer, CCTileMap physicsMap) { bool[,] Tile = extractHitboxTiles (physicsLayer, physicsMap); bool[,] Checked = new bool[(int)physicsLayer.LayerSize.Size.Width, (int)physicsLayer.LayerSize.Size.Height]; //geht das Abbild durch int CurrentWidth; CurrentWidth = 0; int LastX; LastX = 0; for (int y = 0; y < physicsLayer.LayerSize.Size.Height; y++) { for (int x = 0; x < physicsLayer.LayerSize.Size.Width; x++) { if (Tile [x, y] == false) { if (CurrentWidth > 0) { createBoxAt (LastX * (int)physicsLayer.TileTexelSize.Width * (int)physicsMap.ScaleX, ((int)physicsLayer.LayerSize.Size.Height - y - 1) * (int)physicsLayer.TileTexelSize.Height * (int)physicsMap.ScaleY, CurrentWidth * physicsLayer.TileTexelSize.Width * physicsMap.ScaleX, physicsLayer.TileTexelSize.Height * (int)physicsMap.ScaleY); } LastX = x + 1; CurrentWidth = 0; } else { CurrentWidth += 1; } } if (CurrentWidth > 0) { createBoxAt (LastX * (int)physicsLayer.TileTexelSize.Width, ((int)physicsLayer.LayerSize.Size.Height - y - 1) * (int)physicsLayer.TileTexelSize.Height, CurrentWidth * physicsLayer.TileTexelSize.Width, physicsLayer.TileTexelSize.Height); } LastX = 0; CurrentWidth = 0; } }
public void Initialize(float mapSizeWidth, Character Character, CCTileMapLayer physicsLayer, CCTileMap physicsMap, Container gameContainer) { mapSizeWidth /= pixelPerMeter; gameWorld = new b2World (new b2Vec2 (0, -9.8f)); gameWorld.AllowSleep = false; //definiert den MainCharacter Character.createPhysicsBody (gameWorld); defineGround (mapSizeWidth); createBox2DWorldByLayer (physicsLayer, physicsMap); debugDrawer = new DebugDraw (); gameWorld.SetDebugDraw (debugDrawer); debugDrawer.AppendFlags (b2DrawFlags.e_shapeBit); collusionSensor = new CollusionSensor (gameContainer); gameWorld.SetContactListener (collusionSensor); }
public CCTileMapDrawBufferManager(int mapCols, int mapRows, CCTileMapLayer tileMapLayer, CCTileSetInfo tileSetInfo) { Buffers = new List <CCTileMapVertAndIndexBuffer>(); TileSetInfo = tileSetInfo; TileMapLayer = tileMapLayer; // Create the appropriate number of buffers to hold the map. int numOfTiles = mapCols * mapRows; int tilesProcessed = 0; while (tilesProcessed < numOfTiles) { int tilesLeft = numOfTiles - tilesProcessed; int tilesInThisBuffer = Math.Min(tilesLeft, MaxTilesPerDrawBuffer); CreateDrawBuffer(tilesInThisBuffer, tilesProcessed); tilesProcessed += tilesInThisBuffer; } }
public CCTileMap(CCTileMapInfo mapInfo) : base(CCCameraProjection.Projection2D) { Type = mapInfo.MapType; MapDimensions = mapInfo.MapDimensions; MapInfo = mapInfo; TileTexelSize = mapInfo.TileTexelSize; ObjectGroups = mapInfo.ObjectGroups; MapProperties = mapInfo.MapProperties; TileProperties = mapInfo.TileProperties; TileLayersContainer = new CCNode(MapDimensions.Size * TileTexelSize * CCTileMapLayer.DefaultTexelToContentSizeRatios); AddChild(TileLayersContainer); int idx = 0; List <CCTileLayerInfo> layers = mapInfo.Layers; if (layers != null) { foreach (CCTileLayerInfo layerInfo in layers) { if (layerInfo.Visible) { CCTileSetInfo tileset = TilesetForLayer(layerInfo); CCTileMapLayer child = new CCTileMapLayer(tileset, layerInfo, mapInfo); TileLayersContainer.AddChild(child, idx, idx); idx++; } } } }
//Handles individual tiles at individual layers //Some things shouldn't be done by the loop such as exiting the game - so use the calledByLoop to differentiate between the two //if this gets messy could split this function into two for each use void tileHandler(CCPoint world, CCTileMapLayer layer, character oldUser, Boolean calledByLoop=false) { CCTileMapCoordinates tileAtXy = layer.ClosestTileCoordAtNodePosition(world); // get tile coordinates corresponding to our touch location CCTileGidAndFlags info = layer.TileGIDAndFlags(tileAtXy.Column, tileAtXy.Row); //Tilemaps made in the "tiled" program can hold properties - get the properties of the tile Dictionary<string, string> properties = TilePropertiesForGID(info.Gid); //Clicking a walkable tile if (!calledByLoop && properties != null && properties.ContainsKey("walkable") && properties["walkable"] == "true") { //if tile has an enemy and user is near it if(isTileOccupied(tileAtXy) && isUserNear(tileAtXy)) { character attackedEnemy = getEnemyAt(tileAtXy); if (attackedEnemy.attacked(user.weapon.attack)) enemyDeath(attackedEnemy); foreach (character enemy in enemiesList) { CCTileMapCoordinates positionAsTile = LayerNamed("Map").ClosestTileCoordAtNodePosition(enemy.Position); if (!isUserNear(positionAsTile)) enemy.moveOneRandom(); else { if (user.attacked(enemy.weapon.attack) && !ifdied) { userDeath(); break; } } } } //TODO - if tile has an item, pick it up if(!isTileOccupied(tileAtXy)) userMoves = user.move(world); //get new pathfind } //Spawning in the user - only happens once - not called by user if (user == null && calledByLoop && properties != null && properties.ContainsKey("name") && properties["name"] == "exit") { if(oldUser==null) { user = new character("userChar", 20, world, availableWeapons[0]); layer.AddChild(user); } else { user = new character(oldUser, world); layer.AddChild(user); } } }
public PathFinder(CCTileMap setMap) { map = setMap; mapLayer = map.LayerNamed("Map"); }
private void LoadLevel(int levelNumber) { currentLevel = new CCTileMap ("level" + levelNumber + ".tmx"); currentLevel.Antialiased = false; backgroundLayer = currentLevel.LayerNamed ("Background"); // CCTileMap is a CCLayer, so we'll just add it under all entities this.AddChild (currentLevel); // put the game layer after this.RemoveChild(gameplayLayer); this.AddChild(gameplayLayer); this.RemoveChild (hudLayer); this.AddChild (hudLayer); }
public void PopulateFrom(CCTileMap tileMap) { //得到瓦片地图里面的瓦片信息,比如说哪些瓦片可以与entity接触,具体是瓦片的哪个方向的面可以接触 collisions=new List<RectWithDirection> (); //每个小瓦片的边长加上0.5,这样碰撞时就不会陷进去才弹出 tileDimensionWidth = (int)(tileMap.TileTexelSize.Width + .5f); tileDimensionHeight = (int)(tileMap.TileTexelSize.Height + .5f); mBrickLayer = tileMap.LayerNamed ("BrickLayer"); TileMapPropertyFinder finder = new TileMapPropertyFinder (tileMap); foreach (var propertyLocation in finder.GetPropertyLocations()) { //如果在这个位置的瓦片是一个固体 if (propertyLocation.Properties.ContainsKey ("SolidCollision")) { //worldX worldY 是每个瓦片的中心的坐标 float centerX = propertyLocation.WorldX; float centerY = propertyLocation.WorldY; //得到每个小瓦片的左边界和下边界 float left = centerX - tileDimensionWidth/2.0f; float bottom = centerY - tileDimensionHeight/2.0f; //在那个点构造一个小瓦片 RectWithDirection rectangle = new RectWithDirection { Left = left, Bottom = bottom, Width = tileDimensionWidth, Height = tileDimensionHeight, tiledMapCoordinate=propertyLocation.TileCoordinates }; //得到地图上所有的固体小块 collisions.Add (rectangle); } } // Sort by XAxis to speed future searches: //collisions是把整个瓦片地图按每个瓦片的左边的坐标进行排序的list //在每一个左边坐标有一列的瓦片 //debug看一下 collisions = collisions.OrderBy(item=>item.Left).ToList(); // now let's adjust the directions that these point //调整每个小块的角度 for (int i = 0; i < collisions.Count; i++) { var rect = collisions [i]; // By default rectangles can reposition objects in all directions: int valueToAssign = (int)Directions.All; //15 float centerX = rect.CenterX; float centerY = rect.CenterY; // If there are collisions on the sides, then this // rectangle can no longer repositon objects in that direction. // 一开始小瓦片的方向valueToAssign可能是所有的方向,每次减去一种不可能的方向值,最后得到的就是正确的方向的值的和 //direction是这个实体瓦片暴露在外,可以与entity接触的方向 //比如说瓦片地图里的地面,方向就为up if (HasCollisionAt (centerX - tileDimensionWidth, centerY)) { valueToAssign -= (int)Directions.Left; } if (HasCollisionAt (centerX + tileDimensionWidth, centerY)) { valueToAssign -= (int)Directions.Right; } if (HasCollisionAt (centerX, centerY + tileDimensionHeight)) { valueToAssign -= (int)Directions.Up; } if (HasCollisionAt (centerX, centerY - tileDimensionHeight)) { valueToAssign -= (int)Directions.Down; } rect.Directions = (Directions)valueToAssign; //更新瓦片列表中的瓦片的方向 collisions [i] = rect; } for (int i = collisions.Count - 1; i > -1; i--) { //经过筛选后,遍历删除那些没有方向的瓦片 if (collisions [i].Directions == Directions.None) { collisions.RemoveAt (i); } } }
void loadTileMap() { //加载tiledmap预处理 mLevelTest = new CCTileMap ("LevelTest.tmx"); mLevelTest.Antialiased = false; mTiledLayer = mLevelTest.LayerNamed("BrickLayer"); gameplayLayer.AddChild (mTiledLayer); //初始化地图 levelCollision = new LevelCollision (); levelCollision.PopulateFrom (mLevelTest); }
private void LoadLevel(int levelNumber) { //读取瓦片地图 currentLevel = new CCTileMap ("level" + levelNumber + ".tmx"); currentLevel.Antialiased = false; //backgroundLayer是游戏中不动的背景图, 每个currentLevel都有一个对应的backgroundLayer, currentLevel中的其他部分随画面滚动 backgroundLayer = currentLevel.LayerNamed ("Background"); // CCTileMap is a CCLayer, so we'll just add it under all entities this.AddChild (currentLevel); //levelCollision 是确定地图中哪些部分是游戏人物不可以进入的 levelCollision = new LevelCollision (); levelCollision.PopulateFrom (currentLevel); // put the game layer after this.RemoveChild(gameplayLayer); this.AddChild(gameplayLayer); this.RemoveChild (hudLayer); this.AddChild (hudLayer); }
public CCTileMapDrawBufferManager(int mapCols, int mapRows, CCTileMapLayer tileMapLayer, CCTileSetInfo tileSetInfo) { Buffers = new List<CCTileMapVertAndIndexBuffer>(); TileSetInfo = tileSetInfo; TileMapLayer = tileMapLayer; // Create the appropriate number of buffers to hold the map. int numOfTiles = mapCols * mapRows; int tilesProcessed = 0; while (tilesProcessed < numOfTiles) { int tilesLeft = numOfTiles - tilesProcessed; int tilesInThisBuffer = Math.Min(tilesLeft, MaxTilesPerDrawBuffer); CreateDrawBuffer(tilesInThisBuffer, tilesProcessed); tilesProcessed += tilesInThisBuffer; } }
private bool[,] extractHitboxTiles(CCTileMapLayer physicsLayer, CCTileMap physicsMap) { bool[,] Tile = new bool[(int)physicsLayer.LayerSize.Size.Width, (int)physicsLayer.LayerSize.Size.Height]; //erstellt ein bool Abbild der Map Dictionary<string,string> tileProperties; for (int x = 1; x < physicsLayer.LayerSize.Size.Width - 1; x++) { for (int y = 1; y < physicsLayer.LayerSize.Size.Height - 1; y++) { //wenn das tile allein steht, dann ist es ein Element der Hitbox tileProperties = physicsMap.TilePropertiesForGID (physicsLayer.TileGIDAndFlags (new CCTileMapCoordinates (x, y)).Gid); if (tileProperties != null) { tileProperties = physicsMap.TilePropertiesForGID (physicsLayer.TileGIDAndFlags (new CCTileMapCoordinates (x - 1, y)).Gid); if (tileProperties == null) { Tile [x, y] = true; } tileProperties = null; tileProperties = physicsMap.TilePropertiesForGID (physicsLayer.TileGIDAndFlags (new CCTileMapCoordinates (x + 1, y)).Gid); if (tileProperties == null) { Tile [x, y] = true; } tileProperties = null; tileProperties = physicsMap.TilePropertiesForGID (physicsLayer.TileGIDAndFlags (new CCTileMapCoordinates (x, y - 1)).Gid); if (tileProperties == null) { Tile [x, y] = true; } tileProperties = null; tileProperties = physicsMap.TilePropertiesForGID (physicsLayer.TileGIDAndFlags (new CCTileMapCoordinates (x, y + 1)).Gid); if (tileProperties == null) { Tile [x, y] = true; } tileProperties = null; } } } return Tile; }