public void PopulateFrom(CCTileMap tileMap) { //得到瓦片地图里面的瓦片信息,比如说哪些瓦片可以与entity接触,具体是瓦片的哪个方向的面可以接触 //每个小瓦片的边长加上0.5的误差 tileDimension = (int)(tileMap.TileTexelSize.Width + .5f); 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 - tileDimension/2.0f; float bottom = centerY - tileDimension/2.0f; //在那个点构造一个小瓦片 RectWithDirection rectangle = new RectWithDirection { Left = left, Bottom = bottom, Width = tileDimension, Height = tileDimension }; //得到地图上所有的固体小块 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 - tileDimension, centerY)) { valueToAssign -= (int)Directions.Left; } if (HasCollisionAt (centerX + tileDimension, centerY)) { valueToAssign -= (int)Directions.Right; } if (HasCollisionAt (centerX, centerY + tileDimension)) { valueToAssign -= (int)Directions.Up; } if (HasCollisionAt (centerX, centerY - tileDimension)) { 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); } } }
private void ProcessTileProperties() { TileMapPropertyFinder finder = new TileMapPropertyFinder (currentLevel); foreach (var propertyLocation in finder.GetPropertyLocations()) { var properties = propertyLocation.Properties; if (properties.ContainsKey ("EntityType")) { float worldX = propertyLocation.WorldX; float worldY = propertyLocation.WorldY; if (properties.ContainsKey ("YOffset")) { string yOffsetAsString = properties ["YOffset"]; float yOffset = 0; float.TryParse (yOffsetAsString, out yOffset); worldY += yOffset; } bool created = TryCreateEntity (properties ["EntityType"], worldX, worldY); if (created) { propertyLocation.Layer.RemoveTile (propertyLocation.TileCoordinates); } } else if (properties.ContainsKey ("RemoveMe")) { propertyLocation.Layer.RemoveTile (propertyLocation.TileCoordinates); } } touchScreen = new TouchScreenInput(gameplayLayer); }
public void PopulateFrom(CCTileMap tileMap) { tileDimension = (int)(tileMap.TileTexelSize.Width + .5f); TileMapPropertyFinder finder = new TileMapPropertyFinder (tileMap); foreach (var propertyLocation in finder.GetPropertyLocations()) { if (propertyLocation.Properties.ContainsKey ("SolidCollision")) { float centerX = propertyLocation.WorldX; float centerY = propertyLocation.WorldY; float left = centerX - tileDimension/2.0f; float bottom = centerY - tileDimension/2.0f; RectWithDirection rectangle = new RectWithDirection { Left = left, Bottom = bottom, Width = tileDimension, Height = tileDimension }; collisions.Add (rectangle); } } // Sort by XAxis to speed future searches: 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; 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. if (HasCollisionAt (centerX - tileDimension, centerY)) { valueToAssign -= (int)Directions.Left; } if (HasCollisionAt (centerX + tileDimension, centerY)) { valueToAssign -= (int)Directions.Right; } if (HasCollisionAt (centerX, centerY + tileDimension)) { valueToAssign -= (int)Directions.Up; } if (HasCollisionAt (centerX, centerY - tileDimension)) { 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); } } }
private void ProcessTileProperties() { TileMapPropertyFinder finder = new TileMapPropertyFinder (currentLevel); //遍历每一个瓦片 foreach (var propertyLocation in finder.GetPropertyLocations()) { var properties = propertyLocation.Properties; if (properties.ContainsKey ("EntityType")) { float worldX = propertyLocation.WorldX; float worldY = propertyLocation.WorldY; //加上offset if (properties.ContainsKey ("YOffset")) { string yOffsetAsString = properties ["YOffset"]; float yOffset = 0; float.TryParse (yOffsetAsString, out yOffset); worldY += yOffset; } //如果这个瓦片是entity,则添加到游戏layer bool created = TryCreateEntity (properties ["EntityType"], worldX, worldY); if (created) { //如果这个瓦片是entity,且在layer中添加成功的话,在这个layer中移除这个瓦片 propertyLocation.Layer.RemoveTile (propertyLocation.TileCoordinates); } } else if (properties.ContainsKey ("RemoveMe")) { propertyLocation.Layer.RemoveTile (propertyLocation.TileCoordinates); } } //相当于玩家可以开始操作 input = new TouchScreenInput(gameplayLayer); }