void OnTriggerEnter2D(Collider2D col) { Tds_Tile vTile = col.GetComponent <Tds_Tile>(); Tds_Loot vLoot = col.GetComponent <Tds_Loot>(); if (vTile != null) { //ONLY refresh variable current level vListCollider.Add(vTile); //make the player refresh it's pixel tiles variables if (vCharacter != null) { vCharacter.RefreshVariables(vListCollider); } } if (vLoot != null) { //make the player refresh it's pixel tiles variables if (vCharacter != null) { vCharacter.RefreshLoot(vLoot); } } }
public void RefreshLoot(Tds_Loot vLoot) { //show the loot items on ground if (vLoot != null) { vLoot.ShowHide(true); } else if (vCurLoot != null) { vCurLoot.ShowHide(false); } //check loot vCurLoot = vLoot; if (vLoot != null && IsPlayer) { LootNearby = true; vGameManager.vPressSpaceObj.SetActive(true); } else if (IsPlayer) { LootNearby = false; vGameManager.vPressSpaceObj.SetActive(false); } }
void OnTriggerExit2D(Collider2D col) { Tds_Tile vTile = col.GetComponent <Tds_Tile>(); Tds_Loot vLoot = col.GetComponent <Tds_Loot>(); //check if we have it on the list so we can remove it if (vTile != null) { if (vListCollider.Contains(vTile)) { vListCollider.Remove(vTile); //make the player refresh it's pixel tiles variables vCharacter.RefreshVariables(vListCollider); } } else if (vLoot != null) { vLoot = null; vCharacter.RefreshLoot(vLoot); } }
// Update is called once per frame void Update() { //check if the character is ready if (vGameManager != null && IsAlive) { if (vGameManager.IsReady) { //get it's weapon ONLY when the game start if (!GameStarted) { GameStarted = true; ChangeWeapon(); } //check if attacking bool IsAttacking = false; Vector3 vTargetPosition = Camera.main.WorldToScreenPoint(vMainPlayer.transform.position); //reduce the waiting time for the next bullet! if (!CanAttack && ListWeapons [vCurWeapIndex].vTimeWaited > 0f) { ListWeapons [vCurWeapIndex].vTimeWaited -= Time.deltaTime; //check if we waited enought if (ListWeapons [vCurWeapIndex].vTimeWaited <= 0f) { CanAttack = true; } } if (IsPlayer) { //player get mouse position instead of its' own position vTargetPosition = Input.mousePosition; //calculate how far is the cursor from the player Vector3 v3Pos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0f); v3Pos = Camera.main.ScreenToWorldPoint(v3Pos); //prevent the player to rotate on itself when the cursor is above him if (Vector3.Distance(new Vector3(v3Pos.x, v3Pos.y, 0f), new Vector3(transform.position.x, transform.position.y, 0f)) <= vGameManager.vCursorRange) { CursorIsNear = true; } else { CursorIsNear = false; } if (Input.GetAxis("Vertical") > 0 || Input.GetAxis("Vertical") < 0 || Input.GetAxis("Horizontal") > 0 || Input.GetAxis("Horizontal") < 0) { IsWalking = true; } else { IsWalking = false; } //check if we changed walking status if (IsWalking != LastWalkingStatus) { LastWalkingStatus = IsWalking; vLegAnimator.SetBool("IsWalking", IsWalking); } if (vAimIcon != null && vCurrentIcon == null) { vCurrentIcon = Instantiate(vAimIcon); } else if (vCurrentIcon != null) { Vector3 pz = Camera.main.ScreenToWorldPoint(Input.mousePosition); pz.z = 0; pz.x -= 0.1f; pz.y -= 0.2f; vCurrentIcon.transform.position = pz; } if (Input.GetMouseButton(0)) { IsAttacking = true; } //check if the user want to change weapon if (Input.GetAxis("Mouse ScrollWheel") > 0) { GetNextWeapon(1); } else if (Input.GetAxis("Mouse ScrollWheel") < 0) { GetNextWeapon(-1); } //reduce it's time if (IsReloading && TimeToReload > 0f) { TimeToReload -= Time.deltaTime; RefreshWeaponUI(); } //get the item if (LootNearby && Input.GetKeyDown("space") && vCurLoot != null) { //check if we already have the weapon and show it bool HasAlreadyWeapon = false; //check if looting give a weapon if (vCurLoot.vItems.GiveWeapon) { //give the right weapon foreach (Tds_Weapons vCurWeapon in vGameManager.vWeaponList) { if (vCurLoot.vItems.vWeaponName.ToString() == vCurWeapon.vWeaponName.ToString()) { foreach (Tds_Weapons vWeapon in ListWeapons) { if (vWeapon.vWeaponName == vCurWeapon.vWeaponName) { HasAlreadyWeapon = true; } } if (!HasAlreadyWeapon) { //get a copy of this weapon Tds_Weapons vNewWeapon = CopyWeapon(vCurWeapon); //new weapon is already recharge vNewWeapon.vAmmoCur = vNewWeapon.vAmmoSize; //found the weapon, add it for the player ListWeapons.Add(vNewWeapon); } } } } if (vGameManager.vItemLootedAnim != null) { //show the itemlooted going up GameObject vNewObj = vGameManager.vItemLootedAnim; //enable it for the very first use vNewObj.SetActive(true); Text vLabel = vNewObj.transform.Find("Label").GetComponent <Text> (); //show different message if (HasAlreadyWeapon) { vLabel.text = "Already have :"; vLabel.color = Color.red; } else { vLabel.color = Color.white; vLabel.text = "Received :"; //change info on it } //change info on it Text vText = vNewObj.transform.Find("ItemName").GetComponent <Text> (); //show the right item name vText.text = vCurLoot.vItems.vName; //default color vText.color = Color.green; if (vCurLoot.vItems.vDmgType == WeaponValueType.Average) { vText.color = Color.yellow; } else if (vCurLoot.vItems.vDmgType == WeaponValueType.High) { vText.color = Color.red; } else if (vCurLoot.vItems.vDmgType == WeaponValueType.GODLY) { vText.color = new Color(255f, 0f, 195f, 0f); } //reshow the whole animation vNewObj.GetComponent <Animator> ().SetTrigger("Show"); } //destroy loot if (!HasAlreadyWeapon) { GameObject.Destroy(vCurLoot.gameObject); RefreshWeaponUI(); } //clear loot vCurLoot = null; } } else { float vDistance = Vector2.Distance(vMainPlayer.transform.position, transform.position); ///////////////AI//////////////// if ((vDistance <= 20f || IsAggro) && vDistance >= vGameManager.vMeleeRange) { IsWalking = true; IsChasing = true; SeePlayer = true; IsAggro = true; } else { //if (vDistance >= vGameManager.vMeleeRange && IsChasing) IsWalking = false; //check if can Melee Attack if (IsChasing && vDistance <= vGameManager.vMeleeRange) { CanMelee = true; IsAttacking = true; } else { CanMelee = false; } } ///////////////////////////////// } //check if we shoot if (IsAttacking && CanAttack) { //Melee (don't use any ammo) if (ListWeapons [vCurWeapIndex].vWeaponType == WeaponType.Melee && CanMelee) { //get the amount of time to wait until we can shoot again ListWeapons [vCurWeapIndex].vTimeWaited = ListWeapons [vCurWeapIndex].vTimeBtwShot; //prevent from shooting too many time and wait for the animation to be done CanAttack = false; //animate the hand if (ListWeapons [vCurWeapIndex].AttackAnimationUsed != "") { vBodyAnimator.SetTrigger(ListWeapons [vCurWeapIndex].AttackAnimationUsed); } } else { //RANGED //check if has enought ammo if (ListWeapons [vCurWeapIndex].vAmmoCur > 0) { //get the amount of time to wait until we can shoot again ListWeapons [vCurWeapIndex].vTimeWaited = ListWeapons [vCurWeapIndex].vTimeBtwShot; //prevent from shooting too many time and wait for the animation to be done CanAttack = false; //reduce the ammo by 1 ListWeapons [vCurWeapIndex].vAmmoCur--; //animate the hand if (ListWeapons [vCurWeapIndex].AttackAnimationUsed != "") { vBodyAnimator.SetTrigger(ListWeapons [vCurWeapIndex].AttackAnimationUsed); } //create the shot FX GameObject vShotFX = Instantiate(ListWeapons [vCurWeapIndex].vShotFX); vShotFX.transform.position = CurWeaponObj.transform.Find("BulletPos").position; //create the projectile on the aim obj IF EXIST if (ListWeapons [vCurWeapIndex].vProjectile != null) { //create as many shot with the specific angle foreach (float vAngle in ListWeapons[vCurWeapIndex].vBulletAngleList) { //create the projectile GameObject vNewProj = Instantiate(ListWeapons [vCurWeapIndex].vProjectile); vNewProj.transform.position = CurWeaponObj.transform.Find("BulletPos").position; //calculate the new angle for every shot Quaternion vtemp = CurWeaponObj.transform.rotation; vtemp.z += vAngle; vNewProj.transform.rotation = vtemp; //send to the projectile everything it need to kill Tds_Projectile vProj = vNewProj.GetComponent <Tds_Projectile> (); vGameManager.vProjectileList.Add(vProj); vProj.vProjFactionType = vFactionType; vProj.Speed = ListWeapons [vCurWeapIndex].vProjectileSpeed; vProj.vDmg = ListWeapons [vCurWeapIndex].vDmg; vProj.vGameManager = vGameManager; vProj.vRebounce = ListWeapons [vCurWeapIndex].Rebounce; vProj.vImpactFX = ListWeapons [vCurWeapIndex].vImpactFX; vProj.IsReady = true; } } } else { if (IsReloading == false) { IsReloading = true; //time until we have fully reloaded TimeToReload = 1f; //recharging animation // then we wait until the animation is complete and tell the character we have ammo! if (vBodyAnimator != null) { vBodyAnimator.SetTrigger("Reload"); } } } //refresh all the weapon on top if (IsPlayer) { RefreshWeaponUI(); } } } //rotate weapon if have one Vector3 vBodyPosition = vLeftHandObj.transform.position; //calcualte the angle Vector3 pos = Camera.main.WorldToScreenPoint(vBodyPosition); Vector3 dir = vTargetPosition - pos; Quaternion newRotation = Quaternion.LookRotation(dir, Vector3.back); newRotation.x = 0f; newRotation.y = 0f; //check if walking if (IsWalking) { //initialise variable bool vMoveUP = false; bool vMoveRight = false; bool vMoveLeft = false; bool vMoveDown = false; if (IsPlayer) { if (Input.GetAxis("Vertical") > 0 && !Input.GetButtonUp("Vertical")) { vMoveUP = true; } if (Input.GetAxis("Vertical") < 0 && !Input.GetButtonUp("Vertical")) { vMoveDown = true; } if (Input.GetAxis("Horizontal") > 0 && !Input.GetButtonUp("Horizontal")) { vMoveRight = true; } if (Input.GetAxis("Horizontal") < 0 && !Input.GetButtonUp("Horizontal")) { vMoveLeft = true; } } else { //shorten variables float vX = transform.position.x; float vY = transform.position.y; //NPC if (vX <= vMainPlayer.transform.position.x /*&& vXValue*/) { vMoveRight = true; } else //if (vXValue) { vMoveLeft = true; } if (vY <= vMainPlayer.transform.position.y /*&& vYValue*/) { vMoveUP = true; } else // if (vYValue) { vMoveDown = true; } } //check which position we are rotating if (vMoveUP && vMoveRight) { CurWalkDirection = WalkDirection.RightUp; } else if (vMoveUP && vMoveLeft) { CurWalkDirection = WalkDirection.LeftUp; } else if (vMoveDown && vMoveLeft) { CurWalkDirection = WalkDirection.LeftDown; } else if (vMoveDown && vMoveRight) { CurWalkDirection = WalkDirection.RightDown; } else if (vMoveUP) { CurWalkDirection = WalkDirection.Up; } else if (vMoveLeft) { CurWalkDirection = WalkDirection.Left; } else if (vMoveDown) { CurWalkDirection = WalkDirection.Down; } else if (vMoveRight) { CurWalkDirection = WalkDirection.Right; } //calculate the new rotation Vector3 temp = transform.rotation.eulerAngles; temp.x = 0f; temp.y = 0f; temp.z = GetWalkingRotation(); transform.rotation = Quaternion.Euler(temp); //play leg animation backward float vBackForward = 1; if (vMoveDown) { vBackForward = -1; } //show the animation on the right direction vLegAnimator.SetFloat("Direction", vBackForward); //check where it's heading Vector2 vDestination = new Vector2(0f, 1f) * WalkSpeed * Time.deltaTime; //move the character in this direction if (CanWalk) { transform.Translate(vDestination); } } //rotate the body correctly //can only look at the player if he can see it if (CanRotateBody()) { if (IsPlayer) { vBodyObj.transform.rotation = Quaternion.Slerp(vBodyObj.transform.rotation, newRotation, 1000f); } else { vBodyObj.transform.rotation = Quaternion.Slerp(vBodyObj.transform.rotation, newRotation, Time.deltaTime); } } vCamObj.transform.rotation = CamStartRotation; } } }