public override void Update(MainGameState game, float time) { float playerPosRelX = game.CurrentPlayer.X - X; float playerPosRelZ = game.CurrentPlayer.Z - Z; if (Altitude == 0f && playerPosRelX * playerPosRelX + playerPosRelZ * playerPosRelZ < .5f * .5f) { Weapon drop = GetDropped(); if (drop.MaxStack <= 1) { for (int i = 0; i < Player.InventorySize; ++i) { if (game.CurrentPlayer.GetInventoryItem(i) == null) { game.CurrentPlayer.SetInventoryItem(i, drop); Sounds.PickupWeapon.Play(); ShouldBeRemoved = true; return; } } } else { for (int i = 0; i < Player.InventorySize; ++i) { InventoryStack iStack = game.CurrentPlayer.GetInventoryItem(i) as InventoryStack; // thanks novynn for pointing out the silly != / == mistake! if (iStack != null && iStack.GetItemType() == drop.GetType() && iStack.Amount != iStack.MaxValue) { ++iStack.Amount; Sounds.PickupWeapon.Play(); ShouldBeRemoved = true; return; } } for (int i = 0; i < Player.InventorySize; ++i) { if (game.CurrentPlayer.GetInventoryItem(i) == null) { game.CurrentPlayer.SetInventoryItem(i, new DroppedItemInventoryStack(drop.GetType(), drop.MaxStack)); Sounds.PickupWeapon.Play(); ShouldBeRemoved = true; return; } } } } UpdateGravity(time); if (Altitude != 0f) { Vector2 movement = new Vector2(XDir * time * 3f, ZDir * time * 3f); Vector2 finalPosition = new Vector2(X, Z) + movement; Block b = game.BlockGrid[(int)finalPosition.X, (int)finalPosition.Y]; if (b == null || !b.CollidesWithVector(finalPosition)) { X = finalPosition.X; Z = finalPosition.Y; } } }
public static void Restore(MainGameState world) { if (last == null) { world.CurrentPlayer = null; world.LoadLevel(world.CurrentLevel); } else { world.Enemies.Clear(); foreach (Enemy e in last.enemies) { world.Enemies.Add(new Enemy(e.X, e.Z)); } world.HealthPacks.Clear(); // TODO: take healthpack timer into account foreach (HealthPack hp in last.healthPack) { world.HealthPacks.Add(new HealthPack(hp.X, hp.Z)); } world.Crates.Clear(); // TODO: idem foreach (AmmoCrate ac in last.crates) { world.Crates.Add(new AmmoCrate(ac.X, ac.Z)); } world.Blocks.Clear(); world.BlockGrid = new Block[world.WorldWidth, world.WorldHeight]; for (int x = 0; x < world.WorldWidth; ++x) { for (int z = 0; z < world.WorldWidth; ++z) { Block oldBlock = last.blockGrid [x, z]; Door oldDoor = oldBlock as Door; if (oldBlock != null) { Block newBlock = null; if (oldDoor != null) { // TODO: take full door texture into account Door newDoor = new Door(oldDoor.X, oldDoor.Z, TextureTools.TextureDoor1, oldDoor.Upper, oldDoor.Lower); if (oldDoor.IsOpen) { newDoor.Open(); } newBlock = newDoor; } else { newBlock = new Block(oldBlock.X, oldBlock.Z, oldBlock.Wall1.Texture); } world.BlockGrid [x, z] = newBlock; world.Blocks.Add(newBlock); } } } world.DroppedWeapons.Clear(); foreach (DroppedWeapon weapon in last.dropped) { Weapon droppedWeapon = weapon.GetDropped(); world.DroppedWeapons.Add(new InventoryDroppedWeapon(droppedWeapon, weapon.X, weapon.Z, 0f, 0f)); } for (int i = 0; i < world.CurrentPlayer.Inventory.Length; ++i) { InventoryItem current = last.playerInventory [i]; if (current != null) { InventoryStack stack = current as InventoryStack; if (stack != null) { DroppedItemInventoryStack newStack = new DroppedItemInventoryStack(stack.GetItemType(), stack.MaxValue); newStack.Amount = stack.Amount; world.CurrentPlayer.Inventory [i] = newStack; } else { world.CurrentPlayer.Inventory [i] = (InventoryItem)Activator.CreateInstance(current.GetType()); Gun g = current as Gun; if (g != null) { ((Gun)world.CurrentPlayer.Inventory [i]).Ammo = g.Ammo; ((Gun)world.CurrentPlayer.Inventory [i]).MagazinesLeft = g.MagazinesLeft; } } } } world.CurrentPlayer.X = last.xPlayer; world.CurrentPlayer.Z = last.zPlayer; world.CurrentPlayer.Health = last.healthPlayer; } }