// Update is called once per frame void Update() { if (target != null) { Character_BS character = target.GetComponent <Character_BS>(); if (character != null) { if (character.State == Character_BS.AnimState.Loot) { if (character.pickUp(this)) { transform.SetParent(target.transform); transform.position = target.transform.position; SpriteRenderer sr = gameObject.GetComponent <SpriteRenderer>(); coldr.enabled = false; sr.enabled = false; } else { // couldnt carry it, dont pick it up } } } } }
// function to launch the bullet public void Shoot(Vector2 Direction) { float minR = -weapon.recoil; float maxR = weapon.recoil; // recoil (random direction) Character_BS sChar = shooter.GetComponent <Character_BS>(); if (shooter != null && sChar != null) { // triple recoil if the shooter is moving if (sChar.State == Character_BS.AnimState.Walk || sChar.State == Character_BS.AnimState.Run) { minR *= 3; maxR *= 3; } } float recoil = Random.Range(minR, maxR); Direction.Normalize(); Vector2 rotatedVector = Quaternion.AngleAxis(recoil, Vector3.forward) * Direction; // random velocity float randVel = Random.Range(0.9f, 1.15f); rb = GetComponent <Rigidbody2D>(); rotatedVector.Normalize(); rb.AddForce(rotatedVector * velocity * randVel); GameObject.Destroy(gameObject, 3); }
// explode the grenade, injure those nearby public void explode() { Character_BS userChar = User.GetComponent <Character_BS>(); GameStateBS GAMESTATE = userChar.GAMESTATE; foreach (GameObject go in GAMESTATE.ALL_CHARACTERS) { float dist = Vector3.Distance(go.transform.position, transform.position); if (dist <= explosionRadius) { Character_BS targetChar = go.GetComponent <Character_BS>(); int realDam = Mathf.FloorToInt(damage * (1 - dist * damageDropoff)); // SET MIN DEPTH TO 1, so grenade wont be hit! RaycastHit2D[] hitInfo = Physics2D.RaycastAll(transform.position, go.transform.position - transform.position, dist, -1, 1); Collider2D targColl = go.GetComponent <Collider2D>(); Collider2D grenColl = GetComponent <Collider2D>(); bool isHit = true; foreach (RaycastHit2D hit in hitInfo) { if (hit.collider == null || hit.collider == targColl) { // could be target, could be nothing } else if (hit.collider == grenColl) { // grenade hit itself } else { // grenade hit a wall, stops the hit from being processed isHit = false; //print("Missed: " + hit.collider.gameObject); } } if (isHit == true) { //print("Hit: " + targetChar.gameObject); targetChar.hitByExplosion(this, realDam); } } } if (shotClip != null) { AudioSource.PlayClipAtPoint(shotClip, transform.position); } GAMESTATE.MakeNoise(gameObject, transform.position, explodeVolume); GameObject.Destroy(gameObject, 0.05f); }
// Use this for initialization void Start() { // check character obj exists if (character == null) { character = gameObject.GetComponent <Character_BS>(); } // check if gamestate reference exists if (GAMESTATE == null) { GAMESTATE = gameObject.GetComponentInParent <GameStateBS>(); } // check for target // TODO do that... }
float minForgetDistance = 0.05f; // completely forget a soft target if we get within this distance // Use this for initialization void Start() { // check character obj exists if (character == null) { character = gameObject.GetComponent <Character_BS>(); } // check if gamestate reference exists if (GAMESTATE == null) { GAMESTATE = gameObject.GetComponentInParent <GameStateBS>(); } softTarget = transform.position; // set target to where we already are }
// melee attack hit me public void hitByMelee(WeaponBS weapon, Character_BS attacker) { float critRoll = UnityEngine.Random.Range(0.0f, 1.0f); float accRoll = UnityEngine.Random.Range(0.0f, 1.0f); int damage = weapon.damage; if (critRoll <= weapon.critChance * attacker.meleeSkill) // less than or equal to crit chance { // crit! damage *= 10; // mult by 10 } if (accRoll >= weapon.accuracy * attacker.meleeSkill) { damage = 0; } health -= damage; //print("Melee attack from: " + weapon.item.gameObject.name + " dealt " + damage.ToString() + " damage."); }
// throw a grenade! public void MakeTertiaryAttack(GameObject attacker, Vector2 direction, GrenadeBS grenade) { GameObject grenObj = grenade.gameObject; SpriteRenderer sr = grenObj.GetComponent <SpriteRenderer>(); sr.enabled = true; //grenade.GetComponent<ItemBS>().coldr.enabled = true; THIS IS DONE IN THE GRENADE CLASS grenObj.transform.position = attacker.transform.position; grenObj.transform.rotation = attacker.transform.rotation; grenObj.transform.parent = gameObject.transform; // set gamestate to parent, not shooter grenade.User = attacker; grenade.throwGrenade(direction); Character_BS att_char = attacker.GetComponent <Character_BS>(); att_char.tertiaryWeapon = null; // TODO: set this to the next tertiary weapon }
// missile hit me! public void hitByMissile(WeaponBS weapon, Character_BS shooter) { float critRoll = UnityEngine.Random.Range(0.0f, 1.0f); float accRoll = UnityEngine.Random.Range(0.0f, 1.0f); int damage = weapon.damage; if (critRoll <= weapon.critChance * shooter.missileSkill) // less than or equal to crit chance { // crit! damage *= 10; // mult by 10 } if (accRoll >= weapon.accuracy * shooter.missileSkill) { damage = 0; } health -= damage; }
// weapon hits something private void OnTriggerEnter2D(Collider2D collision) { GameObject target = collision.gameObject; GameObject item = weapon.item.gameObject; GameObject wielder = item.GetComponentInParent <Character_BS>().gameObject; if (target == wielder || (target.tag == "Zombie" && weapon.ignoreZombies == true)) { // do nothing, i hit myself } else { Character_BS targetChar = target.GetComponent <Character_BS>(); if (targetChar != null) { targetChar.hitByMelee(weapon, attacker.GetComponent <Character_BS>()); } } }
// Update is called once per frame void Update() { if (target != null) { Character_BS character = target.GetComponent <Character_BS>(); if (character != null) { if (character.State == Character_BS.AnimState.Loot) { character.inventory.Add(this); transform.SetParent(target.transform); gameObject.SetActive(false); // dont need to see it anymore print("You picked up " + gameObject.name); } } } else if (Time.time >= trigTime) { target = null; } }
// pauses game public void togglePaused() { if (isPaused == false) { Time.timeScale = 0; isPaused = true; foreach (GameObject go in ALL_CHARACTERS) { Character_BS chr = go.GetComponent <Character_BS>(); if (chr != null) { chr.freezeChar = true; } } TargetBS target = Player1.GetComponentInChildren <TargetBS>(); if (target != null) { target.freezeTarget = true; } } else { Time.timeScale = 1; isPaused = false; foreach (GameObject go in ALL_CHARACTERS) { Character_BS chr = go.GetComponent <Character_BS>(); if (chr != null) { chr.freezeChar = false; } } TargetBS target = Player1.GetComponentInChildren <TargetBS>(); if (target != null) { target.freezeTarget = false; } } }
// spawns a new zombie (and returns) public GameObject SpawnZombie(Vector2 spawnPoint) { GameObject zombie = Instantiate(prefabZombie, this.transform) as GameObject; ALL_CHARACTERS.Add(zombie); zombie.transform.position = spawnPoint; Character_BS zChar = zombie.GetComponent <Character_BS>(); zChar.health = (int)UnityEngine.Random.Range(60, 100); zChar.maxHealth = zChar.health; zChar.BaseWalkSpeed = UnityEngine.Random.Range(0.25f, 0.35f); zChar.BaseRunSpeed = zChar.BaseWalkSpeed * 1.5f; //zChar.items; does the zombie have some items ZombieBS zBS = zombie.GetComponent <ZombieBS>(); zBS.delayTime = UnityEngine.Random.Range(0.3f, 0.9f); // how long after going idle till they stop zBS.minIntensity = UnityEngine.Random.Range(0.5f, 1f); // how likely to respond to minor sounds zBS.hardDetectRange = UnityEngine.Random.Range(1.5f, 2f); // how close before hard target return(zombie); }
// ignores character objects when doing a "can see" public bool canSeeIgnoreCharacters(Vector3 start, Vector3 destination) { RaycastHit2D[] allHits = Physics2D.RaycastAll(start, destination - start, Vector3.Distance(start, destination)); foreach (RaycastHit2D hit in allHits) { GameObject go = hit.collider.gameObject; Character_BS ch = null; if (go != null) { ch = go.GetComponent <Character_BS>(); } if (ch != null) { //print("Sight not blocked by " + go.name); } else { return(false); } } return(true); }
// bullet hits something private void OnCollisionEnter2D(Collision2D collision) { bool doNotDestroy = false; GameObject target = collision.gameObject; Character_BS targetChar = target.GetComponent <Character_BS>(); // THROWS ERROR after player dies if (target.tag == "Bullet" || target == weapon.item.gameObject || target == shooter) { // Dont destroy for colliding with the weapon or shooter or bullets doNotDestroy = true; } else if (targetChar != null) { targetChar.hitByProjectile(this, shooter.GetComponent <Character_BS>()); } // get rid of the bullet if this was a valid collision if (doNotDestroy == false) { GameObject.Destroy(gameObject, 0.01f); } }
// Use this for initialization void Start() { shotspawn = transform; attackSpeed = 60f / rateOfFire; character = GetComponentInParent <Character_BS>(); }
public void MakeAttack(GameObject attacker, Vector2 direction, WeaponBS weapon) { // checks for validity in the character class GameObject prefab; GameObject newMissile; GameObject newMelee; bool didAttack = true; Transform shotspawn = weapon.shotspawn; // shortcut switch (weapon.type) { // creates an object to act like projectile case WeaponBS.Type.Projectile: if (weapon.ClipItem.rounds > 0) { prefab = weapon.Missile_Prefab; newMissile = Instantiate(prefab, shotspawn.position, shotspawn.rotation) as GameObject; BulletBS newBBS = newMissile.GetComponent <BulletBS>(); // mono behave, act like bullet (maybe need to add more variety) newBBS.weapon = weapon; newBBS.shooter = attacker; newBBS.Shoot(direction); } else { didAttack = false; } break; // detects hits with raycast case WeaponBS.Type.Missile: if (weapon.ClipItem.rounds > 0) { List <GameObject> ignoreList = new List <GameObject>(); ignoreList.Add(attacker); RaycastHit2D[] allHits = Physics2D.RaycastAll(weapon.shotspawn.position, direction, weapon.range); foreach (RaycastHit2D hit in allHits) { GameObject go = hit.collider.gameObject; BarricadeBS br = go.GetComponent <BarricadeBS>(); if (ignoreList.Contains(go) || br != null) { //print("SHOT not blocked by " + go.name); } else { Character_BS ch = go.GetComponent <Character_BS>(); if (ch != null) { ch.hitByMissile(weapon, attacker.GetComponent <Character_BS>()); } break; } } } else { didAttack = false; } break; // creates a little nonmoving object to detect hits case WeaponBS.Type.Melee: prefab = weapon.Melee_Prefab; newMelee = Instantiate(prefab, shotspawn.position, shotspawn.rotation) as GameObject; MeleeAttackBS meleeattack = newMelee.GetComponent <MeleeAttackBS>(); meleeattack.weapon = weapon; newMelee.transform.parent = attacker.transform; meleeattack.attacker = attacker; // still make noise when we attack break; default: break; } if (didAttack) { MakeNoise(attacker, shotspawn.position, weapon.noise); weapon.makeSound(); weapon.attackUpdate(); // update the weapon } }