/// <summary>引数のbehaviourが衝突する属性を持つか調べる</summary> private static MapPhysicsAttribute getCollidedAttribute(GameObject aBehaviour, out MapPhysics.CollisionType oCollisionType) { if (aBehaviour == mCollider.gameObject) //自分の属性とは衝突しない { oCollisionType = MapPhysics.CollisionType.pass; return(null); } MapPhysicsAttribute tCollidedAttribute = null; oCollisionType = MapPhysics.CollisionType.pass; foreach (MapPhysicsAttribute tAttribute in aBehaviour.GetComponents <MapPhysicsAttribute>()) { MapPhysics.CollisionType tCollisionType = MapPhysics.canCollide(mAttribute, tAttribute); string t = aBehaviour.transform.parent.name; if (tCollisionType == MapPhysics.CollisionType.collide) { tCollidedAttribute = tAttribute; oCollisionType = MapPhysics.CollisionType.collide; break; } if (tCollisionType == MapPhysics.CollisionType.stop) { tCollidedAttribute = tAttribute; oCollisionType = MapPhysics.CollisionType.stop; } } return(tCollidedAttribute); }
public override void Load(IContentLoader loader) { _loader = loader; var defaultFont = loader.Load <SpriteFont>(ContentPaths.FONT_UI_GENERAL); varShowLootTip = new GameVariable <bool>(); varShowLootTip.Load(loader); GameObjects.Add(varShowLootTip); // Player player = new Player(eventBus, varShowLootTip); player.Load(loader); GameObjects.Add(player); // Bot bot = new Bot(eventBus, Camera); bot.Load(loader); GameObjects.Add(bot); // Message manager textPopupManager = new TextPopupManager(ContentPaths.FONT_UI_GENERAL, StackingMethod.Parallel); textPopupManager.Load(loader); GameObjects.Add(textPopupManager); // Physics phys = new MapPhysics(chunkMap, physSettings); GameObjects.Add(chunkMap); // Camera GameObjects.Add(Camera); // Services setupKeyListeners(); // Event listeners eventBusSubscriptionIds.AddRange(new[] { eventBus.Subscribe(Events.PlayerPickedUpCoin, (p) => Debug.WriteLine("picked up coin!")), eventBus.Subscribe(Events.PlayerDied, (p) => playerDied()), eventBus.Subscribe(Events.CreateProjectile, (p) => playerCreatedProjectile(p as Projectile)), }); // Game variables var tipLootBoxOpen = "Press <E> to open loot box"; txtDisplayTipLootBoxOpen = new TextElement <string>(tipLootBoxOpen, defaultFont, new TextElementSettings()) { Position = new Vector2(10, 10), IsAttachedToCamera = true, IsHidden = true, }; txtDisplayTipLootBoxOpen.Load(loader); GameObjects.Add(txtDisplayTipLootBoxOpen); // Map loadMap(); }
public override void Load(IContentLoader loader) { _loader = loader; // Player player = new Player(eventBus, Camera); player.Load(loader); GameObjects.Add(player); // Game variables Jumps = new GameVariable <int>(); Jumps.Load(loader); GameObjects.Add(Jumps); CoinsCount = new GameVariable <int>(); CoinsCount.Load(loader); GameObjects.Add(CoinsCount); // Message manager textPopupManager = new TextPopupManager(ContentPaths.FONT_UI_GENERAL, StackingMethod.Parallel); textPopupManager.Load(loader); GameObjects.Add(textPopupManager); // HUD hud = new HUD(this); hud.ScreenBounds = base.Game.GraphicsDevice.Viewport.Bounds; hud.DrawOrder = DrawOrderPosition.HUD; hud.Load(loader); GameObjects.Add(hud); // Physics phys = new MapPhysics(chunkMap, physSettings); GameObjects.Add(chunkMap); // Camera GameObjects.Add(Camera); // Services setupKeyListeners(); // Event listeners eventBusSubscriptionIds.AddRange(new[] { eventBus.Subscribe(Events.PlayerPickedUpCoin, (p) => CoinsCount.Value += 1), eventBus.Subscribe(Events.PlayerJumped, (p) => Jumps.Value += 1), eventBus.Subscribe(Events.PlayerEnteredDoor, (p) => playerTouchedDoor()), eventBus.Subscribe(Events.PlayerDied, (p) => playerDied()), eventBus.Subscribe(Events.CreateProjectile, (p) => playerCreatedProjectile(p as Projectile)), }); // Load first map loadMap(ContentPaths.PATH_MAP_1); startGameTimer = new MillisecCounter(1500); Camera.SetFocus(playerExit, immediateFocus: true); isPlayerFocused = false; }
void loadMap() { // reset game objects GameObjects.RemoveAll(o => o is LootBox || o is Wall); // setup chunks chunkMap.Reset(chunkWidth: 100, chunkHeight: 100, mapWidth: 1000, mapHeight: 1000); // physics reset phys = new MapPhysics(chunkMap, physSettings); // read map elements var doc = SvgDocument.Open(ContentPaths.PATH_MAP_1); var rects = doc.Children.FindSvgElementsOf <SvgRectangle>(); foreach (var rect in rects) { if (rect.Fill.ToString() == "#000001") { playerStart = new Vector2(rect.X, rect.Y); } else if (rect.Fill.ToString() == "#000002") { playerExit = new Vector2(rect.X, rect.Y); } else if (rect.Fill.ToString() == "#000010") { var lootBox = new LootBox(); lootBox.Props.Position = new Vector2(rect.X, rect.Y); lootBox.Load(_loader); GameObjects.Add(lootBox); chunkMap.UpdateEntity(lootBox); } else { // create wall var fill = getRgba(rect.Fill.ToString()); var wall = new Wall(new RectangleF(rect.X, rect.Y, rect.Width, rect.Height), fill); wall.Load(_loader); GameObjects.Add(wall); chunkMap.UpdateEntity(wall); } } // Verify that the level has a beginning and an end. //if (playerStart == Vector2.Zero) // throw new NotSupportedException("A level must have a starting point."); if (playerExit == Vector2.Zero) { throw new NotSupportedException("A level must have an exit."); } // Set player position for new map player.Props.Position = playerStart; bot.Props.Position = new Vector2(10, 10); chunkMap.UpdateEntity(player); chunkMap.UpdateEntity(bot); addCoinToLevel(new Vector2(200, 200)); Camera.SetFocus(player, immediateFocus: true); }
void loadMap(string mapPath) { currentMapPath = mapPath; // Variables Jumps.Value = 0; CoinsCount.Value = 0; // Reset game objects GameObjects.RemoveAll(o => o is Coin || o is Platform || o is Door); // Create map tiles var doc = SvgDocument.Open(mapPath); var elements = doc.Children.FindSvgElementsOf <SvgPath>(); // get images var patterns = doc.Children.FindSvgElementsOf <SvgPatternServer>(); var images = new Dictionary <string, Texture2D>(); foreach (var pattern in patterns) { var image = pattern.Children.FindSvgElementsOf <SvgImage>().FirstOrDefault(); if (image == null) { continue; } if (!string.IsNullOrWhiteSpace(image.Href)) { var v = image.Href.ToString() ?? string.Empty; var startIndex = v.IndexOf(',') + 1; var len = v.Length - startIndex; var base64 = v.Substring(startIndex, len).TrimStart(); var texture = convertBase64ToTexure(base64); images.Add(pattern.ID, texture); } } // Load new platforms var platformsToLoad = new List <Platform>(); var maxMapX = 0; var maxMapY = 0; playerStart = new Vector2(0, 0); playerExit = new Vector2(0, 0); foreach (var elem in elements) { // colors var fillColor = elem.Fill?.ToString() ?? "#FF0000"; var strokeColor = elem.Stroke?.ToString(); fillColor = fillColor.TrimStart("url(".ToArray()).TrimEnd(")".ToArray()); if (fillColor == null || !fillColor.StartsWith("#")) { fillColor = "#FF0000"; } if (strokeColor == null || !strokeColor.StartsWith("#")) { strokeColor = null; } var imageKey = fillColor.TrimStart('#'); Texture2D imageData = null; if (images.ContainsKey(imageKey)) { imageData = images[imageKey]; fillColor = "#FF0000"; } // dimensions var x = (int)Math.Round(elem.Bounds.X, 1); var y = (int)Math.Round(elem.Bounds.Y, 1); var w = (int)Math.Round(elem.Bounds.Width, 1); var h = (int)Math.Round(elem.Bounds.Height, 1); // create poly var vertexes = elem.PathData.Select(d => new Vector2(d.Start.X, d.Start.Y)) .Union(elem.PathData.Select(d => new Vector2(d.End.X, d.End.Y))) .Distinct().ToArray(); Debug.WriteLine($"[shape]"); Debug.WriteLine($"size: {x} {y} - {w} x {h}"); Debug.WriteLine($"verts: {vertexes.Length}"); if (fillColor == "#000001") { playerStart.X = x; playerStart.Y = y; } else if (fillColor == "#000002") { // Door playerExit.X = x; playerExit.Y = y; } else { // Platform var platform = new Platform(fillColor, strokeColor, vertexes, imageData); platform.Load(_loader); platformsToLoad.Add(platform); } maxMapX = Math.Max(maxMapX, x + w); maxMapY = Math.Max(maxMapY, y + h); } Debug.WriteLine($"Map size in px: {maxMapX} x {maxMapY}"); // setup chunks chunkMap.Reset(chunkWidth: 100, chunkHeight: 100, mapWidth: maxMapX, mapHeight: maxMapY); // physics reset var phys = new MapPhysics(chunkMap, physSettings); foreach (var platform in platformsToLoad) { if (!GameObjects.Contains(platform)) { GameObjects.Add(platform); } chunkMap.UpdateEntity(platform); } // Verify that the level has a beginning and an end. if (playerStart == Vector2.Zero) { throw new NotSupportedException("A level must have a starting point."); } if (playerExit == Vector2.Zero) { throw new NotSupportedException("A level must have an exit."); } // Set player position for new map player.Props.Position = playerStart; addCoinToLevel(new Vector2(300, 0)); // Add end point addDoorToLevel(playerExit); }