public override void Destroy() { AudioEngine.Play("EnemyDie"); foreach (string item in CarriedItems) { if (item == "Key") { DoorKey key = new DoorKey(Scene, Transform.Position - new Vector2(0, 10)); key.VelocityY -= 2; } else if (item == "Fuel") { FuelCan fuel = new FuelCan(Scene, Transform.Position - new Vector2(0, 10), 10); fuel.VelocityY -= 2; fuel.VelocityX = -2; } else if (item == "HandgunAmmo") { Ammo ammo = new Ammo(Scene, Transform.Position - new Vector2(0, 10), 10, typeof(Handgun)); ammo.VelocityY -= 2; ammo.VelocityX = 2; } else if (item == "MachinegunAmmo") { Ammo ammo = new Ammo(Scene, Transform.Position - new Vector2(0, 10), 50, typeof(Machinegun)); ammo.VelocityY -= 2; ammo.VelocityX = 2; } else if (item == "ShotgunAmmo") { Ammo ammo = new Ammo(Scene, Transform.Position - new Vector2(0, 10), 10, typeof(Shotgun)); ammo.VelocityY -= 2; ammo.VelocityX = 2; } } base.Destroy(); }
public void LoadEntities(AbstractScene scene, string levelID) { foreach (EntityInstance entity in world.ParseLevel(scene, levelID)) { Vector2 position = new Vector2(entity.Px[0], entity.Px[1]); if (entity.Identifier.Equals("Hero")) { hero = new Hero(scene, position); } else if (entity.Identifier.Equals("Lava")) { new Lava(scene, (int)entity.Width, (int)entity.Height, position); } else if (entity.Identifier.Equals("Spikes")) { Direction dir = default; foreach (FieldInstance field in entity.FieldInstances) { if (field.Identifier == "Direction") { dir = Enum.Parse(typeof(Direction), field.Value); } } float size = entity.Width > entity.Height ? entity.Width : entity.Height; new Spikes(scene, position, (int)size, dir); } else if (entity.Identifier.Equals("Fuel")) { float amount = 0; bool gravity = true; foreach (FieldInstance field in entity.FieldInstances) { if (field.Identifier == "Amount") { amount = (float)field.Value; } else if (field.Identifier == "HasGravity") { gravity = field.Value; } } FuelCan can = new FuelCan(scene, position, amount); can.HasGravity = gravity; } else if (entity.Identifier.Equals("EnemyPatrolTrigger")) { new EnemyPatrolTrigger(scene, (int)entity.Width, (int)entity.Height, position); } else if (entity.Identifier.Equals("Enemy")) { Direction dir = Direction.WEST; bool patrol = true; bool tutorial = false; Newtonsoft.Json.Linq.JArray array = null; foreach (FieldInstance field in entity.FieldInstances) { if (field.Identifier == "Direction") { dir = Enum.Parse(typeof(Direction), field.Value); } else if (field.Identifier == "Patrol") { patrol = field.Value; } else if (field.Identifier == "Tutorial") { tutorial = field.Value; } else if (field.Identifier == "CarriedItems") { array = field.Value; } } List <string> carriedItems = array.ToObject <List <string> >(); EnemyTest enemy = new EnemyTest(scene, position, dir); enemy.Patrol = patrol; enemy.Tutorial = tutorial; if (carriedItems != null && carriedItems.Count > 0) { enemy.CarriedItems = carriedItems; } } else if (entity.Identifier.Equals("Door")) { bool locked = false; foreach (FieldInstance field in entity.FieldInstances) { if (field.Identifier == "Locked") { locked = field.Value; } } new Door(scene, position, (int)entity.Height, locked); } else if (entity.Identifier.Equals("MountedGun")) { Direction dir = default; Newtonsoft.Json.Linq.JArray array = null; foreach (FieldInstance field in entity.FieldInstances) { if (field.Identifier == "Direction") { dir = Enum.Parse(typeof(Direction), field.Value); } else if (field.Identifier == "Integer") { array = field.Value; } } List <int> param = array.ToObject <List <int> >(); new MountedGun(scene, position, dir, param[0], param[1], param[2], param[3]); } else if (entity.Identifier.Equals("Saw")) { bool horizontal = false; foreach (FieldInstance field in entity.FieldInstances) { if (field.Identifier == "Horizontal") { horizontal = field.Value; } } new Saw(scene, position, horizontal); } else if (entity.Identifier.Equals("TrapTrigger")) { new TrapTrigger(scene, position, (int)entity.Width, (int)entity.Height); } else if (entity.Identifier.Equals("TutorialTrigger")) { string tutorial = ""; foreach (FieldInstance field in entity.FieldInstances) { if (field.Identifier == "Tutorial") { tutorial = field.Value; } } new TutorialTrigger(scene, position, (int)entity.Width, (int)entity.Height, tutorial); } else if (entity.Identifier.Equals("Ammo")) { Type weaponType = null; int amount = 0; foreach (FieldInstance field in entity.FieldInstances) { if (field.Identifier == "Weapon") { if (field.Value == "Handgun") { weaponType = typeof(Handgun); } else if (field.Value == "Machinegun") { weaponType = typeof(Machinegun); } else if (field.Value == "Shotgun") { weaponType = typeof(Shotgun); } } else if (field.Identifier == "Amount") { amount = (int)field.Value; } } new Ammo(scene, position, amount, weaponType); } else if (entity.Identifier.Equals("HealthPickup")) { new HealthPickup(scene, position); } else if (entity.Identifier.Equals("EndGameTrigger")) { new EndGameTrigger(scene, position, (int)entity.Width, (int)entity.Height); } } }
public Hero(AbstractScene scene, Vector2 position) : base(scene.LayerManager.EntityLayer, null, position) { //DEBUG_SHOW_PIVOT = true; AddTag("Hero"); AddCollisionAgainst("Enemy"); AddCollisionAgainst("Spikes"); AddCollisionAgainst("Trap"); CanFireTriggers = true; DrawPriority = 10; AddCollisionAgainst("Pickup"); AddCollisionAgainst("Door", false); AddCollisionAgainst("Key"); AddCollisionAgainst("MountedGunBullet"); SetupController(); CollisionOffsetBottom = 1; CollisionOffsetLeft = 0.5f; CollisionOffsetRight = 0.5f; CollisionOffsetTop = 1; CurrentFaceDirection = Direction.EAST; collisionComponent = new BoxCollisionComponent(this, 18, 30, new Vector2(-8, -30)); AddComponent(collisionComponent); //DEBUG_SHOW_COLLIDER = true; AnimationStateMachine animations = new AnimationStateMachine(); animations.Offset = offset; AddComponent(animations); SpriteSheetAnimation idleLeft = new SpriteSheetAnimation(this, Assets.GetTexture("HeroIdle"), 24); /*idleLeft.AnimationSwitchCallback = () => { if (CurrentWeapon != null) CurrentWeapon.SetAnimationBreathingOffset(Vector2.Zero); }; * idleLeft.EveryFrameAction = (frame) => * { * if (CurrentWeapon == null) return; * float unit = 0.5f; * float offsetY = 0; * if (frame == 2 || frame == 3 || frame == 4) * { * offsetY += unit; * } * else if (frame == 6 || frame == 7) * { * offsetY -= unit; * } * CurrentWeapon.SetAnimationBreathingOffset(new Vector2(0, offsetY)); * };*/ idleLeft.StartedCallback = () => { if (CurrentWeapon == null) { return; } if (CurrentWeapon is Shotgun) { (CurrentWeapon as Handgun).SetLeftFacingOffset(new Vector2(-3, -17)); (CurrentWeapon as Handgun).SetRightFacingOffset(new Vector2(3, -17)); } else if (CurrentWeapon is Machinegun) { (CurrentWeapon as Handgun).SetLeftFacingOffset(new Vector2(-3, -17)); (CurrentWeapon as Handgun).SetRightFacingOffset(new Vector2(3, -17)); } else if (CurrentWeapon is Handgun) { (CurrentWeapon as Handgun).SetLeftFacingOffset(new Vector2(-3, -17)); (CurrentWeapon as Handgun).SetRightFacingOffset(new Vector2(3, -17)); } }; animations.RegisterAnimation("IdleLeft", idleLeft, () => previousPosition == Transform.Position && CurrentFaceDirection == Direction.WEST); SpriteSheetAnimation idleRight = idleLeft.CopyFlipped(); animations.RegisterAnimation("IdleRight", idleRight, () => previousPosition == Transform.Position && CurrentFaceDirection == Direction.EAST); SpriteSheetAnimation runLeft = new SpriteSheetAnimation(this, Assets.GetTexture("HeroRun"), 40); runLeft.StartedCallback = () => { if (CurrentWeapon == null) { return; } if (CurrentWeapon is Shotgun) { (CurrentWeapon as Handgun).SetLeftFacingOffset(new Vector2(-3, -17)); (CurrentWeapon as Handgun).SetRightFacingOffset(new Vector2(3, -17)); } else if (CurrentWeapon is Machinegun) { (CurrentWeapon as Handgun).SetLeftFacingOffset(new Vector2(-3, -17)); (CurrentWeapon as Handgun).SetRightFacingOffset(new Vector2(3, -17)); } else if (CurrentWeapon is Handgun) { (CurrentWeapon as Handgun).SetLeftFacingOffset(new Vector2(1, -17)); (CurrentWeapon as Handgun).SetRightFacingOffset(new Vector2(-1, -17)); } }; animations.RegisterAnimation("RunLeft", runLeft, () => Velocity.X < 0 && CurrentFaceDirection == Direction.WEST); SpriteSheetAnimation runRight = runLeft.CopyFlipped(); animations.RegisterAnimation("RunRight", runRight, () => Velocity.X > 0 && CurrentFaceDirection == Direction.EAST); SpriteSheetAnimation runBackwardsLeft = new SpriteSheetAnimation(this, Assets.GetTexture("HeroRunBackwards"), 40); runBackwardsLeft.StartedCallback = () => { if (CurrentWeapon == null) { return; } if (CurrentWeapon is Shotgun) { (CurrentWeapon as Handgun).SetLeftFacingOffset(new Vector2(-3, -17)); (CurrentWeapon as Handgun).SetRightFacingOffset(new Vector2(3, -17)); } else if (CurrentWeapon is Machinegun) { (CurrentWeapon as Handgun).SetLeftFacingOffset(new Vector2(-3, -17)); (CurrentWeapon as Handgun).SetRightFacingOffset(new Vector2(3, -17)); } else if (CurrentWeapon is Handgun) { (CurrentWeapon as Handgun).SetLeftFacingOffset(new Vector2(1, -17)); (CurrentWeapon as Handgun).SetRightFacingOffset(new Vector2(-1, -17)); } }; animations.RegisterAnimation("RunBackwardsLeft", runBackwardsLeft, () => Velocity.X > 0 && CurrentFaceDirection == Direction.WEST); SpriteSheetAnimation runRighrunBackwardsRight = runBackwardsLeft.CopyFlipped(); animations.RegisterAnimation("RunBackwardsRight", runRighrunBackwardsRight, () => Velocity.X < 0 && CurrentFaceDirection == Direction.EAST); SpriteSheetAnimation jumpLeft = new SpriteSheetAnimation(this, Assets.GetTexture("HeroRun"), 1); jumpLeft.StartFrame = 6; jumpLeft.EndFrame = 7; animations.RegisterAnimation("JumpLeft", jumpLeft, () => !IsOnGround && CurrentFaceDirection == Direction.WEST, 1); SpriteSheetAnimation jumpRight = jumpLeft.CopyFlipped(); animations.RegisterAnimation("JumpRight", jumpRight, () => !IsOnGround && CurrentFaceDirection == Direction.EAST, 1); SpriteSheetAnimation jetpackLeft = new SpriteSheetAnimation(this, Assets.GetTexture("Jetpacking"), 40); animations.RegisterAnimation("JetpackLeft", jetpackLeft, () => flying && CurrentFaceDirection == Direction.WEST, 2); SpriteSheetAnimation jetpackRight = jetpackLeft.CopyFlipped(); animations.RegisterAnimation("JetpackRight", jetpackRight, () => flying && CurrentFaceDirection == Direction.EAST, 2); SpriteSheetAnimation kickLeft = new SpriteSheetAnimation(this, Assets.GetTexture("Kick"), 40); kickLeft.Looping = false; kickLeft.AddFrameAction(5, (frame) => { AudioEngine.Play("Kick"); IsKicking = true; if (THIS_IS_SPARTAAAAA && collidingWith.Count > 0) { THIS_IS_SPARTAAAAA = false; Vector2 canSpawnPos = collidingWith[0].Transform.Position; FuelCan can = new FuelCan(scene, canSpawnPos + new Vector2(0, -20), TankCapacity); can.Velocity += new Vector2(-3, -1); can.CollisionsEnabled = false; Ammo ammo = new Ammo(scene, canSpawnPos + new Vector2(0, -20), 20, typeof(Handgun)); ammo.Velocity += new Vector2(-4f, -1.5f); ammo.CollisionsEnabled = false; Timer.TriggerAfter(2000, () => { can.CollisionsEnabled = true; ammo.CollisionsEnabled = true; new TextPopup(Scene, Assets.GetTexture("FuelAmmoText"), Transform.Position + new Vector2(-20, -80), 0.3f, 3000); }); } }); kickLeft.AddFrameAction(6, (frame) => { IsKicking = false; }); kickLeft.StartedCallback = () => { if (THIS_IS_SPARTAAAAA && collidingWith.Count > 0) { THIS_IS_SPARTA(); Globals.FixedUpdateMultiplier = 0.1f; Timer.TriggerAfter(3000, () => { Globals.FixedUpdateMultiplier = 0.5f; Timer.Repeat(1000, (elapsedTime) => { Scene.Camera.Zoom -= 0.002f * elapsedTime; }); }); } //IsKicking = true; if (IsOnGround) { MovementSpeed = 0; } if (CurrentWeapon == null) { return; } if (CurrentWeapon is Shotgun) { (CurrentWeapon as Handgun).SetLeftFacingOffset(new Vector2(-3, -17)); (CurrentWeapon as Handgun).SetRightFacingOffset(new Vector2(3, -17)); } else if (CurrentWeapon is Machinegun) { (CurrentWeapon as Handgun).SetLeftFacingOffset(new Vector2(-3, -17)); (CurrentWeapon as Handgun).SetRightFacingOffset(new Vector2(3, -17)); } else if (CurrentWeapon is Handgun) { (CurrentWeapon as Handgun).SetLeftFacingOffset(new Vector2(1, -17)); (CurrentWeapon as Handgun).SetRightFacingOffset(new Vector2(-1, -17)); } }; kickLeft.AnimationSwitchCallback = () => { IsKicking = false; MovementSpeed = Config.CHARACTER_SPEED; foreach (AbstractEnemy enemy in collidingWith) { enemy.IsKicked = false; } }; animations.RegisterAnimation("KickLeft", kickLeft, () => false); SpriteSheetAnimation kickRight = kickLeft.CopyFlipped(); animations.RegisterAnimation("KickRight", kickRight, () => false); SpriteSheetAnimation kickJetpackLeft = new SpriteSheetAnimation(this, Assets.GetTexture("KickJetpacking"), 40); kickJetpackLeft.Looping = false; kickJetpackLeft.AddFrameAction(5, (frame) => { AudioEngine.Play("Kick"); IsKicking = true; }); kickJetpackLeft.AddFrameAction(6, (frame) => { IsKicking = false; }); kickJetpackLeft.StartedCallback = () => { //IsKicking = true; if (CurrentWeapon == null) { return; } if (CurrentWeapon is Shotgun) { (CurrentWeapon as Handgun).SetLeftFacingOffset(new Vector2(-3, -17)); (CurrentWeapon as Handgun).SetRightFacingOffset(new Vector2(3, -17)); } else if (CurrentWeapon is Machinegun) { (CurrentWeapon as Handgun).SetLeftFacingOffset(new Vector2(-3, -17)); (CurrentWeapon as Handgun).SetRightFacingOffset(new Vector2(3, -17)); } else if (CurrentWeapon is Handgun) { (CurrentWeapon as Handgun).SetLeftFacingOffset(new Vector2(1, -17)); (CurrentWeapon as Handgun).SetRightFacingOffset(new Vector2(-1, -17)); } }; kickJetpackLeft.AnimationSwitchCallback = () => { IsKicking = false; }; animations.RegisterAnimation("KickJetpackLeft", kickJetpackLeft, () => false); SpriteSheetAnimation kickJetpackRight = kickJetpackLeft.CopyFlipped(); animations.RegisterAnimation("KickJetpackRight", kickJetpackRight, () => false); animations.AddFrameTransition("RunLeft", "RunBackwardsLeft"); animations.AddFrameTransition("RunRight", "RunBackwardsRight"); animations.AddFrameTransition("RunLeft", "RunRight"); animations.AddFrameTransition("RunBackwardsLeft", "RunBackwardsRight"); animations.AddFrameTransition("JetpackLeft", "JetpackRight"); Visible = true; Active = true; /*DebugFunction = () => * { * return "Fuel: " + (int)(((float)(Fuel / TankCapacity)) * 100) + " %"; * };*/ CurrentWeapon = new Handgun(scene, this); weapons.Add(CurrentWeapon); Machinegun mg = new Machinegun(scene, this); mg.Visible = false; Shotgun sg = new Shotgun(scene, this); sg.Visible = false; weapons.Add(mg); weapons.Add(sg); }