public void ThrowItem(XYZ target, IItem item) { if (Main.CalcAim2(target - p.Position, 60, out XYZ lower, out XYZ _)) { item.Velocity = lower / Constants.STEPS_PER_SECOND; //Remove the item from the player's inventory and create a thrown item in the world p.Inventory.Remove(item); var t = new ThrownItem(p, item); w.AddEntity(t); //Track this on the player p.Watch.Add(t); p.AddMessage(new InfoEvent(new ColoredString("You throw: ") + item.Name.WithBackground(Color.Black))); } }
public void ThrowHeldItem() { Vector3 ThrowDirection; ThrowDirection = GetComponent <MovementPlayerCharacter>().totaldirection; GameObject HeldItem; HeldItem = GetComponent <InteractPlayerCharacter>().Holding; if (HeldItem != null) { GameObject ThrownItem; int xdirection = 1; ThrownItem = HeldItem; if (ThrowDirection.x < 0) { xdirection = -1; } else if (ThrowDirection.x == 0) { xdirection = 0; } GetComponent <InteractPlayerCharacter>().Holding = null; ThrownItem.GetComponent <Rigidbody2D>().isKinematic = false; ThrownItem.GetComponent <Collider2D>().enabled = true; ThrownItem.GetComponent <Rigidbody2D>().AddForce((ThrownItem.transform.position - transform.position) * 1000); //ThrownItem.GetComponent<Rigidbody2D>().AddForce(new Vector2(600* xdirection, ((200 * ThrowDirection.y) + 300))); Debug.Log("Threw " + ThrownItem.name); } }
public GameObject DropItem(Vector2 position) { return(ThrownItem.Spawn(this, position, Vector2.zero).gameObject); }
// Return an instance of the type's archetypal class. public static Item FromType(Types type) { Item ret = null; MeleeWeapon mw; ProjectileWeapon pw; ThrownItem ti; SpellCaster sc; ItemData dat; RestorationSpell rs; switch (type) { case Types.Hand: ret = new MeleeWeapon() as Item; break; case Types.Rifle: ret = new ProjectileWeapon() as Item; break; case Types.Bullet: ret = new Projectile() as Item; ret.stackable = true; break; case Types.HealthPack: ret = new HealthPowerUp() as Item; break; case Types.AmmoPack: ret = new AmmoPowerUp() as Item; break; case Types.Ammo: ret = new Item(); ret.stackable = true; break; case Types.AidHealthPack: ret = new HealthAid() as Item; break; case Types.Spear: ti = new ThrownItem(); ti.healthDamage = 100; ti.staminaCost = 25; ret = ti as Item; ret.color = new Vector3(0, 1, 0); break; case Types.Claws: mw = new MeleeWeapon(); mw.healthDamage = 50; mw.swingSpeed = 0.5f; mw.staminaCost = 15; ret = mw as Item; break; case Types.Staff: List <Types> spells = Session.session.career.GetAvailableSpells(); sc = new SpellCaster(spells); ret = sc as Item; break; case Types.Crossbow: pw = new ProjectileWeapon(); pw.healthDamage = 100; pw.maxAmmo = 1; pw.ammoType = "Bolt"; pw.impulseStrength = 100f; dat = new ItemData(); dat.type = Types.Ammo; dat.name = pw.ammoType; pw.LoadInternalReserve(dat, 10); ret = pw as Item; break; case Types.RapidCrossbow: pw = new ProjectileWeapon(); pw.healthDamage = 100; pw.maxAmmo = 1; pw.ammoType = "Bolt"; pw.impulseStrength = 100f; pw.reloadDelay = 0.5f; // Four times faster dat = new ItemData(); dat.type = Types.Ammo; dat.name = pw.ammoType; pw.LoadInternalReserve(dat, 10); ret = pw as Item; break; case Types.DoubleCrossbow: pw = new ProjectileWeapon(); pw.healthDamage = 100; pw.maxAmmo = 2; pw.ammoType = "Bolt"; pw.impulseStrength = 100f; dat = new ItemData(); dat.type = Types.Ammo; dat.name = pw.ammoType; pw.LoadInternalReserve(dat, 10); ret = pw as Item; break; case Types.FlintlockPistol: pw = new ProjectileWeapon(); pw.healthDamage = 100; pw.maxAmmo = 1; pw.ammoType = "MusketBall"; pw.impulseStrength = 100f; dat = new ItemData(); dat.type = Types.Ammo; dat.name = pw.ammoType; pw.LoadInternalReserve(dat, 6); ret = pw as Item; break; case Types.Knife: mw = new MeleeWeapon(); mw.healthDamage = 35; mw.swingSpeed = 0.5f; mw.staminaCost = 15; ret = mw as Item; break; case Types.FireballSpell: pw = new ProjectileWeapon(); pw.healthDamage = 30; pw.requireAmmoToFire = false; pw.ammoType = "Fireball"; pw.manaCost = 15; dat = new ItemData(); dat.type = Types.Ammo; dat.name = pw.ammoType; pw.LoadInternalReserve(dat, 1); ret = pw as Item; break; case Types.FireballIISpell: pw = new ProjectileWeapon(); pw.healthDamage = 60; pw.requireAmmoToFire = false; pw.ammoType = "Fireball"; pw.manaCost = 20; dat = new ItemData(); dat.type = Types.Ammo; dat.name = pw.ammoType; pw.LoadInternalReserve(dat, 1); ret = pw as Item; break; case Types.FireballIIISpell: pw = new ProjectileWeapon(); pw.healthDamage = 100; pw.requireAmmoToFire = false; pw.ammoType = "Fireball"; pw.manaCost = 50; dat = new ItemData(); dat.type = Types.Ammo; dat.name = pw.ammoType; pw.LoadInternalReserve(dat, 1); ret = pw as Item; break; case Types.HealSpell: rs = new RestorationSpell(); rs.health = 15; rs.manaCost = 50; rs.coolDown = 1f; ret = rs as Item; break; case Types.StaminaSpell: rs = new RestorationSpell(); rs.stamina = 15; rs.manaCost = 15; rs.coolDown = 0.5f; ret = rs as Item; break; case Types.ManaSpell: rs = new RestorationSpell(); rs.mana = 25; rs.manaCost = 15; rs.coolDown = 0.5f; ret = rs as Item; break; } return(ret); }
// Update is called once per frame void Update() { if (Input.GetKeyDown(KeyCode.K)) { DebugInventory(); } //Drop item if (Input.GetKeyDown(PlayerData.Instance.keybinds["Drop Item"]) && mouseInventory.inventoryReadOnly[0] != null) { ItemEntityPool.Instance.CreateItemEntity(Camera.main.ScreenToWorldPoint(Input.mousePosition), mouseInventory.inventoryReadOnly[0].item); mouseInventory.DecrementStack(0); } //Fire weapon if (Input.GetKey(PlayerData.Instance.keybinds["Fire Weapon"])) { fireWeapon = true; } if (Input.GetKeyDown(PlayerData.Instance.keybinds["Rotate Buildable"])) { placementRotation--; if (placementRotation < 0) { placementRotation = 7; } } if (!UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject()) { Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y); RaycastHit2D[] hits = new RaycastHit2D[1]; Physics2D.Raycast(mousePos2D, Vector2.zero, filterMining, hits); if (mouseInventory.inventoryReadOnly[0] != null) { ItemThrowable throwable = mouseInventory.inventoryReadOnly[0].item as ItemThrowable; if (throwable != null && Input.GetMouseButtonDown(0)) //thrown item code { Vector3 place = Camera.main.ScreenToWorldPoint(Input.mousePosition); ThrownItem thrown = Instantiate(throwable.thrownResult, this.transform.position, Quaternion.identity).GetComponent <ThrownItem>(); thrown.SetTarget(place); mouseInventory.DecrementStack(0); } else if (mouseInventory.inventoryReadOnly[0].item.placeableResult != null) //place building code { Vector2 size; IRotate rotateable; if (mouseInventory.inventoryReadOnly[0].item.placeableResult.TryGetComponent(out rotateable)) { placeSpriteRenderer.sprite = rotateable.GetRotatedPlaceSprite(placementRotation); size = rotateable.GetRotatedBoundingBox(placementRotation); } else { placeSpriteRenderer.sprite = mouseInventory.inventoryReadOnly[0].item.placeableResult.GetComponent <SpriteRenderer>().sprite; size = mouseInventory.inventoryReadOnly[0].item.placeableResult.GetComponent <BoxCollider2D>().size; } Vector3 place = Camera.main.ScreenToWorldPoint(Input.mousePosition); Vector3 placeRounded = new Vector3(Mathf.Round(place.x), Mathf.Round(place.y), 0); Vector3 placeVector = placeGrid.WorldToCell(placeRounded); size = new Vector2(Mathf.CeilToInt(size.x), Mathf.CeilToInt(size.y)); Vector3 offset = new Vector3(0, 0); if (size.x % 2 != 0) { offset.x = 0.5f; } if (size.y % 2 != 0) { offset.y = 0.5f; } placeSprite.transform.position = placeVector - offset; BuildingManager buildingManager; if (rotateable != null && Physics2D.OverlapBox(placeVector - offset, size - new Vector2(0.1f, 0.1f), 0.0f, filter, colliders) == 0) { rotateable.GetRotatedPlaceSprite(placementRotation); placeSpriteRenderer.color = new Color(0, 1, 0, 0.25f); if (Input.GetMouseButton(0)) { GameObject building = Instantiate(mouseInventory.inventoryReadOnly[0].item.placeableResult, placeVector - offset, Quaternion.identity); building.GetComponent <Health>().alliance = characterAlliance.allianceCode; building.GetComponent <IRotate>().InitializeRotated(placementRotation); DestroyGrassAt(placeVector - offset, size); mouseInventory.DecrementStack(0); UpdateLoaders(building.transform.position, size); } } else if (mouseInventory.inventoryReadOnly[0].item.placeableResult.TryGetComponent(out buildingManager)) { Physics2D.OverlapBox(placeVector - offset, size - new Vector2(0.1f, 0.1f), 0.0f, filterMining, collidersList); List <Mineable> mineables; if (buildingManager.CheckPlacement(collidersList, out mineables, placeVector - offset)) { placeSpriteRenderer.color = new Color(0, 1, 0, 0.25f); if (Input.GetMouseButton(0)) { GameObject building = Instantiate(mouseInventory.inventoryReadOnly[0].item.placeableResult, placeVector - offset, Quaternion.identity); building.GetComponent <CraftingManagerFixed>().SetMineables(mineables); building.GetComponent <Health>().alliance = characterAlliance.allianceCode; DestroyGrassAt(placeVector - offset, size); mouseInventory.DecrementStack(0); UpdateLoaders(building.transform.position, size); } } else { placeSpriteRenderer.color = new Color(1, 0, 0, 0.25f); } } else if (Physics2D.OverlapBox(placeVector - offset, size - new Vector2(0.1f, 0.1f), 0.0f, filter, colliders) == 0) { placeSpriteRenderer.color = new Color(0, 1, 0, 0.25f); if (Input.GetMouseButton(0)) { GameObject building = Instantiate(mouseInventory.inventoryReadOnly[0].item.placeableResult, placeVector - offset, Quaternion.identity); building.GetComponent <Health>().alliance = characterAlliance.allianceCode; DestroyGrassAt(placeVector - offset, size); mouseInventory.DecrementStack(0); UpdateLoaders(building.transform.position, size); } } else { placeSpriteRenderer.color = new Color(1, 0, 0, 0.25f); } } else if (mouseInventory.inventoryReadOnly[0].item.placeableResult == null) { placeSpriteRenderer.color = new Color(1, 1, 1, 0); } } else { placeSpriteRenderer.color = new Color(1, 1, 1, 0); } if (hits[0].collider != null) { #region Left Click Interaction if (Input.GetMouseButtonDown(0)) { Inventory inventory = null; CraftingManagerRecipe craftingManagerRecipe = null; CraftingManagerFixed craftingManagerFixed = null; IItemFilter itemFilter = null; if (hits[0].collider.gameObject.TryGetComponent <CraftingManagerRecipe>(out craftingManagerRecipe)) { filterItemUI.SetFilterFocus(null); machineUI.SetLinkedMachine(hits[0].collider.gameObject); inventoryInteractUI.SetViewedInventory(null); } else if (hits[0].collider.gameObject.TryGetComponent <CraftingManagerFixed>(out craftingManagerFixed)) { filterItemUI.SetFilterFocus(null); machineUI.SetLinkedMachine(hits[0].collider.gameObject); inventoryInteractUI.SetViewedInventory(null); } else if (hits[0].collider.gameObject.TryGetComponent <Inventory>(out inventory)) { filterItemUI.SetFilterFocus(null); inventoryInteractUI.SetViewedInventory(inventory); machineUI.SetLinkedMachine(null); } else if (hits[0].collider.gameObject.TryGetComponent <IItemFilter>(out itemFilter)) { filterItemUI.SetFilterFocus(itemFilter); machineUI.SetLinkedMachine(null); inventoryInteractUI.SetViewedInventory(null); } else { inventoryInteractUI.SetViewedInventory(null); machineUI.SetLinkedMachine(null); } } #endregion Left Click Interaction #region Right Click Interaction if (Input.GetMouseButtonDown(1)) { Mineable mineable; if (hits[0].collider.TryGetComponent(out mineable)) { currentMineable = mineable; } } if (currentMineable != null) { Mineable mineable; if (hits[0].collider.TryGetComponent(out mineable)) { if (!ReferenceEquals(currentMineable, mineable)) { miningProgress = 0; currentMineable = mineable; } } } #endregion Right Click Interaction } else { currentMineable = null; } } else { placeSpriteRenderer.color = new Color(1, 1, 1, 0); } if (Input.GetKeyDown(PlayerData.Instance.keybinds["Change Ammo"])) { selectedAmmoSlot++; if (selectedAmmoSlot >= ammoInventory.inventoryReadOnly.Count) { selectedAmmoSlot = 0; } ammoSlotSelected.SetHighlightedSlot(selectedAmmoSlot); } }