Exemple #1
0
        public void LoadContent(MainPlayScreen screen)
        {
            this.screen = screen;
            var animationSpriteSheet = CONTENT_MANAGER.Sprites["animation"];

            font = MainPlayScreen.font;
            var frames    = JsonConvert.DeserializeObject <List <KeyValuePair <string, Rectangle> > >(File.ReadAllText(@"Content/sprite/animation.json"));
            var anims     = new List <Animation>();
            var animnames = Enum.GetNames(typeof(AnimationName));

            foreach (var an in animnames)
            {
                anims.Add(LoadAnimation(an, frames));
            }

            animatedEntity = new AnimatedEntity(IsoPos, VT2.Zero, Color.White, 0.4f, Constant.SCALE);
            animatedEntity.LoadContent(animationSpriteSheet);
            animatedEntity.AddAnimation(anims);
            animatedEntity.PlayAnimation(AnimationName.idle_right.ToString());

            walksfx                = CONTENT_MANAGER.Sounds["footstep"].CreateInstance();
            walksfx.Volume         = 0f;
            leversfx               = CONTENT_MANAGER.Sounds["lever"].CreateInstance();
            floorswitchpressedsfx  = CONTENT_MANAGER.Sounds["switch_pressed"].CreateInstance();
            floorswitchreleasedsfx = CONTENT_MANAGER.Sounds["switch_released"].CreateInstance();

            timer = new Timer();
        }
Exemple #2
0
 public void PlayAnimation(string animationName)
 {
     AnimatedEntity?.PlayAnimation(animationName);
 }
Exemple #3
0
        private void MovePlayer(KeyboardState currentKeyboardState, KeyboardState lastKeyboardState, GameTime gameTime)
        {
            var delta     = (float)gameTime.ElapsedGameTime.TotalMilliseconds;
            var curpos    = new VT2(CollisionBox.X, CollisionBox.Y);
            var direction = Direction.none;

            if (HelperMethod.IsKeyHold(Keys.A, currentKeyboardState, lastKeyboardState))
            {
                direction = Direction.left;
            }
            if (HelperMethod.IsKeyHold(Keys.D, currentKeyboardState, lastKeyboardState))
            {
                direction = Direction.right;
            }
            if (HelperMethod.IsKeyHold(Keys.W, currentKeyboardState, lastKeyboardState))
            {
                direction = Direction.up;
            }
            if (HelperMethod.IsKeyHold(Keys.S, currentKeyboardState, lastKeyboardState))
            {
                direction = Direction.down;
            }

            if (direction != Direction.none && !timer.IsRunning)
            {
                timer.Start();
            }

            if (timer.IsRunning && timer.ElapsedTime <= 1f)
            {
                walksfx.Volume = timer.ElapsedTime;
            }

            switch (direction)
            {
            case Direction.up:
                Velocity = new VT2(0, -MovementSpeed);
                dir      = direction;
                if (animatedEntity.CurntAnimationName != AnimationName.walk_up.ToString())
                {
                    animatedEntity.PlayAnimation(AnimationName.walk_up.ToString());
                }
                if (walksfx.State != SoundState.Playing)
                {
                    walksfx.Play();
                }
                break;

            case Direction.down:
                Velocity = new VT2(0, MovementSpeed);
                dir      = direction;
                if (animatedEntity.CurntAnimationName != AnimationName.walk_down.ToString())
                {
                    animatedEntity.PlayAnimation(AnimationName.walk_down.ToString());
                }
                if (walksfx.State != SoundState.Playing)
                {
                    walksfx.Play();
                }
                break;

            case Direction.right:
                Velocity = new VT2(MovementSpeed, 0);
                dir      = direction;
                if (animatedEntity.CurntAnimationName != AnimationName.walk_right.ToString())
                {
                    animatedEntity.PlayAnimation(AnimationName.walk_right.ToString());
                }
                if (walksfx.State != SoundState.Playing)
                {
                    walksfx.Play();
                }
                break;

            case Direction.left:
                Velocity = new VT2(-MovementSpeed, 0);
                dir      = direction;
                if (animatedEntity.CurntAnimationName != AnimationName.walk_left.ToString())
                {
                    animatedEntity.PlayAnimation(AnimationName.walk_left.ToString());
                }
                if (walksfx.State != SoundState.Playing)
                {
                    walksfx.Play();
                }
                break;

            case Direction.none:
                var anme = "idle_" + dir.ToString();
                if (animatedEntity.CurntAnimationName != anme)
                {
                    animatedEntity.PlayAnimation(anme);
                }
                walksfx.Stop();
                walksfx.Volume = 0;
                timer.Reset();
                break;
            }

            var move = CollisionBox.Move(curpos.X + Velocity.X, curpos.Y + Velocity.Y, x => {
                if (x.Other.HasTag(CollisionTag.FloorSwitch))
                {
                    return(CollisionResponses.Cross);
                }

                if (x.Other.HasTag(CollisionTag.Lever))
                {
                    return(CollisionResponses.Cross);
                }

                if (x.Other.HasTag(CollisionTag.DoorOpened))
                {
                    return(CollisionResponses.Cross);
                }

                if (x.Other.HasTag(CollisionTag.EndPoint))
                {
                    return(CollisionResponses.Cross);
                }

                if (x.Other.HasTag(CollisionTag.PushableBlock))
                {
                    return(CollisionResponses.Slide);
                }

                return(CollisionResponses.Slide);
            });

            Object.WorldPos = new Vector3(CollisionBox.X, CollisionBox.Y, 0);

            var floorswitch = move.Hits.FirstOrDefault(c => c.Box.HasTag(CollisionTag.FloorSwitch));

            if (floorswitch != null)
            {
                Object obj = (Object)floorswitch.Box.Data;
                if (lastInteractableObject != obj && obj.TileType.IsOff() && floorswitchpressedsfx.State != SoundState.Playing)
                {
                    obj.TileType = SpriteSheetRectName.ButtonPressed_E;
                    floorswitchpressedsfx.Play();
                    obj.Activate(Object, obj);
                    lastInteractableObject = obj;
                }
            }
            else
            {
                if (lastInteractableObject != null && lastInteractableObject.TileType.IsFloorSwitch())
                {
                    if (floorswitchreleasedsfx.State != SoundState.Playing)
                    {
                        floorswitchreleasedsfx.Play();
                    }
                    lastInteractableObject.TileType = SpriteSheetRectName.Button_E;
                    lastInteractableObject.Deactivate(Object, lastInteractableObject);
                    lastInteractableObject = null;
                }
            }

            var endpoint = move.Hits.FirstOrDefault(c => c.Box.HasTag(CollisionTag.EndPoint));

            if (endpoint != null)
            {
                Object obj = (Object)endpoint.Box.Data;
                if (lastInteractableObject != obj)
                {
                    obj.Activate(Object, obj);
                    lastInteractableObject = obj;
                }
            }
            else
            {
                if (lastInteractableObject != null && lastInteractableObject.TileType.GetCollisionTag() == CollisionTag.EndPoint)
                {
                    lastInteractableObject = null;
                }
            }

            var lever = move.Hits.FirstOrDefault(c => c.Box.HasTag(CollisionTag.Lever));

            if (lever != null)
            {
                Object obj = (Object)lever.Box.Data;
                lastInteractableObject = obj;
            }
            else
            {
                if (lastInteractableObject != null && lastInteractableObject.TileType.IsLever())
                {
                    lastInteractableObject = null;
                }
            }

            var block = move.Hits.FirstOrDefault(c => c.Box.HasTag(CollisionTag.PushableBlock));

            if (block != null)
            {
                Block obj = (Block)block.Box.Data;
                var   n   = block.Normal;
                obj.Velocity = new VT2(n.X * n.X, n.Y * n.Y) * Velocity;
            }

            var portal = move.Hits.FirstOrDefault(c => c.Box.HasTag(CollisionTag.Portal));

            if (portal != null)
            {
                Object obj = (Object)portal.Box.Data;
                if (obj.TileType.IsOn())
                {
                    obj.Activate(Object, obj);
                }
            }

            animatedEntity.Position = IsoPos;
            Velocity = VT2.Zero;
        }
        private static void LoadAnimationContent()
        {
            //string delimit = "Yellow";
            CONTENT_MANAGER.animationEntities = new Dictionary <SpriteSheetUnit, AnimatedEntity>();
            CONTENT_MANAGER.animationSheets   = new Dictionary <SpriteSheetUnit, Texture2D>();
            CONTENT_MANAGER.animationTypes    = new List <Animation>();

            //list of unit type
            var UnitTypes = new List <SpriteSheetUnit>((IEnumerable <SpriteSheetUnit>)Enum.GetValues(typeof(SpriteSheetUnit)));

            //Artillery
            //load animation sprite sheet for each unit type
            foreach (SpriteSheetUnit unittype in UnitTypes)
            {
                var paths = unittype.ToString().Split('_');
                //if (paths[0].CompareTo(delimit) == 0)
                {
                    //  break;
                }
                CONTENT_MANAGER.animationSheets.Add(unittype, CONTENT_MANAGER.Content.Load <Texture2D>("sprite//Animation//" + paths[0] + "//" + paths[1]));
            }

            //declare animation frame

            //animation frame for "normal" unit
            Animation idle = new Animation("idle", true, 4, string.Empty);

            for (int i = 0; i < 4; i++)
            {
                idle.AddKeyFrame(i * 48, 0, 48, 48);
            }

            Animation right = new Animation("right", true, 4, string.Empty);

            for (int i = 0; i < 4; i++)
            {
                right.AddKeyFrame(i * 48, 48, 48, 48);
            }

            Animation up = new Animation("up", true, 4, string.Empty);

            for (int i = 0; i < 4; i++)
            {
                up.AddKeyFrame(i * 48, 96, 48, 48);
            }

            Animation down = new Animation("down", true, 4, string.Empty);

            for (int i = 0; i < 4; i++)
            {
                down.AddKeyFrame(i * 48, 144, 48, 48);
            }

            Animation done = new Animation("done", true, 1, string.Empty);

            done.AddKeyFrame(0, 192, 48, 48);

            //animation frame for "HIGH" unit
            Animation idleAir = new Animation("idle", true, 4, string.Empty);

            for (int i = 0; i < 4; i++)
            {
                idleAir.AddKeyFrame(i * 64, 0, 64, 64);
            }

            Animation rightAir = new Animation("right", true, 4, string.Empty);

            for (int i = 0; i < 4; i++)
            {
                rightAir.AddKeyFrame(i * 64, 64, 64, 64);
            }

            Animation upAir = new Animation("up", true, 4, string.Empty);

            for (int i = 0; i < 4; i++)
            {
                upAir.AddKeyFrame(i * 64, 128, 64, 64);
            }

            Animation downAir = new Animation("down", true, 4, string.Empty);

            for (int i = 0; i < 4; i++)
            {
                downAir.AddKeyFrame(i * 64, 192, 64, 64);
            }

            Animation doneAir = new Animation("done", true, 1, string.Empty);

            doneAir.AddKeyFrame(0, 256, 64, 64);

            //animation frame for copter unit
            Animation idleCopter = new Animation("idle", true, 3, string.Empty);

            for (int i = 0; i < 3; i++)
            {
                idleCopter.AddKeyFrame(i * 64, 0, 64, 64);
            }

            CONTENT_MANAGER.animationTypes.Add(idle);
            CONTENT_MANAGER.animationTypes.Add(right);
            CONTENT_MANAGER.animationTypes.Add(up);
            CONTENT_MANAGER.animationTypes.Add(down);
            CONTENT_MANAGER.animationTypes.Add(done);

            foreach (SpriteSheetUnit unittype in UnitTypes)
            {
                string         unittypestring = unittype.ToString();
                AnimatedEntity temp           = new AnimatedEntity(Vector2.Zero, Vector2.Zero, Color.White, LayerDepth.Unit);
                //if (unittypestring.Contains(delimit))
                {
                    //break;
                }

                temp.LoadContent(CONTENT_MANAGER.animationSheets[unittype]);

                if (unittypestring.Contains("TransportCopter") ||
                    unittypestring.Contains("BattleCopter") ||
                    unittypestring.Contains("Fighter") ||
                    unittypestring.Contains("Bomber"))
                {
                    //we enter "HIGH" mode
                    //first we set the origin to "HIGH"
                    //because we are drawing from topleft and the sprite size is 64x64
                    temp.Origin = new Vector2(8, 16);

                    //then we load the "HIGH" animation in
                    if (unittypestring.Contains("Copter"))
                    {
                        temp.AddAnimation(idleCopter, rightAir, upAir, downAir, doneAir);
                    }
                    else
                    {
                        temp.AddAnimation(idleAir, rightAir, upAir, downAir, doneAir);
                    }
                }
                else
                {
                    //we enter "normal" mode
                    temp.AddAnimation(idle, right, up, down, done);
                }

                temp.PlayAnimation("idle");
                CONTENT_MANAGER.animationEntities.Add(unittype, temp);
            }
        }