void Update() { if (GameManager.Paused) { return; } if (playerThing.Dead) { if (PlayerCamera.Instance != null) { PlayerCamera.Instance.bopActive = false; } if (PlayerWeapon.Instance != null) { PlayerWeapon.Instance.bopActive = false; } if (deathTime < 1f) { deathTime += Time.deltaTime; } else { if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0)) { deathTime = 0; viewDirection = Vector2.zero; playerThing.hitpoints = 100; playerThing.armor = 0; if (PlayerWeapon.Instance != null) { Destroy(PlayerWeapon.Instance.gameObject); PlayerWeapon.Instance = null; } PlayerInfo.Instance.Reset(); GameManager.Instance.ChangeMap = MapLoader.CurrentMap; } } return; } if (pokeSoundTime > 0) { pokeSoundTime -= Time.deltaTime; } viewDirection.y += Input.GetAxis("Mouse X") * Options.MouseSensitivity.x; viewDirection.x -= Input.GetAxis("Mouse Y") * Options.MouseSensitivity.y; //so you don't fall when no-clipping bool outerSpace = false; //smoother elevator movement bool sticktofloor = false; { Triangle t = TheGrid.GetExactTriangle(transform.position); if (t != null) { if (playerThing.currentSector != t.sector) { if (playerThing.currentSector != null) { playerThing.currentSector.floorObject.DynamicThings.Remove(playerThing); } t.sector.floorObject.DynamicThings.AddLast(playerThing); } playerThing.LastSector = playerThing.currentSector; playerThing.currentSector = t.sector; if (playerThing.LastSector != null && playerThing.currentSector != null) { if (playerThing.LastSector == playerThing.currentSector) { if (lastFloorHeight != playerThing.currentSector.floorHeight) { lastFloorHeight = playerThing.currentSector.floorHeight; if (controller.isGrounded) { float diff = Mathf.Abs((transform.position.y - centerHeight) - playerThing.currentSector.floorHeight); if (diff <= MapLoader._4units) { transform.position = new Vector3(transform.position.x, playerThing.currentSector.floorHeight + centerHeight, transform.position.z); sticktofloor = true; } } } } else { lastFloorHeight = playerThing.currentSector.floorHeight; } } //so we can no-clip from lower platform to higher if (transform.position.y < t.sector.floorHeight) { transform.position = new Vector3(transform.position.x, t.sector.floorHeight, transform.position.z); } } else { outerSpace = true; } } //read input if (Input.GetKey(KeyCode.LeftArrow)) { viewDirection.y -= Time.deltaTime * 90; } if (Input.GetKey(KeyCode.RightArrow)) { viewDirection.y += Time.deltaTime * 90; } if (viewDirection.y < -180) { viewDirection.y += 360; } if (viewDirection.y > 180) { viewDirection.y -= 360; } //restricted up/down looking angle as sprites look really bad when looked at steep angle //also the game doesn't really require such as originally there was no way to rotate camera pitch //if (viewDirection.x < -90) viewDirection.x = -90; //if (viewDirection.x > 90) viewDirection.x = 90; if (viewDirection.x < -45) { viewDirection.x = -45; } if (viewDirection.x > 45) { viewDirection.x = 45; } transform.rotation = Quaternion.Euler(0, viewDirection.y, 0); //qwerty and dvorak combatible =^-^= float forwardSpeed = 0f; if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.Comma) || Input.GetKey(KeyCode.UpArrow)) { forwardSpeed += 1f; } if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.O) || Input.GetKey(KeyCode.DownArrow)) { forwardSpeed -= 1f; } Vector3 forward = transform.TransformDirection(Vector3.forward) * forwardSpeed; float sidewaysSpeed = 0f; if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.E)) { sidewaysSpeed += 1f; } if (Input.GetKey(KeyCode.A)) { sidewaysSpeed -= 1f; } Vector3 right = transform.TransformDirection(Vector3.right) * sidewaysSpeed; //fall down or hit ground if (controller.isGrounded || outerSpace) { if (!lastFrameStickToFloor) { if (gravityAccumulator > gravityOofThreshold) { audioSource.clip = SoundLoader.Instance.LoadSound("DSOOF"); audioSource.Play(); } } gravityAccumulator = 0f; } else { gravityAccumulator += Time.deltaTime * GameManager.Instance.gravity; } //terminal velocity or in elevator if (gravityAccumulator > GameManager.Instance.terminalVelocity || sticktofloor) { gravityAccumulator = GameManager.Instance.terminalVelocity; } //apply move Vector3 move = Vector3.down * Time.deltaTime * gravityAccumulator; if (sidewaysSpeed != 0f || forwardSpeed != 0f) { move += (forward + right).normalized * speed * Time.deltaTime; } controller.Move(move); //used so player doesn't hit the elevator floor constantly and make the OOF sound lastFrameStickToFloor = sticktofloor; //apply bop if (Mathf.Abs(forwardSpeed) + Mathf.Abs(sidewaysSpeed) > .1f) { if (PlayerCamera.Instance != null) { PlayerCamera.Instance.bopActive = true; } if (PlayerWeapon.Instance != null) { PlayerWeapon.Instance.bopActive = true; } } else { if (PlayerCamera.Instance != null) { PlayerCamera.Instance.bopActive = false; } if (PlayerWeapon.Instance != null) { PlayerWeapon.Instance.bopActive = false; } } //such vanity if (PlayerWeapon.Instance != null) { if (playerThing.currentSector != null) { SectorController sc = playerThing.currentSector.floorObject; PlayerWeapon.Instance.sectorLight = sc.sector.brightness; } } //poke stuff if (Input.GetKeyDown(KeyCode.Space)) { Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward); RaycastHit hit; if (Physics.Raycast(ray, out hit, 2, ~((1 << 9) | (1 << 10)), QueryTriggerInteraction.Ignore)) { bool noway = true; Pokeable lc = hit.collider.gameObject.GetComponent <Pokeable>(); if (lc != null) { if (lc.Poke(gameObject)) { noway = false; } } if (noway && pokeSoundTime <= 0) { audioSource.clip = SoundLoader.Instance.LoadSound("DSNOWAY"); audioSource.Play(); pokeSoundTime = .175f; } } } //use weapon if (Input.GetMouseButton(0) || Input.GetKey(KeyCode.RightControl) || Input.GetKey(KeyCode.LeftControl)) { if (PlayerWeapon.Instance != null) { if (PlayerWeapon.Instance.Fire()) { if (PlayerWeapon.Instance.Noise > 0) { playerThing.CastNoise(PlayerWeapon.Instance.Noise); } } } } //swap weapon if (PlayerWeapon.Instance == null) { if (SwapWeapon == -1) { SwapToBestWeapon(); } if (SwapWeapon > -1) { PlayerWeapon.Instance = Instantiate(PlayerInfo.Instance.WeaponPrefabs[SwapWeapon]); CurrentWeapon = SwapWeapon; SwapWeapon = -1; } } if (Input.GetKeyDown(KeyCode.Alpha1)) { if (!TrySwapWeapon(1)) { TrySwapWeapon(0); } } if (Input.GetKeyDown(KeyCode.Alpha2)) { TrySwapWeapon(2); } if (Input.GetKeyDown(KeyCode.Alpha3)) { TrySwapWeapon(3); } if (Input.GetKeyDown(KeyCode.Alpha4)) { TrySwapWeapon(4); } if (Input.GetKeyDown(KeyCode.Alpha5)) { TrySwapWeapon(5); } if (Input.GetKeyDown(KeyCode.Escape)) { Application.Quit(); } }
public override void Tick() { if (!alert) { if (decisionTime > 0f) { decisionTime -= Time.deltaTime; return; } decisionTime = Random.Range(.4f, .6f); int distance = AxMath.WeightedDistance(owner.cell, GameManager.Instance.Player[0].cell); if (distance > SeeDistance) { return; } if (Random.value <= NearSoundChanceSleeping) { if (mc.NearSounds.Length > 0) { if (!mc.audioSource.isPlaying) { mc.audioSource.clip = mc.NearSounds[Random.Range(0, mc.NearSounds.Length)]; mc.audioSource.Play(); } } } if (SeePlayerRay()) { alert = true; decisionTime = 0f; } if (!alert) { return; } } if (painFrame) { mc.moveVector = Vector3.zero; painFrame = false; attackTime = 0f; decisionTime = 0f; return; } if (attackTime > 0f) { attackTime -= Time.deltaTime; Vector3 aimAt = (GameManager.Instance.Player[0].transform.position - mc.transform.position).normalized; //mc.transform.rotation = Quaternion.LookRotation(Vector3.Lerp(mc.transform.forward, new Vector3(aimAt.x, 0, aimAt.z), Time.deltaTime * mc.turnSpeed), Vector3.up); //instantenous rotation towards target mc.transform.rotation = Quaternion.LookRotation(new Vector3(aimAt.x, 0, aimAt.z), Vector3.up); if (attackTime < AttackHappenTime && !attacked) { attacked = true; float distance = (owner.transform.position + Vector3.up * AttackHeight - GameManager.Instance.Player[0].transform.position).magnitude; if (distance < meleeAttackRange) { if (CanMeleeRay(distance)) { if (mc.AttackSounds.Length > 0) { GameManager.Create3DSound(mc.transform.position, mc.AttackSounds[1], 5f); } Damageable d = GameManager.Instance.Player[0].GetComponent <Damageable>(); if (d != null) { d.Damage(Random.Range(MeleeDamageMin, MeleeDamageMax + 1), DamageType.Generic, owner.gameObject); } } } else { if (mc.AttackSounds.Length > 0) { GameManager.Create3DSound(mc.transform.position, mc.AttackSounds[0], 5f); } if (mc.AttackProjectile != null) { FireballProjectile fireball = GameObject.Instantiate(mc.AttackProjectile).GetComponent <FireballProjectile>(); if (fireball != null) { fireball.transform.position = owner.transform.position + Vector3.up * AttackHeight; fireball.owner = owner.gameObject; fireball.transform.LookAt(GameManager.Instance.Player[0].transform.position + Random.insideUnitSphere * AttackSpread + Vector3.up * TargetHeightAimFix); fireball.transform.SetParent(GameManager.Instance.TemporaryObjectsHolder); } } } } return; } if (wantDirection != Vector3.zero) { mc.transform.rotation = Quaternion.LookRotation(Vector3.Lerp(mc.transform.forward, wantDirection, Time.deltaTime * mc.turnSpeed), Vector3.up); } if (decisionTime > 0f) { decisionTime -= Time.deltaTime; return; } if (Random.value <= PokeChance) { Ray ray = new Ray(owner.transform.position + Vector3.up, owner.transform.forward); RaycastHit hit; if (Physics.Raycast(ray, out hit, 2, ~((1 << 9) | (1 << 11)), QueryTriggerInteraction.Ignore)) { Pokeable lc = hit.collider.gameObject.GetComponent <Pokeable>(); if (lc != null) { if (lc.AllowMonsters()) { lc.Poke(owner.gameObject); } } } } if (Random.value <= NearSoundChanceAwake) { if (mc.NearSounds.Length > 0) { if (!mc.audioSource.isPlaying) { mc.audioSource.clip = mc.NearSounds[Random.Range(0, mc.NearSounds.Length)]; mc.audioSource.Play(); } } } decisionTime = Random.Range(.4f, .6f); wantDirection = Vector3.zero; bool aggro = false; { float distance = (owner.transform.position + Vector3.up * AttackHeight - GameManager.Instance.Player[0].transform.position).magnitude; if (distance < meleeAttackRange) { wantMove = false; aggro = true; attacked = false; attackTime = .7f; decisionTime = 0f; mc.InitAttackAnimation(); mc.frametime = .2f; } else if (Random.value < AggroChance) { Ray toPlayer; if (SeePlayerRay(out toPlayer)) { wantDirection = new Vector3(toPlayer.direction.x, 0, toPlayer.direction.z); wantMove = false; aggro = true; attacked = false; attackTime = 1f; decisionTime = 0f; mc.InitAttackAnimation(); } } } if (!aggro) { float moveRoll = Random.value; if (moveRoll < randomMoveChance) { MoveToRandomNearbyCell(); } else if (moveRoll < closestMoveChance) { if (!MoveToRandomClosestBreath()) { MoveToRandomNearbyCell(); } } else { if (!MoveTowardsBreath()) { if (!MoveToRandomClosestBreath()) { MoveToRandomNearbyCell(); } } } } if (Random.value < IdleChance) { wantMove = false; } if (wantMove) { mc.moveVector.x = 1; } else { mc.moveVector.x = 0; } }
public override void Tick() { if (!alert) { if (decisionTime > 0f) { decisionTime -= Time.deltaTime; return; } decisionTime = Random.Range(.4f, .6f); int distance = AxMath.WeightedDistance(owner.cell, GameManager.Instance.Player[0].cell); if (distance > SeeDistance) { return; } if (Random.value <= NearSoundChanceSleeping) { if (mc.NearSounds.Length > 0) { if (!mc.audioSource.isPlaying) { mc.audioSource.clip = mc.NearSounds[Random.Range(0, mc.NearSounds.Length)]; mc.audioSource.Play(); } } } if (SeePlayerRay()) { alert = true; decisionTime = 0f; } if (!alert) { return; } } if (painFrame) { mc.moveVector = Vector3.zero; painFrame = false; attackTime = 0f; decisionTime = 0f; return; } if (attackTime > 0f) { attackTime -= Time.deltaTime; Vector3 aimAt = (GameManager.Instance.Player[0].transform.position - mc.transform.position).normalized; //mc.transform.rotation = Quaternion.LookRotation(Vector3.Lerp(mc.transform.forward, new Vector3(aimAt.x, 0, aimAt.z), Time.deltaTime * mc.turnSpeed), Vector3.up); //instantenous rotation towards target mc.transform.rotation = Quaternion.LookRotation(new Vector3(aimAt.x, 0, aimAt.z), Vector3.up); if (attackTime < AttackHappenTime && !attacked) { attacked = true; if (mc.AttackSounds.Length > 0) { GameManager.Create3DSound(mc.transform.position, mc.AttackSounds[Random.Range(0, mc.AttackSounds.Length)], 10f); } PlayerThing player = GameManager.Instance.Player[0]; if (player != null) { //swap layers for a while to avoid hitting self int originalLayer = owner.gameObject.layer; owner.gameObject.layer = 9; for (int i = 0; i < shotCount; i++) { Vector3 eyePos = owner.transform.position + Vector3.up * EyeHeight; Vector3 toPlayer = ((player.transform.position + Random.onUnitSphere * player.RayCastSphereRadius) - eyePos).normalized; toPlayer += Random.insideUnitSphere * attackSpread; toPlayer.Normalize(); Ray ray = new Ray(eyePos, toPlayer); /*GameObject visualLine = new GameObject(); * LineRenderer lr = visualLine.AddComponent<LineRenderer>(); * lr.positionCount = 2; * lr.SetPosition(0, ray.origin); * lr.SetPosition(1, ray.origin + ray.direction * 200); * lr.widthMultiplier = .02f; * visualLine.AddComponent<DestroyAfterTime>();*/ RaycastHit hit; if (Physics.Raycast(ray, out hit, 200, ~((1 << 9) | (1 << 14)), QueryTriggerInteraction.Ignore)) { Damageable target = hit.collider.gameObject.GetComponent <Damageable>(); if (target != null) { target.Damage(Random.Range(DamageMin, DamageMax + 1), DamageType.Generic, owner.gameObject); if (target.Bleed) { GameObject blood = GameObject.Instantiate(GameManager.Instance.BloodDrop); blood.transform.position = hit.point - ray.direction * .2f; } else { GameObject puff = GameObject.Instantiate(GameManager.Instance.BulletPuff); puff.transform.position = hit.point - ray.direction * .2f; } } else { GameObject puff = GameObject.Instantiate(GameManager.Instance.BulletPuff); puff.transform.position = hit.point - ray.direction * .2f; } } } owner.gameObject.layer = originalLayer; } } return; } if (wantDirection != Vector3.zero) { mc.transform.rotation = Quaternion.LookRotation(Vector3.Lerp(mc.transform.forward, wantDirection, Time.deltaTime * mc.turnSpeed), Vector3.up); } if (decisionTime > 0f) { decisionTime -= Time.deltaTime; return; } if (Random.value <= PokeChance) { Ray ray = new Ray(owner.transform.position + Vector3.up, owner.transform.forward); RaycastHit hit; if (Physics.Raycast(ray, out hit, 2, ~((1 << 9) | (1 << 11)), QueryTriggerInteraction.Ignore)) { Pokeable lc = hit.collider.gameObject.GetComponent <Pokeable>(); if (lc != null) { if (lc.AllowMonsters()) { lc.Poke(owner.gameObject); } } } } if (Random.value <= NearSoundChanceAwake) { if (mc.NearSounds.Length > 0) { if (!mc.audioSource.isPlaying) { mc.audioSource.clip = mc.NearSounds[Random.Range(0, mc.NearSounds.Length)]; mc.audioSource.Play(); } } } decisionTime = Random.Range(.4f, .6f); wantDirection = Vector3.zero; bool aggro = false; if (Random.value < AggroChance) { Ray toPlayer; if (SeePlayerRay(out toPlayer)) { wantDirection = new Vector3(toPlayer.direction.x, 0, toPlayer.direction.z); wantMove = false; aggro = true; attacked = false; attackTime = 1f; decisionTime = 0f; mc.InitAttackAnimation(); } } if (!aggro) { float moveRoll = Random.value; if (moveRoll < randomMoveChance) { MoveToRandomNearbyCell(); } else if (moveRoll < closestMoveChance) { if (!MoveToRandomClosestBreath()) { MoveToRandomNearbyCell(); } } else { if (!MoveTowardsBreath()) { if (!MoveToRandomClosestBreath()) { MoveToRandomNearbyCell(); } } } } if (Random.value < IdleChance) { wantMove = false; } if (wantMove) { mc.moveVector.x = 1; } else { mc.moveVector.x = 0; } }
void Update() { if (GameManager.Paused) { return; } if (playerThing.Dead) { PlayerCamera.Instance.bopActive = false; if (PlayerWeapon.Instance != null) { PlayerWeapon.Instance.bopActive = false; } if (deathTime < 1f) { deathTime += Time.deltaTime; } else { if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0)) { deathTime = 0; viewDirection = Vector2.zero; playerThing.hitpoints = 100; playerThing.armor = 0; if (PlayerWeapon.Instance != null) { Destroy(PlayerWeapon.Instance.gameObject); PlayerWeapon.Instance = null; } PlayerInfo.Instance.Reset(); GameManager.Instance.ChangeMap = MapLoader.CurrentMap; } } return; } if (pokeSoundTime > 0) { pokeSoundTime -= Time.deltaTime; } viewDirection.y += Input.GetAxis("Mouse X") * Options.MouseSensitivity.x; viewDirection.x -= Input.GetAxis("Mouse Y") * Options.MouseSensitivity.y; //so you don't fall when no-clipping bool outerSpace = false; //smoother elevator movement bool stickTofloor = false; { Triangle t = TheGrid.GetExactTriangle(transform.position); if (t != null) { if (playerThing.currentSector != t.sector) { if (playerThing.currentSector != null) { playerThing.currentSector.floorObject.DynamicThings.Remove(playerThing); } t.sector.floorObject.DynamicThings.AddLast(playerThing); } playerThing.LastSector = playerThing.currentSector; playerThing.currentSector = t.sector; if (playerThing.LastSector != null && playerThing.currentSector != null) { if (playerThing.LastSector == playerThing.currentSector) { if (lastFloorHeight != playerThing.currentSector.floorHeight) { lastFloorHeight = playerThing.currentSector.floorHeight; if (controller.isGrounded) { float diff = Mathf.Abs((transform.position.y - centerHeight) - playerThing.currentSector.floorHeight); if (diff <= MapLoader._4units) { transform.position = new Vector3(transform.position.x, playerThing.currentSector.floorHeight + centerHeight, transform.position.z); stickTofloor = true; } } } } else { lastFloorHeight = playerThing.currentSector.floorHeight; } } //so we can no-clip from lower platform to higher if (transform.position.y < t.sector.floorHeight) { transform.position = new Vector3(transform.position.x, t.sector.floorHeight, transform.position.z); } } else { outerSpace = true; } } //read input if (Input.GetKey(KeyCode.LeftArrow)) { viewDirection.y -= Time.deltaTime * 90; } if (Input.GetKey(KeyCode.RightArrow)) { viewDirection.y += Time.deltaTime * 90; } if (viewDirection.y < -180) { viewDirection.y += 360; } if (viewDirection.y > 180) { viewDirection.y -= 360; } //if (viewDirection.x < -90) viewDirection.x = -90; //if (viewDirection.x > 90) viewDirection.x = 90; if (viewDirection.x < -45) { viewDirection.x = -45; } if (viewDirection.x > 45) { viewDirection.x = 45; } transform.rotation = Quaternion.Euler(0, viewDirection.y, 0); //used so player doesn't hit the elevator floor constantly and make the OOF sound lastFrameStickToFloor = stickTofloor; MovementInput(outerSpace, stickTofloor); //such vanity if (PlayerWeapon.Instance != null) { if (playerThing.currentSector != null) { SectorController sc = playerThing.currentSector.floorObject; PlayerWeapon.Instance.sectorLight = sc.sector.brightness; } } //poke stuff if (Input.GetKeyDown(KeyCode.Space)) { Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward); RaycastHit hit; if (Physics.Raycast(ray, out hit, 2, ~((1 << 9) | (1 << 10)), QueryTriggerInteraction.Ignore)) { bool noway = true; Pokeable lc = hit.collider.gameObject.GetComponent <Pokeable>(); if (lc != null) { if (lc.Poke(gameObject)) { noway = false; } } if (noway && pokeSoundTime <= 0) { audioSource.clip = SoundLoader.Instance.LoadSound("DSNOWAY"); audioSource.Play(); pokeSoundTime = .175f; } } } //use weapon if (Input.GetMouseButton(0) || Input.GetKey(KeyCode.RightControl) || Input.GetKey(KeyCode.LeftControl)) { if (PlayerWeapon.Instance != null) { if (PlayerWeapon.Instance.Fire()) { if (PlayerWeapon.Instance.Noise > 0) { playerThing.CastNoise(PlayerWeapon.Instance.Noise); } } } } //swap weapon if (PlayerWeapon.Instance == null) { if (SwapWeapon == WeaponType.None) { SwapToBestWeapon(); } if (SwapWeapon > WeaponType.None) { PlayerWeapon.Instance = Instantiate(PlayerInfo.Instance.WeaponPrefabs[(int)SwapWeapon]); CurrentWeapon = SwapWeapon; SwapWeapon = WeaponType.None; } } if (Input.GetKeyDown(KeyCode.Alpha1)) { if (!TrySwapWeapon(WeaponType.Chainsaw)) { TrySwapWeapon(WeaponType.Fist); } } if (Input.GetKeyDown(KeyCode.Alpha2)) { TrySwapWeapon(WeaponType.Pistol); } if (Input.GetKeyDown(KeyCode.Alpha3)) { TrySwapWeapon(WeaponType.Shotgun); } if (Input.GetKeyDown(KeyCode.Alpha4)) { TrySwapWeapon(WeaponType.Chainsaw); } if (Input.GetKeyDown(KeyCode.Alpha5)) { TrySwapWeapon(WeaponType.RocketLauncher); } if (Input.GetKeyDown(KeyCode.Escape)) { Application.Quit(); } }