Last active
September 14, 2023 15:00
-
-
Save javel555/6a8f27fdf8bf723ef78cba354c5064b1 to your computer and use it in GitHub Desktop.
EasySTG GameManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.InputSystem; | |
using UnityEngine.UI; | |
public class GameManager : MonoBehaviour | |
{ | |
public Transform player; | |
public Transform playerModel; | |
public GameObject BulletPrefab; | |
public GameObject EnemyPrefab; | |
public Transform BulletContainer; | |
public Transform EnemyContainer; | |
public Text Score; | |
public Camera MainCamera; | |
public RectTransform UiCanvas; | |
public RectTransform Marker; | |
private Transform TargettingEnemy; | |
private bool isTitle = true; | |
private int score = 0; | |
private float lastFireTime = 0f; | |
private Vector3 plVelocity = Vector2.zero; | |
private Vector3 plqbVelocity = Vector2.zero; | |
private readonly Dictionary<int, Vector3> blVelocity = new(); | |
private float lastWaveStartTime = 0f; | |
// Start is called before the first frame update | |
void Start() | |
{ | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
if (isTitle) | |
{ | |
TitleUpdate(); | |
return; | |
} | |
// | |
// Player | |
// | |
var plVelMag = plVelocity.magnitude; | |
var plVelNor = plVelocity.normalized; | |
var plqbVelMag = plqbVelocity.magnitude; | |
var plqbVelNor = plqbVelocity.normalized; | |
// Player - Friction | |
float move_friction = 40f; | |
plVelocity = MyPhysics.ApplyFriction(plVelocity, move_friction); | |
float qb_friction = 200f; | |
plqbVelocity = MyPhysics.ApplyFriction(plqbVelocity, qb_friction); | |
// Player - Input move | |
float move_speed = 20f; | |
float move_acc = 100f; | |
Vector2 leftStick = (Gamepad.current?.leftStick.ReadValue()) ?? Vector2.zero; | |
if (Keyboard.current.upArrowKey.isPressed) leftStick.y = 1; | |
else if (Keyboard.current.leftArrowKey.isPressed) leftStick.x = -1; | |
else if (Keyboard.current.rightArrowKey.isPressed) leftStick.x = 1; | |
else if (Keyboard.current.downArrowKey.isPressed) leftStick.y = -1; | |
var stickVec = new Vector3(leftStick.x, 0, leftStick.y); | |
// Debug.Log(stickVec); | |
plVelocity += stickVec * move_acc * Time.deltaTime; | |
plVelocity = Vector3.ClampMagnitude(plVelocity, move_speed); | |
float qb_speed = 100f; | |
if (Gamepad.current.leftTrigger.wasPressedThisFrame) | |
plqbVelocity = stickVec * qb_speed; | |
player.Translate((plVelocity + plqbVelocity) * Time.deltaTime); | |
float rotate_speed = 50f; | |
var rightStick = Gamepad.current?.rightStick.ReadValue() ?? Vector2.zero; | |
if (plqbVelMag > 0) rotate_speed = 150f; | |
if (Keyboard.current.aKey.isPressed) rightStick.x = -1f; | |
else if (Keyboard.current.sKey.isPressed) rightStick.x = 1f; | |
player.Rotate(Vector3.up, rightStick.x * rotate_speed * Time.deltaTime); | |
// Targetting | |
Marker.gameObject.SetActive(false); | |
var nowPos = player.position; | |
foreach (Transform e in EnemyContainer) | |
{ | |
var len = (e.position - nowPos).sqrMagnitude; | |
if (len > 100 * 100) continue; | |
if (len < .1f * .1f) continue; | |
if (TargettingEnemy) | |
{ | |
var len2 = (TargettingEnemy.position - nowPos).sqrMagnitude; | |
if (len < len2) TargettingEnemy = e; | |
} | |
else | |
{ | |
TargettingEnemy = e; | |
} | |
} | |
if (TargettingEnemy) | |
{ | |
// ターゲットした敵にUIマーカーを追従させる | |
var screenPos = MainCamera.WorldToScreenPoint(TargettingEnemy.position); | |
if (MainCamera.pixelRect.Contains(screenPos)) | |
{ | |
Vector2 anchordPos; | |
RectTransformUtility.ScreenPointToLocalPointInRectangle(UiCanvas, screenPos, MainCamera, out anchordPos); | |
Marker.anchoredPosition = anchordPos; | |
Marker.gameObject.SetActive(true); | |
} | |
else | |
{ | |
TargettingEnemy = null; | |
} | |
} | |
float playerModel_Angle = 240f; | |
if (TargettingEnemy) | |
{ | |
var lookAt = Quaternion.LookRotation(TargettingEnemy.position - playerModel.position); | |
playerModel.rotation = Quaternion.RotateTowards(playerModel.rotation, lookAt, playerModel_Angle * Time.deltaTime); | |
} | |
else | |
{ | |
playerModel.rotation = Quaternion.RotateTowards(playerModel.rotation, player.rotation, playerModel_Angle * Time.deltaTime); | |
} | |
// Fire bullet | |
float bullet_speed = 120f; | |
if (Keyboard.current.spaceKey.isPressed | |
|| (Gamepad.current?.rightTrigger.isPressed == true)) | |
{ | |
if (Time.time > lastFireTime + 0.25f) | |
{ | |
var baby = Instantiate(BulletPrefab, player.position, playerModel.rotation, BulletContainer); | |
baby.SetActive(true); | |
Destroy(baby, 10); | |
blVelocity.Add(baby.GetHashCode(), plVelocity + Vector3.forward * bullet_speed); | |
lastFireTime = Time.time; | |
} | |
} | |
// | |
// Bullet | |
// | |
foreach (Transform bullet in BulletContainer) | |
{ | |
var velocity = blVelocity[bullet.gameObject.GetHashCode()]; | |
bullet.Translate(velocity * Time.deltaTime); | |
} | |
// | |
// Wave | |
// | |
var time = Time.time; | |
if (time > 3f) | |
{ | |
if (EnemyContainer.childCount == 0 | |
|| time > lastWaveStartTime + 15f) | |
{ | |
Wave1(); | |
} | |
} | |
// | |
// Enemy | |
// | |
float enemy_speed = 18f; | |
float enemy_rotate_speed = 100f; | |
foreach (Transform enemy in EnemyContainer) | |
{ | |
var targetDirection = (player.position - enemy.position); | |
var targetQuaternion = Quaternion.LookRotation(targetDirection); | |
// LookAt | |
// enemy.rotation = targetQuaternion; | |
// RotateTowards | |
enemy.rotation = Quaternion.RotateTowards(enemy.rotation, targetQuaternion, enemy_rotate_speed * Time.deltaTime); | |
enemy.Translate(Vector3.forward * enemy_speed * Time.deltaTime); | |
foreach (Transform bullet in BulletContainer) | |
{ | |
var diff = enemy.position - bullet.position; | |
var mag = Mathf.Abs(diff.magnitude); | |
if (mag < 1f) | |
{ | |
Destroy(enemy.gameObject); | |
Destroy(bullet.gameObject); | |
score += 1; | |
break; | |
} | |
} | |
var diffByPl = player.position - enemy.position; | |
var magByPl = Mathf.Abs(diffByPl.magnitude); | |
if (magByPl < 1f) | |
{ | |
isTitle = true; | |
break; | |
} | |
} | |
// | |
// UI | |
// | |
Score.text = $"{score}"; | |
} | |
// プレイヤーの周囲に12匹出す | |
void Wave1() | |
{ | |
float enemyCount = 12; | |
lastWaveStartTime = Time.time; | |
float deg = 360 / enemyCount; | |
var plpos = player.position; | |
for (int i = 0; i < enemyCount; i++) | |
{ | |
float bx = Mathf.Sin(deg * i * Mathf.Deg2Rad); | |
float by = Mathf.Cos(deg * i * Mathf.Deg2Rad); | |
var popPos = plpos + new Vector3(bx, 0, by) * 60; | |
var baby = Instantiate(EnemyPrefab, popPos, Quaternion.LookRotation(plpos - popPos), EnemyContainer); | |
baby.SetActive(true); | |
} | |
} | |
void TitleUpdate() | |
{ | |
if (Gamepad.current.buttonNorth.isPressed) | |
{ | |
isTitle = false; | |
score = 0; | |
lastFireTime = 0f; | |
player.position = Vector3.zero; | |
player.rotation = Quaternion.identity; | |
plVelocity = Vector3.zero; | |
plqbVelocity = Vector3.zero; | |
foreach (Transform bl in BulletContainer) | |
{ | |
Destroy(bl.gameObject); | |
} | |
blVelocity.Clear(); | |
foreach (Transform en in EnemyContainer) | |
{ | |
Destroy(en.gameObject); | |
} | |
lastWaveStartTime = 0f; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment