public void SetupTiles() { if (Core.Scene == null) { return; } MapList = FileManager.GetMapInformation("Data/" + ConstantValues.MapDataFileName); Map.Map map = null; CharacterPlayer c = LoginManagerClient.GetCharacter(); foreach (var maps in MapList) { if (maps.MapName == c.LastMultiLocation) { map = maps; } } if (map != null) { entity = Scene.CreateEntity(map.MapName); TiledMapRenderer tmr = entity.AddComponent(new TiledMapRenderer(map.TmxMap)); tmr.Material = new Material(BlendState.NonPremultiplied); tmr.SetRenderLayer(1); tmr.SetLayersToRender(new string[] { "Tile", "Collision", "Decoration", "CustomCollision" }); TmxObjectGroup l = map.TmxMap.GetLayer <TmxObjectGroup>("Objects"); foreach (TmxObject obj in l.Objects) { ObjectSceneEntity osc = new ObjectSceneEntity(obj, tmr.TiledMap.TileWidth); osc.SetPosition(osc.Position); Scene.AddEntity(osc); } } }
private static void DrawObjectLayerColliders(Graphics g, TmxMap tmxMap, TmxObjectGroup objectLayer) { foreach (var obj in objectLayer.Objects) { if (!obj.Visible) { continue; } // Either type color or object color or unity:layer color Color objColor = objectLayer.Color; string collisionType = objectLayer.UnityLayerOverrideName; if (String.IsNullOrEmpty(collisionType)) { collisionType = obj.Type; } if (tmxMap.ObjectTypes.TmxObjectTypeMapping.ContainsKey(collisionType)) { objColor = tmxMap.ObjectTypes.TmxObjectTypeMapping[collisionType].Color; } if (objectLayer.Ignore == TmxLayerNode.IgnoreSettings.Collision) { // We're ignoring collisions but the game object is still a part of the map. DrawObjectMarker(g, tmxMap, obj, objColor); } else { DrawObjectCollider(g, tmxMap, obj, objColor); } } }
public void load(TmxMap map, bool createObjects) { this.map = map; // structural recreation structure = map.GetLayer <TmxLayer>(LAYER_STRUCTURE); features = map.GetLayer <TmxLayer>(LAYER_FEATURES); nature = map.GetObjectGroup(LAYER_NATURE); worldTileset = map.Tilesets[TILESET_WORLD]; if (NGame.context.config.enableWalls) { createWallColliders(); // comment out to disable wall collision } // analysis mapRepr = new MapRepr(); mapRepr.tmxMap = map; analyzeRooms(); mapRepr.sng = createStructuralNavigationGraph(mapRepr.roomGraph); // load entities loadFeatures(); if (createObjects) { loadNature(); } Global.log.info("loaded data from map"); }
// Pre-condition: `spawnGroup` is the object group containing spawns public Spawns(TmxObjectGroup spawnGroup) { spawnPositions = new Dictionary <Kind, List <PhysicalVector2> >(); foreach (TmxObject tmxObject in spawnGroup.Objects) { Option <Kind> kindOpt = GetKind(tmxObject.Type); kindOpt.Match( some: kind => { var spawnPosition = new PhysicalVector2( (float)tmxObject.X, (float)tmxObject.Y ); if (spawnPositions.ContainsKey(kind)) { spawnPositions[kind].Add(spawnPosition); } else { spawnPositions.Add( kind, new List <PhysicalVector2> { spawnPosition } ); } }, none: () => System.Console.WriteLine("Invalid spawn kind") ); } }
private void AddOpaqueTexture(TmxObjectGroup tmxObjectGroup) { foreach (var rect in tmxObjectGroup.Objects) { Rectangle x = new Rectangle((int)rect.X, (int)rect.Y, (int)rect.Width, (int)rect.Height); opaqueTextures.Add(new NonCollidableTexture(x, map.TileWidth, map.TileHeight)); } }
/// <summary> /// Processes the object group, and creates the entities the objects represent. /// </summary> /// <param name="rawGroup">The raw object group data.</param> /// <param name="map">The map.</param> private void ProcessObjectGroup(TmxObjectGroup rawGroup, Map map) { if (rawGroup == null) { throw new ArgumentNullException(nameof(rawGroup)); } if (map == null) { throw new ArgumentNullException(nameof(map)); } string val; int i; int visLayer = 0; int?collisionPath = null; if (rawGroup.Properties.TryGetValue("vis_layer", out val)) { if (int.TryParse(val, out i)) { visLayer = i; } } if (rawGroup.Properties.TryGetValue("collision_path", out val)) { if (int.TryParse(val, out i)) { collisionPath = i; } } foreach (var obj in rawGroup.Objects) { var centerOrigin = new Point(obj.X + (obj.Width / 2.0D), obj.Y - (obj.Height / 2.0D)); var collisionBox = new BoundingBox(-(obj.Width / 2.0), -(obj.Height / 2.0), (obj.Width / 2.0), (obj.Height / 2.0)); if (string.IsNullOrEmpty(obj.Type)) { TraceSource.TraceEvent(TraceEventType.Warning, 0, Strings.EntityBadTypeName, obj.Type, centerOrigin.X, centerOrigin.Y); } else { var ent = _entityService.CreateEntity(obj.Type, centerOrigin); if (ent != null) { var animatable = ent as Animatable; ent.VisLayer = visLayer; ent.CollisionPath = collisionPath; ent.CollisionBox = collisionBox; ent.SetProperties(obj.Properties); // load key/value pairs from map data } } } }
public void GetInterraction() { TmxObjectGroup ObjectGroup = map.ObjectGroups.SingleOrDefault(x => x.Name == "Interraction"); //Collision foreach (TmxObject item in ObjectGroup.Objects.Where(x => x.Type == "bloque")) { } }
public void SetupTiles() { if (Core.Scene == null || enabled) { return; } //enabled = true; TmxMap map = Core.Scene.Content.LoadTiledMap("Assets/map.tmx"); Entity entity = Core.Scene.CreateEntity("tiled-map"); TiledMapRenderer tmr = entity.AddComponent(new TiledMapRenderer(map, "Collision", true)); TmxLayer CustomCollisionLayer = (TmxLayer)map.GetLayer("CustomCollision"); foreach (TmxLayerTile tile in CustomCollisionLayer.Tiles) { if (tile != null && tile.TilesetTile != null) { TmxList <TmxObjectGroup> objgl = tile.TilesetTile.ObjectGroups; if (objgl != null && objgl.Count > 0) { TmxObjectGroup objg = objgl[0]; if (objg.Objects != null && objg.Objects.Count > 0) { TmxObject obj = objg.Objects[0]; TmxObjectType type = obj.ObjectType; if (type == TmxObjectType.Ellipse) { //Draw Ellipse as collision Core.Scene.CreateEntity(obj.Name, new Vector2(tile.Position.X * tile.Tileset.TileWidth + obj.Width / 2, tile.Position.Y * tile.Tileset.TileHeight + obj.Height / 2)) .AddComponent(new FSCollisionEllipse(obj.Width / 2, obj.Height / 2)) .AddComponent(new CircleCollider((obj.Width + obj.Height) / 4)); // have to get an average of sides, hence / 4 } else if (type == TmxObjectType.Polygon) { Vector2[] points = obj.Points; Core.Scene.CreateEntity(obj.Name, new Vector2(tile.Tileset.TileWidth * tile.Position.X + obj.X, tile.Tileset.TileHeight * tile.Position.Y + obj.Y)) .AddComponent(new FSCollisionPolygon(points)) .AddComponent(new PolygonCollider(points)); } //basic is rectangle else if (type == TmxObjectType.Basic) { Core.Scene.CreateEntity(obj.Name, new Vector2(tile.Position.X * tile.Tileset.TileWidth + obj.Width / 2, tile.Position.Y * tile.Tileset.TileHeight + obj.Height / 2)) .AddComponent(new FSCollisionBox(obj.Width, obj.Height)) .AddComponent(new BoxCollider(obj.Width, obj.Height)); } } } } } tmr.SetLayersToRender(new string[] { }); }
private void SetSpawnPoint() { TmxObjectGroup objectLayer = TileMap.GetLayer <TmxObjectGroup>("Objects"); foreach (TmxObject obj in objectLayer.Objects) { if (obj.ObjectType == TmxObjectType.Point && obj.Name.Equals("Spawn")) { SpawnPoint = new Vector2(obj.X, obj.Y); } } }
public static void LeerTmx(TmxObjectGroup objectGroup, Entity defaultCollidable) { foreach (var obj in objectGroup.Objects) { var properties = obj.Properties; if (properties != null) { CrearSolidos(obj, properties, defaultCollidable); } } }
public override void OnAddedToScene() { base.OnAddedToScene(); Karta = Scene.Content.LoadTiledMap(TMXKartaPlats); TiledMapRenderer = AddComponent(new TiledMapRenderer(Karta, KollitionsLager)); TiledMapRenderer.SetLayersToRender(new[] { "Mark" }); //här är huur man får fram object gruper från tiléd ObjGrupp = Karta.GetObjectGroup("SpelarSpawn"); SpawnSpelareEtt = ObjGrupp.Objects["spelare_ett"]; SpawnSpelareTvå = ObjGrupp.Objects["spelare_två"]; // render below/behind everything else. higher number = further back TiledMapRenderer.SetRenderLayer(2); KamraGränser(); }
private static void DrawTilesInObjectGroup(Graphics g, TmxMap tmxMap, TmxObjectGroup objectGroup) { // Get opacity in eventually #if !TILED2UNITY_MAC ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.Matrix33 = objectGroup.Opacity; ImageAttributes imageAttributes = new ImageAttributes(); imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); #endif foreach (var tmxObject in objectGroup.Objects) { if (!tmxObject.Visible) { continue; } TmxObjectTile tmxObjectTile = tmxObject as TmxObjectTile; if (tmxObjectTile == null) { continue; } GraphicsState state = g.Save(); PointF xfPosition = TmxMath.ObjectPointFToMapSpace(tmxMap, tmxObject.Position); g.TranslateTransform(xfPosition.X, xfPosition.Y); g.RotateTransform(tmxObject.Rotation); { GraphicsState tileState = g.Save(); PrepareTransformForTileObject(g, tmxMap, tmxObjectTile); // Draw the tile Rectangle destination = new Rectangle(0, -tmxObjectTile.Tile.TileSize.Height, tmxObjectTile.Tile.TileSize.Width, tmxObjectTile.Tile.TileSize.Height); Rectangle source = new Rectangle(tmxObjectTile.Tile.LocationOnSource, tmxObjectTile.Tile.TileSize); //g.DrawRectangle(Pens.Black, destination); #if !TILED2UNITY_MAC g.DrawImage(tmxObjectTile.Tile.TmxImage.ImageBitmap, destination, source.X, source.Y, source.Width, source.Height, GraphicsUnit.Pixel, imageAttributes); #else g.DrawImage(tmxObjectTile.Tile.TmxImage.ImageBitmap, destination, source.X, source.Y, source.Width, source.Height, GraphicsUnit.Pixel); #endif g.Restore(tileState); } g.Restore(state); } }
/// <summary> /// Initializes a new instance of the <see cref="TiledMapObjectLayer" /> class. /// </summary> /// <param name="tmxObjectLayer">The TMX parsed object layer</param> public TiledMapObjectLayer(TmxObjectGroup tmxObjectLayer) { this.ObjectLayerName = tmxObjectLayer.Name; this.Color = new Color(tmxObjectLayer.Color.R, tmxObjectLayer.Color.G, tmxObjectLayer.Color.B); this.Opacity = tmxObjectLayer.Opacity; this.Visible = tmxObjectLayer.Visible; this.Objects = new List <TiledMapObject>(); this.Properties = new Dictionary <string, string>(tmxObjectLayer.Properties); foreach (var tmxObject in tmxObjectLayer.Objects) { var tiledMapObject = new TiledMapObject(tmxObject); this.Objects.Add(tiledMapObject); } }
private void SetupCustomCollision() { TmxLayer CustomCollisionLayer = (TmxLayer)TileMap.GetLayer("CustomCollision"); foreach (TmxLayerTile tile in CustomCollisionLayer.Tiles) { if (tile != null && tile.TilesetTile != null) { TmxList <TmxObjectGroup> objgl = tile.TilesetTile.ObjectGroups; if (objgl != null && objgl.Count > 0) { TmxObjectGroup objg = objgl[0]; if (objg.Objects != null && objg.Objects.Count > 0) { TmxObject obj = objg.Objects[0]; TmxObjectType type = obj.ObjectType; if (type == TmxObjectType.Ellipse) { //Draw Ellipse as collision Entity.Scene.CreateEntity(obj.Name, new Vector2(Entity.Position.X + tile.Position.X * tile.Tileset.TileWidth + obj.Width / 2, Entity.Position.Y + tile.Position.Y * tile.Tileset.TileHeight + obj.Height / 2)) .AddComponent(new FSCollisionEllipse(obj.Width / 2, obj.Height / 2)) .AddComponent(new CircleCollider((obj.Width + obj.Height) / 4)); // have to get an average of sides, hence / 4 } else if (type == TmxObjectType.Polygon) { Vector2[] points = obj.Points; Entity.Scene.CreateEntity(obj.Name, new Vector2(Entity.Position.X + tile.Tileset.TileWidth * tile.Position.X + obj.X, Entity.Position.Y + tile.Tileset.TileHeight * tile.Position.Y + obj.Y)) .AddComponent(new FSCollisionPolygon(points)) .AddComponent(new PolygonCollider(points)); } //basic is rectangle else if (type == TmxObjectType.Basic) { Entity.Scene.CreateEntity(obj.Name, new Vector2(Entity.Position.X + tile.Position.X * tile.Tileset.TileWidth + obj.Width / 2, Entity.Position.Y + tile.Position.Y * tile.Tileset.TileHeight + obj.Height / 2)) .AddComponent(new FSCollisionBox(obj.Width, obj.Height)) .AddComponent(new BoxCollider(obj.Width, obj.Height)); Entity.AddComponent(new FSCollisionBox(obj.Width, obj.Height)).AddComponent(new BoxCollider(obj.Width, obj.Height)); } } } } } }
public void LoadObjects() { TmxObjectGroup objectGroup = map.ObjectGroups[0]; Texture2D tex; for (int i = 0; i < objectGroup.Objects.Count; i++) { tex = Game.Content.Load <Texture2D>(objectGroup.Objects[i].Properties["path"]); Backgroundobs.Add(new Obstacle(new Vector2((float)objectGroup.Objects[i].X, (float)objectGroup.Objects[i].Y - tex.Bounds.Height), tex)); } objectGroup = map.ObjectGroups[1]; for (int i = 0; i < objectGroup.Objects.Count; i++) { tex = Game.Content.Load <Texture2D>("Devices/controlPanel"); controlPanel = new ControlPanel(this.Game, new Vector2((float)objectGroup.Objects[i].X, (float)objectGroup.Objects[i].Y), tex); } controlPanel.LoadContent(); }
public override void OnAddedToScene() { base.OnAddedToScene(); tileMap = Core.Content.LoadTiledMap(System.IO.Path.Combine( Core.Content.RootDirectory, "Levels/TileMap1.tmx")); tileMapRenderer = AddComponent(new TiledMapRenderer(tileMap)); tileMapRenderer.SetLayersToRender(new string[] { "Foreground", "Background" }); tileMapRenderer.PhysicsLayer = (int)Physics.PhysicsLayer.Collidable; tileMapRenderer.CollisionLayer = tileMap.GetLayer <TmxLayer>("Collision"); tileMapRenderer.AddColliders(); tileMapRenderer.LayerDepth = 1; TmxObjectGroup objectsLayer = tileMap.GetObjectGroup("Objects"); objectsLayer.Visible = false; foreach (TmxObject obj in objectsLayer.Objects) { switch (obj.Type) { case AvatarObjectType: Avatar avatar = Scene.AddEntity(new Avatar(obj.Position() + TiledObjectOffset)); Scene.Camera.AddComponent(new FollowCamera(avatar, FollowCamera.CameraStyle.LockOn)); break; case SlimeObjectType: Slime slime = Scene.AddEntity( new Slime( obj.Position() + TiledObjectOffset, obj.FullName())); break; } } }
// Pre-condition: `teleportGroup` is the object group containing teleports public Teleports(TmxObjectGroup teleportGroup) { foreach (TmxObject tmxObject in teleportGroup.Objects) { switch (tmxObject.Type) { case "TriforceDestination": var position = new PhysicalVector2( (float)tmxObject.X, (float)tmxObject.Y ); triforceDestination = position; break; default: System.Console.WriteLine("Invalid spawn kind"); break; } } }
private static void DrawTilesInObjectGroup(SKCanvas canvas, TmxMap tmxMap, TmxObjectGroup objectGroup) { using (SKPaint paint = new SKPaint()) { // Draw with the given opacity paint.Color = SKColors.White.WithAlpha((byte)(objectGroup.Opacity * byte.MaxValue)); foreach (var tmxObject in objectGroup.Objects) { if (!tmxObject.Visible) { continue; } TmxObjectTile tmxObjectTile = tmxObject as TmxObjectTile; if (tmxObjectTile == null) { continue; } using (new SKAutoCanvasRestore(canvas)) { PointF xfPosition = TmxMath.ObjectPointFToMapSpace(tmxMap, tmxObject.Position); canvas.Translate(xfPosition.X, xfPosition.Y); canvas.RotateDegrees(tmxObject.Rotation); using (new SKAutoCanvasRestore(canvas)) { PrepareTransformForTileObject(canvas, tmxMap, tmxObjectTile); // Draw the tile SKRect destination = new RectangleF(0, -tmxObjectTile.Tile.TileSize.Height, tmxObjectTile.Tile.TileSize.Width, tmxObjectTile.Tile.TileSize.Height).ToSKRect(); SKRect source = new RectangleF(tmxObjectTile.Tile.LocationOnSource, tmxObjectTile.Tile.TileSize).ToSKRect(); canvas.DrawBitmap(tmxObjectTile.Tile.TmxImage.ImageBitmap, source, destination, paint); } } } } }
public static Level ParseLevel(int levelNo) { //Get Map var levelStr = levelNo.ToString().PadLeft(5, '0'); var map = new TmxMap($"{Application.streamingAssetsPath}/Levels/level_{levelStr}.tmx"); //Get Level Info From Map var mapProperties = map.Properties.ToArray(); var width = map.Width; var height = map.Height; var itemLayer = map.TileLayers.FirstOrDefault(layer => layer.Name == ItemLayer); var cellItemLayer = map.TileLayers.FirstOrDefault(layer => layer.Name == CellItemLayer); var groupLayers = new TmxObjectGroup[10]; var groups = new Group[10]; foreach (var temp in map.ObjectGroups) { if (!GroupNames.ContainsKey(temp.Name)) { continue; } groupLayers[GroupNames[temp.Name]] = temp; } //goals var goals = new Dictionary <GoalType, int>(4); var goalCount = 0; foreach (var pair in mapProperties) { if (goalCount >= 4) { break; } if (!Enum.TryParse(pair.Key, true, out GoalType goalType)) { continue; } if (!int.TryParse(pair.Value, out var goalAmount)) { continue; } goals.Add(goalType, goalAmount); goalCount++; } if (goalCount == 0) { throw new ApplicationException("No goal in level"); } //move count var moveCountPair = mapProperties.FirstOrDefault(p => p.Key == MoveCount); if (!int.TryParse(moveCountPair.Value, out var moveCount)) { throw new ApplicationException("No move count"); } //items if (itemLayer == null) { throw new ApplicationException("Item layer is null"); } var itemList = itemLayer.Tiles.Select(i => FakeItemIds.Contains(i.Gid - 1) ? ItemTypeInBoard.Empty : Enumeration.FromValue <ItemTypeInBoard>(i.Gid - 1)) .ToArray(); var itemGrid = itemList.ToArray().Make2DArray(height, width); //cell items CellItemTypeInBoard[,] cellItemGrid; if (cellItemLayer == null) { cellItemGrid = new CellItemTypeInBoard[height, width].Populate(CellItemTypeInBoard.Empty); } else { cellItemGrid = cellItemLayer.Tiles.Select(i => Enumeration.FromValue <CellItemTypeInBoard>(i.Gid - 1)) .ToArray().Make2DArray(height, width); } //groups if (groupLayers.All(g => g == null)) { var items = new List <ItemTypeInBoard> { ItemTypeInBoard.BlueCube, ItemTypeInBoard.GreenCube, ItemTypeInBoard.RedCube, ItemTypeInBoard.YellowCube, }; groups[0] = new Group { Items = items, UseForDrop = true }; Debug.LogError("Could not find any Group. Created Temp group for drop"); } else { for (var i = 0; i < groupLayers.Length; i++) { if (groupLayers[i] == null) { continue; } //group items if (groupLayers[i].Objects.Count <= 0) { throw new ApplicationException( $"There were no items in {groupLayers[i].Name}. Added temp items to group"); } var items = groupLayers[i].Objects.ToList() .Select(o => Enumeration.FromValue <ItemTypeInBoard>(o.Tile.Gid - 1)).ToList(); //use for drop bool useForDrop; if (!groupLayers[i].Properties.ContainsKey(UseForDrop)) { useForDrop = false; } else { bool.TryParse(groupLayers[i].Properties[UseForDrop], out useForDrop); } groups[i] = new Group { Items = items, UseForDrop = useForDrop, }; } if (groups.All(g => g == null || !g.UseForDrop)) { throw new ApplicationException($"Could not find any group for drop. Please add it"); } } var level = new Level(width, height, itemGrid, cellItemGrid, goals, moveCount, groups); return(level); }
public void GenerateObjects(TmxObjectGroupList objectGroupList) { int objectGroupCount = objectGroupList.Count; for (int i = 0; i < objectGroupCount; i += 1) { TmxObjectGroup objectGroup = objectGroupList[i]; int objectCount = objectGroup.Objects.Count; PropertyDict properties = objectGroup.Properties; Transform layerContainer = new GameObject(objectGroup.Name).transform; layerContainer.rotation = Quaternion.identity; layerContainer.transform.SetParent(objectContainer); for (int j = 0; j < objectCount; j += 1) { if (!properties.ContainsKey("Type")) { Debug.Log("ERROR: Object Layer \"" + objectGroup.Name + "\" doesn't have a Type property!"); continue; } string objectType = properties["Type"]; TmxObjectGroup.TmxObject tmxObject = objectGroup.Objects[j]; int xpos = (int)tmxObject.X / tile_width; int ypos = (int)tmxObject.Y / tile_height - 1; Vector3 worldPos = new Vector3(-xpos + 0.5f, 0.5f, ypos - 0.5f); GameObject spawnedObject = (GameObject)GameObject.Instantiate(Resources.Load("Objects/" + objectType), worldPos, Quaternion.identity); spawnedObject.name = tmxObject.ToString(); spawnedObject.transform.position = worldPos; spawnedObject.transform.parent = layerContainer; if (objectType == "Hero") { hero = spawnedObject.GetComponent <Hero>(); hero.Init(tileMap, xpos, ypos); } if (objectType == "End") { endX = xpos; endY = ypos; } /*TmxObjectGroup.TmxObject startEnd = ObjectGroups[i].Objects[j]; * * int startEndX = (int)startEnd.X / tile_width; * int startEndY = (int)startEnd.Y / tile_height; * Vector3 worldPos = new Vector3(-startEndX + 0.5f, 1f, startEndY - 1.5f); * * if (startEnd.Name == "Start") * { * //player.Spawn(worldPos, tile_width, tile_height); * GameObject startObject = (GameObject)GameObject.Instantiate(Resources.Load("SpawnPoint"), worldPos, Quaternion.identity); * } * else if (startEnd.Name == "End") * { * GameObject endObjectPrefab = (GameObject)GameObject.Instantiate(Resources.Load("LevelEndTrigger")); * endObjectPrefab.transform.position = worldPos; * }*/ } } }
public Level(string mapFile, GraphicsDeviceManager graphicsManager) { this.isComplete = false; this.bunnydead = false; this.graphics = graphicsManager; this.map = new TmxMap(mapFile); this.Bunny = new Bunny( Single.Parse(map.Properties["bunnySpawnX"]), Single.Parse(map.Properties["bunnySpawnY"])); this.Clyde = new Clyde( Single.Parse(map.Properties["clydeSpawnX"]), Single.Parse(map.Properties["clydeSpawnY"])); this.inventory = new Inventory(0, 0, 55, 55); this.worldSprites = new List <Sprite>(); this.platforms = new List <Sprite>(); this.items = new List <Item>(); this.sounds = new List <SoundEffect>(); this.keys = new List <Item>(); this.ramps = new List <Item>(); this.doors = new List <Item>(); this.buttons = new List <Item>(); this.waters = new List <Item>(); this.goal = new List <Item>(); this.gates = new List <Item>(); this.physics = new Physics(); this.physics.Add(this.Bunny); this.physics.Add(this.Clyde); this.collisions = new CollisionManager(platforms, items, new List <Sprite>()); this.collisions.addMoving(this.Clyde); this.collisions.addMoving(this.Bunny); this.mapObjectsDrawable = map.ObjectGroups["drawable"]; this.mapObjectsNonDrawable = map.ObjectGroups["nondrawable"]; // draw the nondrawable objects as Regions foreach (TmxObjectGroup.TmxObject o in mapObjectsNonDrawable.Objects) { Console.WriteLine("X:" + o.X); Console.WriteLine("Y:" + o.Y); Console.WriteLine("Width:" + o.Width); Console.WriteLine("Height:" + o.Height); Region currentRegion = new Region(o.X, o.Y, o.Width, o.Height); //Sprite currentRegion = new Sprite("ground", o.X, o.Y, o.Width, o.Height); this.worldSprites.Add(currentRegion); this.platforms.Add(currentRegion); } // draw the drawable objects foreach (TmxObjectGroup.TmxObject o in mapObjectsDrawable.Objects) { Item currentObject; Key whiteKey = new Key(Color.AliceBlue, this, 0, 0, 0, 0); if (o.Properties["type"] == "water") { currentObject = new Water(o.X, o.Y, o.Width, o.Height, this); this.waters.Add(currentObject); } else if (o.Properties["type"] == "key") { System.Drawing.Color drawColor = System.Drawing.Color.FromName(o.Properties["color"]); Color c = new Color(drawColor.R, drawColor.G, drawColor.B, drawColor.A); whiteKey = new Key(c, this, o.X, o.Y, o.Width, o.Height); currentObject = whiteKey; this.keys.Add(currentObject); } else if (o.Properties["type"] == "goal_door") { System.Drawing.Color drawColor = System.Drawing.Color.FromName(o.Properties["color"]); Color c = new Color(drawColor.R, drawColor.G, drawColor.B, drawColor.A); currentObject = new Goal(this, c, o.X, o.Y, o.Width, o.Height); this.goal.Add(currentObject); } else if (o.Properties["type"] == "switch_button") { System.Drawing.Color drawColor = System.Drawing.Color.FromName(o.Properties["color"]); Color c = new Color(drawColor.R, drawColor.G, drawColor.B, drawColor.A); currentObject = new Switch(c, this, o.X, o.Y, o.Width, o.Height, false); this.buttons.Add(currentObject); } else if (o.Properties["type"] == "reverse_switch") { System.Drawing.Color drawColor = System.Drawing.Color.FromName(o.Properties["color"]); Color c = new Color(drawColor.R, drawColor.G, drawColor.B, drawColor.A); currentObject = new Switch(c, this, o.X, o.Y, o.Width, o.Height, true); this.buttons.Add(currentObject); } else if (o.Properties["type"] == "switch_gate") { System.Drawing.Color drawColor = System.Drawing.Color.FromName(o.Properties["color"]); Color c = new Color(drawColor.R, drawColor.G, drawColor.B, drawColor.A); currentObject = new Gate(o.Properties["imageName"], c, o.X, o.Y, o.Width, o.Height); this.platforms.Add(currentObject); this.gates.Add(currentObject); } else if (o.Properties["type"] == "door") { System.Drawing.Color drawColor = System.Drawing.Color.FromName(o.Properties["color"]); Color c = new Color(drawColor.R, drawColor.G, drawColor.B, drawColor.A); currentObject = new Door(this, c, o.X, o.Y, o.Width, o.Height); this.platforms.Add(currentObject); this.doors.Add(currentObject); } else if (o.Properties["type"] == "ramp") { Ramp r = new Ramp(o.Properties["imageName"], o.X, o.Y, o.Width, o.Height, this); currentObject = r; this.items.Add(r.leftPushBox); this.worldSprites.Add(r.leftPushBox); this.items.Add(r.rightPushBox); this.worldSprites.Add(r.rightPushBox); this.physics.Add(r); this.platforms.Add(currentObject); this.ramps.Add(currentObject); this.collisions.addMoving(currentObject); } else if (o.Properties["type"] == "cloud") { currentObject = new Cloud(o.Properties["imageName"], o.X, o.Y, o.Width, o.Height); } else { //this shouldn't happen currentObject = new Water(o.X, o.Y, o.Width, o.Height, this); Console.WriteLine(o.Properties["imageName"]); } this.worldSprites.Add(currentObject); this.items.Add(currentObject); } this.PlayerIcon = new PlayerIcon("BunnyCurrentsmall", "ClydeCurrentsmall"); this.background = new Sprite(map.Properties["backgroundImage"], 0, 0, GameGlobals.WINDOW_WIDTH, GameGlobals.WINDOW_HEIGHT); this.worldSprites.Add(this.Bunny); this.worldSprites.Add(this.Clyde); this.worldSprites.Add(this.Clyde.back); this.items.Add(this.Clyde.back); this.platforms.Add(this.Clyde); this.inputManager = new InputManager(worldSprites, this.Bunny, this.Clyde, platforms, sounds, this); this.worldSprites.Add(inventory); this.imshow3 = new imageshow("win", 0, 0, 0, (map.TileHeight * map.Height)); this.worldSprites.Add(imshow3); this.imshow2 = new imageshow("bunnydies", (map.TileWidth * map.Width) / 3, (map.TileHeight * map.Height) / 4, 0, (map.TileHeight * map.Height) / 2); this.worldSprites.Add(imshow2); this.Bunny.die = imshow2; this.Bunny.mapwidth = (map.TileWidth * map.Width) / 3; String logoImage = "mainlogo"; String resetImage = "reset_keyboard.png"; if (GamePad.GetState(PlayerIndex.One).IsConnected) { logoImage = "mainlogo_controller"; resetImage = "reset_controller"; } this.imshow = new imageshow(logoImage, (map.TileWidth * map.Width) / 4, (map.TileHeight * map.Height) / 4, 0, (map.TileHeight * map.Height) / 2); this.imshoww = (map.TileWidth * map.Width) / 2; this.worldSprites.Add(imshow); }
public void GenerateTiles(TmxMap map) { if (baseLayerName != "") { TmxLayer baseLayer = map.Layers [baseLayerName]; foreach (var tile in baseLayer.Tiles) { int val = tile.Gid - 1; if (val != -1) { float xpos = tile.X * 0.32f; float ypos = tile.Y * -0.32f; GameObject go = Instantiate(spriteBase); go.transform.position = new Vector3(xpos, ypos, go.transform.position.z); go.GetComponent <SpriteRenderer> ().sprite = allSprites [val]; go.transform.parent = parentTransform; } } } if (optionalLayerName != "") { TmxLayer optionalLayer = map.Layers [optionalLayerName]; //parentTransform.gameObject.AddComponent<RandomCreation> (); RandomCreation rCreate = parentTransform.gameObject.GetComponent <RandomCreation> (); rCreate.randomEntities = new List <GameObject> (); rCreate.randChances = new List <float> (); foreach (var tile in optionalLayer.Tiles) { int val = tile.Gid - 1; if (val != -1) { float xpos = tile.X * 0.32f; float ypos = tile.Y * -0.32f; GameObject go = Instantiate(optionalBase); go.transform.position = new Vector3(xpos, ypos, go.transform.position.z); go.GetComponent <SpriteRenderer> ().sprite = allSprites [val]; go.transform.parent = parentTransform; rCreate.randomEntities.Add(go); rCreate.randChances.Add(0.5f); } } } if (colliderLayerName != "") { TmxObjectGroup colliderGroup = map.ObjectGroups [colliderLayerName]; foreach (var item in colliderGroup.Objects) { // instantiate the collider GameObject go = Instantiate(colliderBase); BoxCollider2D collider = go.GetComponent <BoxCollider2D> (); // extract the information float colWidth = (float)item.Width * 0.01f; float colHeight = (float)item.Height * 0.01f; float xPos = (float)item.X * 0.01f + (colWidth / 2); float yPos = (float)item.Y * -0.01f - (colHeight / 2); // position the collider collider.size = new Vector2(colWidth, colHeight); go.transform.position = new Vector3(xPos, yPos, go.transform.position.z); go.transform.parent = parentTransform; } } if (enemyLayerName != "") { NetEnemySpawner enemySpawner = parentTransform.gameObject.GetComponent <NetEnemySpawner> (); if (enemySpawner == null) { parentTransform.gameObject.AddComponent <NetEnemySpawner> (); enemySpawner = parentTransform.gameObject.GetComponent <NetEnemySpawner> (); } enemySpawner.thingsToSpawn = new List <GameObject> (); enemySpawner.spawnPositions = new List <Vector3> (); TmxObjectGroup enemyGroup = map.ObjectGroups [enemyLayerName]; foreach (var enemy in enemyGroup.Objects) { // instantiate the enemy GameObject go = null; if (enemy.Name == "FireDino") { go = fireDino; //go = Instantiate (fireDino); } else if (enemy.Name == "Slime") { go = squishSlime; } if (go == null) { continue; } // extract the information float xPos = (float)enemy.X * 0.01f + (0.16f); float yPos = (float)enemy.Y * -0.01f - (0.16f); // position the enemy Vector3 pos = new Vector3(xPos, yPos, go.transform.position.z); // go.transform.parent = parentTransform; enemySpawner.thingsToSpawn.Add(go); enemySpawner.spawnPositions.Add(pos); } } if (doorLayerName != "") { TmxObjectGroup doorGroup = map.ObjectGroups [doorLayerName]; foreach (var door in doorGroup.Objects) { GameObject go = null; go = Instantiate(doorPrefab); BoxCollider2D collider = go.GetComponent <BoxCollider2D> (); float colWidth = (float)door.Width * 0.01f; float colHeight = (float)door.Height * 0.01f; float xPos = (float)door.X * 0.01f + (colWidth / 2); float yPos = (float)door.Y * -0.01f - (colHeight / 2); collider.size = new Vector2(colWidth, colHeight); go.transform.position = new Vector3(xPos, yPos, go.transform.position.z); go.transform.parent = parentTransform; CoinGateway coinGateway = go.GetComponent <CoinGateway> (); coinGateway.deltaX = float.Parse(door.Properties ["deltaX"]); coinGateway.deltaY = float.Parse(door.Properties ["deltaY"]); coinGateway.coinRequirement = int.Parse(door.Properties ["Coin Requirement"]); } } }
public bool Parse(XmlDocument doc) { if (doc == null) { return(false); } XmlNode root_node = doc.DocumentElement; ///1.properties if (root_node.Attributes.GetNamedItem("version") != null) { version = root_node.Attributes.GetNamedItem("version").Value; } if (root_node.Attributes.GetNamedItem("orientation") != null) { orientation = root_node.Attributes.GetNamedItem("orientation").Value; } if (root_node.Attributes.GetNamedItem("renderorder") != null) { renderorder = root_node.Attributes.GetNamedItem("renderorder").Value; } if (root_node.Attributes.GetNamedItem("height") != null) { tile_rows = System.Convert.ToInt32(root_node.Attributes.GetNamedItem("height").Value); } if (root_node.Attributes.GetNamedItem("width") != null) { tile_cols = System.Convert.ToInt32(root_node.Attributes.GetNamedItem("width").Value); } if (root_node.Attributes.GetNamedItem("tilewidth") != null) { tile_width = System.Convert.ToSingle(root_node.Attributes.GetNamedItem("tilewidth").Value); } if (root_node.Attributes.GetNamedItem("tileheight") != null) { tile_height = System.Convert.ToSingle(root_node.Attributes.GetNamedItem("tileheight").Value); } if (root_node.Attributes.GetNamedItem("nextobjectid") != null) { nextobjectid = System.Convert.ToInt32(root_node.Attributes.GetNamedItem("nextobjectid").Value); } ///2.properties XmlNodeList node_list = root_node.SelectNodes("properties/property"); foreach (XmlNode obj in node_list) { string _name = obj.Attributes.GetNamedItem("name").Value; string _value = obj.Attributes.GetNamedItem("value").Value; DicProperty.Add(_name, _value); } ///3.tileset node_list = root_node.SelectNodes("tileset"); foreach (XmlNode obj in node_list) { TmxTileSet tile_set = new TmxTileSet(); tile_set.Parse(obj); if (DicTileSet.ContainsKey(tile_set.firstGID)) { Log.Error("TmxMap::Parse the same tileset is exist:" + tile_set.firstGID); } else { DicTileSet.Add(tile_set.firstGID, tile_set); } } ///4.layer node_list = root_node.SelectNodes("layer"); foreach (XmlNode obj in node_list) { TmxLayer layer = new TmxLayer(); layer.Parse(obj); if (DicLayer.ContainsKey(layer.name)) { Log.Error("TmxMap::Parse the same layer is exist:" + layer.name); } else { DicLayer.Add(layer.name, layer); } } ///5.objectgroup node_list = root_node.SelectNodes("objectgroup"); foreach (XmlNode obj in node_list) { TmxObjectGroup objectgroup = new TmxObjectGroup(); objectgroup.Parse(obj); if (DicObjectGroup.ContainsKey(objectgroup.name)) { Log.Error("TmxMap::Parse the same objectgroup is exist:" + objectgroup.name); } else { DicObjectGroup.Add(objectgroup.name, objectgroup); } } return(true); }
public void VisitObjectLayer(TmxObjectGroup objectLayer) { objectLayer.DrawOrderIndex = drawOrderIndex; objectLayer.DepthBufferIndex = ((objectLayer.ParentNode == null) ? depthBufferIndex++ : 0); }