private void Update() { Move.x = Input.GetAxis("Horizontal"); Move.y = Input.GetAxis("Vertical"); Shoot.x = Input.GetAxis("ShootX"); Shoot.y = Input.GetAxis("ShootY"); FireCooldown = Mathf.Max(0.0f, FireCooldown - Time.deltaTime); var settings = TwoStickBootstrap.Settings; Transform2D xform = GetComponent <Transform2D>(); xform.Position += Time.deltaTime * Move * settings.playerMoveSpeed; if (Fire) { xform.Heading = math.normalize(Shoot); FireCooldown = settings.playerFireCoolDown; var newShotData = new ShotSpawnData() { Position = xform.Position, Heading = xform.Heading, Faction = xform.GetComponent <Faction>() }; ShotSpawnSystem.SpawnShot(newShotData); } }
protected override void OnUpdate() { /* * SM -Including Length gives a convenient way to check if * we need to continue. */ if (m_Data.Length == 0) { return; } var settings = TwoStickBootstrap.Settings; /* * SM - Moving Time.deltaTime outside of loops is more efficient, * and means all items in the loop receive the same value for deltaTime. */ float dt = Time.deltaTime; var firingPlayers = new List <GameObject>(); for (int index = 0; index < m_Data.Length; ++index) { var position = m_Data.Position[index]; var heading = m_Data.Heading[index]; var playerInput = m_Data.Input[index]; //GetComponent<Position2D>().Value += Time.deltaTime * Move * settings.playerMoveSpeed; position.Value += dt * playerInput.Move * settings.playerMoveSpeed; //if(Fire) if (playerInput.Fire) { // GetComponent<Heading2D>().Value = math.normalize(Shoot); heading.Value = math.normalize(playerInput.Shoot); // FireCooldown = settings.playerFireCoolDown; playerInput.FireCooldown = settings.playerFireCoolDown; firingPlayers.Add(m_Data.GameObject[index]); } } foreach (var player in firingPlayers) { var newShotData = new ShotSpawnData() { Position = player.GetComponent <Position2D>().Value, Heading = player.GetComponent <Heading2D>().Value, Faction = player.GetComponent <Faction>() }; ShotSpawnSystem.SpawnShot(newShotData); } }
public static void SpawnShot(ShotSpawnData data) { var settings = TwoStickBootstrap.Settings; var prefab = data.Faction.Value == Faction.Type.Player ? settings.PlayerShotPrefab : settings.EnemyShotPrefab; var newShot = Object.Instantiate(prefab); newShot.GetComponent <Position2D>().Value = data.Position; newShot.GetComponent <Heading2D>().Value = data.Heading; var shotFaction = newShot.GetComponent <Faction>(); shotFaction.Value = data.Faction.Value; }
protected override void OnUpdate() { if (m_Player.Length == 0) { return; } // var playerPos = player.GetComponent<Position2D>().Value; var playerPos = m_Player.Position[0].Value; var shotSpawnData = new List <ShotSpawnData>(); float dt = Time.deltaTime; float shootRate = TwoStickBootstrap.Settings.enemyShootRate; for (int i = 0; i < m_Data.Length; i++) { //var state = GetComponent<EnemyShootState>(); var state = m_Data.ShootState[i]; //state.Cooldown -= Time.deltaTime; state.Cooldown -= dt; if (state.Cooldown <= 0.0) { //state.Cooldown = TwoStickBootstrap.Settings.enemyShootRate; state.Cooldown = shootRate; //var position = GetComponent<Position2D>().Value; var position = m_Data.Position[i].Value; ShotSpawnData spawn = new ShotSpawnData() { Position = position, Heading = math.normalize(playerPos - position), Faction = TwoStickBootstrap.Settings.EnemyFaction }; //ShotSpawnSystem.SpawnShot(spawn); shotSpawnData.Add(spawn); } } foreach (var spawn in shotSpawnData) { ShotSpawnSystem.SpawnShot(spawn); } }
private void Update() { var player = FindObjectOfType <Player>(); if (!player) { Destroy(gameObject); return; } // Movement var settings = TwoStickBootstrap.Settings; var minY = settings.playfield.yMin; var maxY = settings.playfield.yMax; var xform = GetComponent <Transform2D>(); if (xform.Position.y > maxY || xform.Position.y < minY) { GetComponent <Health>().Value = -1; } // Shooting var playerPos = player.GetComponent <Transform2D>().Position; var state = GetComponent <EnemyShootState>(); state.Cooldown -= Time.deltaTime; if (state.Cooldown <= 0.0) { state.Cooldown = TwoStickBootstrap.Settings.enemyShootRate; var position = GetComponent <Transform2D>().Position; ShotSpawnData spawn = new ShotSpawnData() { Position = position, Heading = math.normalize(playerPos - position), Faction = TwoStickBootstrap.Settings.EnemyFaction }; ShotSpawnSystem.SpawnShot(spawn); } }