Пример #1
0
 public override void Setup(Entity entity, TiledMapObject i)
 {
     this.entity   = entity;
     canShoot      = true;
     shootCooldown = 2;
     shootTimer    = new Timer();
 }
        public Entity CreateMapObject(string type, Vector2?position = null, TiledMapObject tiledMapObj = null)
        {
            if (!GameObjectTypes.TryGetValue(type, out var objType))
            {
                throw new ArgumentException("Invalid type: " + type);
            }

            var entity = _world.CreateEntity();

            if (position.HasValue)
            {
                var x = position.Value.X - (position.Value.X % 32) + 16;
                var y = position.Value.Y - (position.Value.Y % 32) + 16;
                entity.Set(new Position(new Vector2(x, y)));
            }

            if (objType.Sprite != null)
            {
                entity.Set(new RenderingObject(_content.GetSprite(objType.Sprite), objType.Sprite.Path));
            }

            if (objType.Handler == null || !Handlers.TryGetValue(objType.Handler, out var handler))
            {
                return(entity);
            }

            handler(entity, objType, tiledMapObj);

            return(entity);
        }
        private EnemyBehaviour GetEnemyBehaviour(TiledMapObject i)
        {
            if (i.Properties.ContainsKey("Behaviour"))
            {
                switch (i.Properties["Behaviour"])
                {
                case "UpDown":
                    return(new EnemyBehaviourUpDown());

                case "LeftRight":
                    return(new EnemyBehaviourLeftRight());

                case "Circle":
                    return(new EnemyBehaviourCircle());

                case "Shoot":
                    return(new EnemyBehaviourShoot());

                default:
                    return(new EnemyEmptyBehaviour());
                }
            }

            return(new EnemyEmptyBehaviour());
        }
Пример #4
0
        void LoadObjects()
        {
            foreach (TiledMapObjectLayer layer in map.ObjectLayers)
            {
                if (layer.Name == "Enemies")
                {
                    foreach (TiledMapObject thing in layer.Objects)
                    {
                        Enemy   enemy = new Enemy();
                        Vector2 tiles = new Vector2((int)(thing.Position.X / tileHeight), (int)(thing.Position.Y / tileHeight));
                        enemy.enemySprite.position = tiles * tileHeight;
                        enemy.Load(Content, this);
                        enemies.Add(enemy);
                    }
                }

                if (layer.Name == "Goal")
                {
                    TiledMapObject thing = layer.Objects[0];
                    if (thing != null)
                    {
                        Chest chest = new Chest();
                        chest.chestSprite.position = new Vector2(thing.Position.X, thing.Position.Y);
                        chest.Load(Content, this);
                        goal = chest;
                    }
                }
            }
        }
Пример #5
0
 private void Start()
 {
     player.SetPosition(new Vector2(64, 448));
     score = 0;
     enemies.Clear();
     foreach (TiledMapObjectLayer layer in map.ObjectLayers)
     {
         if (layer.Name == "Enemies")
         {
             foreach (TiledMapObject obj in layer.Objects)
             {
                 Enemy enemy = new Enemy(this);
                 enemy.Load(Content);
                 enemy.Position = new Vector2(
                     obj.Position.X, obj.Position.Y);
                 enemies.Add(enemy);
             }
         }
         if (layer.Name == "Goal")
         {
             TiledMapObject obj = layer.Objects[0];
             if (obj != null)
             {
                 AnimatedTexture anim = new AnimatedTexture(
                     Vector2.Zero, 0, 1, 1);
                 anim.Load(Content, "goal", 1, 1);
                 goal = new Sprite();
                 goal.Add(anim, 0, -32);
                 goal.position = new Vector2(
                     obj.Position.X, obj.Position.Y);
             }
         }
     }
 }
Пример #6
0
 public static Rectangle GetObjectRectangleInMapPoints(TiledMapObject deployment)
 {
     return(new Rectangle(
                (int)Math.Round(deployment.Position.X / 32, 0, MidpointRounding.AwayFromZero),
                (int)Math.Round(deployment.Position.Y / 32, 0, MidpointRounding.AwayFromZero),
                (int)Math.Round(deployment.Size.Width / 32, 0, MidpointRounding.AwayFromZero),
                (int)Math.Round(deployment.Size.Height / 32, 0, MidpointRounding.AwayFromZero)));
 }
 private int GetEnemyRange(TiledMapObject i)
 {
     if (i.Properties.ContainsKey("Range"))
     {
         return(Int32.Parse(i.Properties["Range"]));
     }
     return(0);
 }
 private bool GetEnemyGoingDown(TiledMapObject i)
 {
     if (i.Properties.ContainsKey("GoingDown"))
     {
         return(string.Compare(i.Properties["GoingDown"], "true") == 0 ? true : false);
     }
     return(false);
 }
Пример #9
0
        public Entity(TiledMapObject tile)
        {
            entityTile = tile;

            positionVector = new Vector2(
                (int)entityTile.Position.X + 1,
                (int)entityTile.Position.Y + 1
                );
        }
Пример #10
0
        public Enemy1(TiledMapObject tile) : base(tile)
        {
            currentMovement = "center";
            timerElapsed    = false;

            aTimer           = new Timer(2000);
            aTimer.Elapsed  += OnTimedEvent;
            aTimer.AutoReset = true;
            aTimer.Enabled   = true;
        }
Пример #11
0
 public override void Setup(Entity entity, TiledMapObject i)
 {
     this.entity  = entity;
     startX       = entity.box.X;
     startY       = entity.box.Y;
     radius       = 32;
     arc          = 0;
     angularSpeed = Math.PI / 2.0;
     entity.box.X = (int)(startX + radius * Math.Cos(arc) - entity.box.Width / 2);
     entity.box.Y = (int)(startY + radius * Math.Sin(arc) - entity.box.Height / 2);
 }
Пример #12
0
 void PortalHandler(Entity e, GameObjectTypeInfo obj, TiledMapObject tiled)
 {
     e.Set(new Trigger
     {
         Invoke = (action, entity) =>
         {
             var destination = tiled.Properties["destination"];
             GameContext.LoadMap(destination);
         }
     });
     e.Set(new Cursor("hand", new Rectangle(-16, -16, 32, 32)));
 }
Пример #13
0
        public Warp(TiledMapObject ob, Location loc) : base(loc)
        {
            collisionBox = new Rectangle(0, 0, (int)ob.Size.Width, (int)ob.Size.Height);
            posX         = (int)ob.Position.X;
            posY         = (int)ob.Position.Y;
            var pos = new Vector2(float.Parse(ob.Properties["xPos"]), float.Parse(ob.Properties["yPos"]));

            OffsetPos       = pos;
            DestinationMap  = ob.Properties["destMap"];
            DestinationWarp = ob.Properties["destWarpName"];
            Name            = ob.Name;
        }
Пример #14
0
        public GameWorldArea(TiledMapObject obj)
        {
            this.details = obj;

            this.ID     = obj.Identifier;
            this.bounds = null;

            if (obj is TiledMapPolygonObject)
            {
                var polyObj = (TiledMapPolygonObject)obj;
                var polygon = new PolygonF(polyObj.Points.Select(x => new Vector2(x.X, x.Y)));
                polygon.Offset(new Vector2(polyObj.Position.X, polyObj.Position.Y));

                this.bounds = polygon;
            }
            else if (obj is TiledMapRectangleObject)
            {
                var rectObj = (TiledMapRectangleObject)obj;
                this.bounds = new RectangleF((int)rectObj.Position.X, (int)rectObj.Position.Y, (int)rectObj.Size.Width, (int)rectObj.Size.Height);
            }
            else
            {
                throw new ArgumentException("Invalid GameMapArea");
            }


            if (obj.Properties != null)
            {
                String lifeDegen, speedModifier, slippery;

                if (obj.Properties.TryGetValue("life_degeneration", out lifeDegen))
                {
                    this.degenerationRatio = float.Parse(lifeDegen, CultureInfo.InvariantCulture);
                }

                if (obj.Properties.TryGetValue("speed_modifier", out speedModifier))
                {
                    this.speedModifier = float.Parse(speedModifier, CultureInfo.InvariantCulture);
                }
                else
                {
                    this.speedModifier = 1.0f;
                }

                if (obj.Properties.TryGetValue("slippery", out slippery))
                {
                    this.slippery = Boolean.Parse(slippery);
                }
            }
        }
Пример #15
0
        // a method to create collision detection from the object layers
        public void collision(TiledMapObject obj)
        {
            // collision object dimensions
            float objX      = obj.Position.X;
            float objY      = obj.Position.Y;
            float objHeight = obj.Size.Height;
            float objWidth  = obj.Size.Width;

            // buffers to create separation between collision sides
            float buff1 = sprite.Width / 2; // compensate for player width
            float buff2 = sprite.Width / 2 + 4;

            // hard code collision detection

            // left
            if (position.X > objX - buff1 &&
                position.X < objX + objWidth + buff1 &&
                position.Y > objY - buff1 &&
                position.Y < objY + objHeight + buff1)
            {
                position.X = objX - buff1;
            }

            // top
            if (position.Y > objY - buff2 &&
                position.Y < objY + objHeight &&
                position.X > objX - buff1 &&
                position.X < objX + objWidth + buff1)
            {
                position.Y = objY - buff2;
            }

            // right
            if (position.X < objX + objWidth + buff2 &&
                position.X > objX - buff1 &&
                position.Y > objY - buff1 &&
                position.Y < objY + objHeight + buff1)
            {
                position.X = objX + objWidth + buff2;
            }

            // bottom
            if (position.Y < objY + objHeight + buff2 &&
                position.Y > objY &&
                position.X > objX - buff1 &&
                position.X < objX + objWidth + buff1)
            {
                position.Y = objY + objHeight + buff2;
            }
        }
        public override void Setup(Entity entity, TiledMapObject i)
        {
            this.entity = entity;
            start       = entity.box.X;

            if (i.Properties.ContainsKey("Range"))
            {
                this.range = Int32.Parse(i.Properties["Range"]);
            }

            if (i.Properties.ContainsKey("GoingLeft"))
            {
                this.goingLeft = string.Compare(i.Properties["GoingLeft"], "true") == 0 ? true : false;
            }
        }
Пример #17
0
        void ChestHandler(Entity e, GameObjectTypeInfo obj, TiledMapObject tiled)
        {
            e.Set(GameObjectType.Storage);
            e.Set(new Blocking());
            var storage = new Storage();

            foreach (var prop in tiled.Properties.Where(e => e.Key.StartsWith('~')))
            {
                var type         = GameObjectTypes[prop.Key.Substring(1)];
                var countInStack = int.Parse(prop.Value);
                if (countInStack == 0)
                {
                    continue;
                }

                while (countInStack > 0)
                {
                    if (type.StackSize < countInStack)
                    {
                        countInStack -= type.StackSize;
                        storage.Content.Add(new ItemStack
                        {
                            ItemType = type,
                            Count    = type.StackSize
                        });
                    }
                    else
                    {
                        storage.Content.Add(new ItemStack
                        {
                            ItemType = type,
                            Count    = countInStack,
                        });
                        break;
                    }
                }
            }

            e.Set(storage);
            e.Set(new Serializable());
            e.Set(new Cursor("hand", new Rectangle(-16, -16, 32, 32)));
        }
        private EnemyMeteorSize GetEnemyMeteorSize(TiledMapObject i)
        {
            if (i.Properties.ContainsKey("Size"))
            {
                switch (i.Properties["Size"])
                {
                case "SM":
                    return(EnemyMeteorSize.SM);

                case "MD":
                    return(EnemyMeteorSize.MD);

                case "LG":
                    return(EnemyMeteorSize.LG);

                default:
                    return(EnemyMeteorSize.SM);
                }
            }
            return(EnemyMeteorSize.SM);
        }
Пример #19
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            // update player position
            player.updatePosition(gameTime);

            // collision detection
            for (int i = 0; i < objectLayer.Objects.Length; i++)
            {
                collisionObject = map.GetLayer <TiledMapObjectLayer>("collision").Objects.ElementAt <TiledMapObject>(i);
                player.collision(collisionObject);
            }
            player.setBoundaries(mapWidth, mapHeight);

            // update tmx map
            mapRenderer.Update(gameTime);

            base.Update(gameTime);
        }
Пример #20
0
        private void InitGroup(TiledMapObject obj, Rectangle bounds)
        {
            var loot = EntityData.Get(obj.Properties.GetString("loot"));

            if (loot == null)
            {
                Log.Error("Group at " + bounds + " has an invalid loot '" + obj.Properties.GetString("loot") + "'.");
                return;
            }

            var id = groupRewards.Count;

            groupRewards.Add(loot);

            IterateEntities((entity) =>
            {
                if (entity.Data.Groupable && Mathf.IntersectPoint(bounds, entity.TileX * Global.TileSize, entity.TileY * Global.TileSize))
                {
                    entity.GroupId = id;
                    Log.Debug("Assigned " + entity + " to group " + id + ".");
                }
            });
        }
Пример #21
0
        void ActorHandler(Entity e, GameObjectTypeInfo type, TiledMapObject tiledMapObj)
        {
            if (type.TypeName == "player")
            {
                e.Set(new Storage());
                GameContext.Player = e;
                e.Set <IGameAI>(new PlayerControl());
            }
            if (type.TypeName == "enemy")
            {
                var sprite = e.Get <RenderingObject>();
                e.Set(new Cursor("sword", new Rectangle((int)sprite.Origin.X, (int)sprite.Origin.Y, sprite.Bounds.Width, sprite.Bounds.Height)));
                e.Set(GameObjectType.Enemy);
                e.Set <IGameAI>(new RandomMovement());
            }

            e.Set(new ActionPoints
            {
                Max    = 10,
                Remain = 10
            });
            e.Set(new Serializable());
            e.Set(new AllowedToAct());
        }
        void LoadObjects()
        {
            foreach (TiledMapObjectLayer layer in map.ObjectLayers)
            {
                if (layer.Name == "Respawn")
                {
                    TiledMapObject thing = layer.Objects[0];
                    if (thing != null)
                    {
                        Sprite respawn = new Sprite();
                        respawn.position  = new Vector2(thing.Position.X, thing.Position.Y);
                        currentCheckpoint = respawn;
                    }
                }

                if (layer.Name == "Enemies")
                {
                    foreach (TiledMapObject thing in layer.Objects)
                    {
                        Enemy   enemy = new Enemy();
                        Vector2 tiles = new Vector2((int)(thing.Position.X / tileHeight), (int)(thing.Position.Y / tileHeight));
                        enemy.enemySprite.position = tiles * tileHeight;
                        enemy.Load(Content, this);
                        enemies.Add(enemy);
                    }
                }

                if (layer.Name == "Key")
                {
                    TiledMapObject thing = layer.Objects[0];
                    if (thing != null)
                    {
                        Key key = new Key();
                        key.keySprite.position = new Vector2(thing.Position.X, thing.Position.Y);
                        key.Load(Content, this);
                        unlock = key;
                    }
                }

                if (layer.Name == "Goal")
                {
                    TiledMapObject thing = layer.Objects[0];
                    if (thing != null)
                    {
                        Chest chest = new Chest();
                        chest.chestSprite.position = new Vector2(thing.Position.X, thing.Position.Y);
                        chest.Load(Content, this);
                        goal = chest;
                    }
                }

                if (layer.Name == "Hazards")
                {
                    foreach (TiledMapObject thing in layer.Objects)
                    {
                        Hazards hazard = new Hazards();
                        Vector2 tiles  = new Vector2((int)(thing.Position.X / tileHeight), (int)(thing.Position.Y / tileHeight));
                        hazard.hazardsSprite.position = tiles * tileHeight;
                        hazard.Load(Content, this);
                        hazards.Add(hazard);
                    }
                }
            }
        }
Пример #23
0
        public override void Update(ContentManager Content, GameTime gameTime)
        {
            if (isLoaded == false)
            {
                font = Content.Load <SpriteFont>("Arial");

                var viewportAdapter = new BoxingViewportAdapter(game1.Window, game1.GraphicsDevice, ScreenWidth, ScreenHeight);
                camera          = new Camera2D(viewportAdapter);
                camera.Position = new Vector2(0, ScreenHeight);

                map         = Content.Load <TiledMap>("Project_Map");
                mapRenderer = new TiledMapRenderer(game1.GraphicsDevice);

                player.Load(Content);

                foreach (TiledMapTileLayer layer in map.TileLayers)
                {
                    if (layer.Name == "Collision")
                    {
                        collisionLayer = layer;
                    }
                    if (layer.Name == "Spikes")
                    {
                        spikeLayer = layer;
                    }
                }

                foreach (TiledMapObjectLayer layer in map.ObjectLayers)
                {
                    if (layer.Name == "Enemies")
                    {
                        foreach (TiledMapObject obj in layer.Objects)
                        {
                            Enemy enemy = new Enemy(this);
                            enemy.Load(Content);
                            enemy.Position = new Vector2(obj.Position.X, obj.Position.Y);
                            enemies.Add(enemy);
                        }
                    }
                    if (layer.Name == "Goal")
                    {
                        TiledMapObject obj = layer.Objects[0];
                        if (obj != null)
                        {
                            AnimatedTexture anim = new AnimatedTexture(Vector2.Zero, 0, 1, 1);
                            anim.Load(Content, "chest", 1, 1);
                            goal = new Sprite();
                            goal.Add(anim, 0, 5);
                            goal.position = new Vector2(obj.Position.X, obj.Position.Y);
                        }
                    }
                    if (layer.Name == "Key")
                    {
                        TiledMapObject key = layer.Objects[0];
                        if (key != null)
                        {
                            AnimatedTexture keyAnim = new AnimatedTexture(Vector2.Zero, 0, 1, 1);
                            keyAnim.Load(Content, "Key", 1, 1);
                            keys = new Sprite();
                            keys.Add(keyAnim, 0, 5);
                            keys.position = new Vector2(key.Position.X, key.Position.Y);
                        }
                    }
                }

                gameMusic                  = Content.Load <SoundEffect>("Superhero_violin_no_intro");
                gameMusicInstance          = gameMusic.CreateInstance();
                gameMusicInstance.IsLooped = true;
                gameMusicInstance.Play();

                zombieDeath         = Content.Load <SoundEffect>("zombie_death");
                zombieDeathInstance = zombieDeath.CreateInstance();
                keyJingle           = Content.Load <SoundEffect>("key_collect");
                keyJingleInstance   = keyJingle.CreateInstance();
                chestOpen           = Content.Load <SoundEffect>("chest_opened");
                chestOpenInstance   = chestOpen.CreateInstance();

                arialFont = Content.Load <SpriteFont>("Arial");
                heart     = Content.Load <Texture2D>("heart");
                keyIcon   = Content.Load <Texture2D>("Key - Icon");
                lives     = 3;

                isLoaded = true;
            }

            float deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds;

            player.Update(deltaTime);
            camera.Position = player.Position - new Vector2(ScreenWidth / 2, ScreenHeight / 2);

            if (keyLost == true)
            {
                timer += deltaTime;
                if (timer >= 3.0f)
                {
                    keyLost = false;
                    timer   = 0f;
                }
            }

            if (score <= 0)
            {
                score = 0;
            }

            foreach (Enemy e in enemies)
            {
                e.Update(deltaTime);
            }

            CheckCollisions();

            if (lives <= 0 || keyCollected == true && chestInteracted == true)
            {
                if (keyCollected == true && chestInteracted == true)
                {
                    score += 3;
                    chestOpen.Play();
                }
                _2D_Platformer.StateManager.ChangeState("GAMEOVER");
                enemies.Clear();
                gameMusicInstance.Stop();
                keyCollected = false;

                isLoaded = false;
            }
        }
Пример #24
0
 public override void Setup(Entity entity, TiledMapObject i)
 {
 }
Пример #25
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            player.Load(Content);
            candaraFont = Content.Load <SpriteFont>("Candara");
            heart       = Content.Load <Texture2D>("heart");

            BoxingViewportAdapter viewportAdapter = new BoxingViewportAdapter(Window,
                                                                              GraphicsDevice,
                                                                              ScreenWidth, ScreenHeight);

            camera          = new Camera2D(viewportAdapter);
            camera.Position = new Vector2(0, ScreenHeight);

            gameMusic          = Content.Load <Song>("Music/SuperHero_original_no_Intro");
            MediaPlayer.Volume = 0.1f;
            MediaPlayer.Play(gameMusic);

            map         = Content.Load <TiledMap>("samp1");
            mapRenderer = new TiledMapRenderer(GraphicsDevice);

            foreach (TiledMapTileLayer layer in map.TileLayers)
            {
                if (layer.Name == "Collisions")
                {
                    collisionLayer = layer;
                }
            }

            foreach (TiledMapObjectLayer layer in map.ObjectLayers)
            {
                if (layer.Name == "Enemies")
                {
                    foreach (TiledMapObject obj in layer.Objects)
                    {
                        Enemy enemy = new Enemy(this);
                        enemy.Load(Content);
                        enemy.Position = new Vector2(obj.Position.X, obj.Position.Y);
                        enemies.Add(enemy);
                    }
                }

                if (layer.Name == "Loot")
                {
                    TiledMapObject obj = layer.Objects[0];
                    if (obj != null)
                    {
                        AnimatedTexture anim = new AnimatedTexture(Vector2.Zero, 0, 1, 1);
                        anim.Load(Content, "gem_3", 1, 1);
                        gem = new Sprite();
                        gem.Add(anim, 0, 5);
                        gem.position = new Vector2(obj.Position.X, obj.Position.Y);
                    }
                }
            }


            // TODO: use this.Content to load your game content here
        }
 public abstract void Setup(Entity entity, TiledMapObject i);
Пример #27
0
 public Player(TiledMapObject tile) : base(tile)
 {
 }