@ -5,11 +5,11 @@ using UnityEngine;
namespace Actions {
public class ShootAction : BaseAction {
private const float rotationSpeed = 10f ;
private const float shootingStateTime = . 2f ;
private const float coolOffStateTime = 1f ;
private const float aimingStateTime = 1.5f ;
private const float unitShoulderHeight = 1.7f ;
private const float ROTATION_SPEED = 10f ;
private const float SHOOTING_STATE_TIME = . 2f ;
private const float COOL_OFF_STATE_TIME = 1f ;
private const float AIMING_STATE_TIME = 1.5f ;
private const float UNIT_SHOULDER_HEIGHT = 1.7f ;
[SerializeField] private LayerMask obstaclesLayerMask ;
private bool canShootBullet ;
@ -33,7 +33,8 @@ namespace Actions {
switch ( state ) {
case State . Aiming :
Vector3 aimDirection = ( TargetUnit . GetWorldPosition ( ) - Unit . GetWorldPosition ( ) ) . normalized ;
transform . forward = Vector3 . Lerp ( transform . forward , aimDirection , rotationSpeed * Time . deltaTime ) ;
aimDirection . y = 0f ;
transform . forward = Vector3 . Slerp ( transform . forward , aimDirection , ROTATION_SPEED * Time . deltaTime ) ;
break ;
case State . Shooting :
if ( canShootBullet ) {
@ -63,11 +64,11 @@ namespace Actions {
switch ( state ) {
case State . Aiming :
state = State . Shooting ;
stateTimer = shootingStateTime ;
stateTimer = SHOOTING_STATE_TIME ;
break ;
case State . Shooting :
state = State . Cooloff ;
stateTimer = coolOffStateTime ;
stateTimer = COOL_OFF_STATE_TIME ;
break ;
case State . Cooloff :
ActionComplete ( ) ;
@ -82,7 +83,7 @@ namespace Actions {
canShootBullet = true ;
state = State . Aiming ;
stateTimer = aimingStateTime ;
stateTimer = AIMING_STATE_TIME ;
ActionStart ( onActionComplete ) ;
}
@ -91,25 +92,27 @@ namespace Actions {
private List < GridPosition > GetValidActionGridPositionList ( GridPosition unitGridPosition ) {
List < GridPosition > validGridPositionList = new ( ) ;
for ( int x = - MaxShootDistance ; x < = MaxShootDistance ; x + + ) {
for ( int z = - MaxShootDistance ; z < = MaxShootDistance ; z + + ) {
GridPosition offsetGridPosition = new ( x , z , 0 ) ;
GridPosition testGridPosition = unitGridPosition + offsetGridPosition ;
for ( int xPosition = - MaxShootDistance ; xPosition < = MaxShootDistance ; xPosition + + ) {
for ( int zPosition = - MaxShootDistance ; zPosition < = MaxShootDistance ; zPosition + + ) {
for ( int floor = - MaxShootDistance ; floor < MaxShootDistance ; floor + + ) {
GridPosition offsetGridPosition = new ( xPosition , zPosition , floor ) ;
GridPosition testGridPosition = unitGridPosition + offsetGridPosition ;
if ( ! LevelGrid . Instance . IsValidGridPosition ( testGridPosition ) ) continue ; //Only return valid grid positions
if ( Mathf . Abs ( x ) + Mathf . Abs ( z ) > MaxShootDistance ) continue ; //Out of Range
if ( ! LevelGrid . Instance . IsValidGridPosition ( testGridPosition ) ) continue ; //Only return valid grid positions
if ( Mathf . Abs ( x Position ) + Mathf . Abs ( z Position ) > MaxShootDistance ) continue ; //Out of Range
if ( ! LevelGrid . Instance . HasAnyUnitOnGridPosition ( testGridPosition ) ) continue ; //Grid position is empty, no unit
if ( ! LevelGrid . Instance . HasAnyUnitOnGridPosition ( testGridPosition ) ) continue ; //Grid position is empty, no unit
Unit unitAtGridPosition = LevelGrid . Instance . GetUnitAtGridPosition ( testGridPosition ) ;
if ( unitAtGridPosition . IsEnemy = = Unit . IsEnemy ) continue ; //Both units are on the same 'team'
TargetUnit = unitAtGridPosition ;
Unit unitAtGridPosition = LevelGrid . Instance . GetUnitAtGridPosition ( testGridPosition ) ;
if ( unitAtGridPosition . IsEnemy = = Unit . IsEnemy ) continue ; //Both units are on the same 'team'
TargetUnit = unitAtGridPosition ;
Vector3 unitWorldPosition = LevelGrid . Instance . GetWorldPosition ( unitGridPosition ) ;
Vector3 shootDir = ( TargetUnit . GetWorldPosition ( ) - unitWorldPosition ) . normalized ;
if ( Physics . Raycast ( unitWorldPosition + Vector3 . up * unitShoulderHeight , shootDir , Vector3 . Distance ( unitWorldPosition , TargetUnit . GetWorldPosition ( ) ) , obstaclesLayerMask ) ) continue ; // Blocked by an obstacle
Vector3 unitWorldPosition = LevelGrid . Instance . GetWorldPosition ( unitGridPosition ) ;
Vector3 shootDir = ( TargetUnit . GetWorldPosition ( ) - unitWorldPosition ) . normalized ;
if ( Physics . Raycast ( unitWorldPosition + Vector3 . up * UNIT_SHOULDER_HEIGHT , shootDir , Vector3 . Distance ( unitWorldPosition , TargetUnit . GetWorldPosition ( ) ) , obstaclesLayerMask ) ) continue ; // Blocked by an obstacle
validGridPositionList . Add ( testGridPosition ) ;
validGridPositionList . Add ( testGridPosition ) ;
}
}
}