@@ -19,6 +19,8 @@ public class AimAtCursorPlayerCharacterController : BasePlayerCharacterControlle
1919
2020 public NearbyEntityDetector ActivatableEntityDetector { get ; protected set ; }
2121 public NearbyEntityDetector ItemDropEntityDetector { get ; protected set ; }
22+ protected RaycastHit [ ] raycasts = new RaycastHit [ 512 ] ;
23+ protected RaycastHit2D [ ] raycasts2D = new RaycastHit2D [ 512 ] ;
2224
2325 protected override void Awake ( )
2426 {
@@ -175,22 +177,33 @@ protected void UpdateWASDInput()
175177 }
176178
177179 // Attack when player pressed attack button
178- if ( ! CacheUISceneGameplay . IsBlockController ( ) &&
180+ if ( ! CacheUISceneGameplay . IsPointerOverUIObject ( ) &&
179181 ! UICharacterHotkeys . UsingHotkey &&
180- ( InputManager . GetButton ( "Fire1" ) || InputManager . GetButton ( "Attack" ) ) )
181- {
182- if ( PlayerCharacterEntity . RequestAttack ( isLeftHandAttacking ) )
183- isLeftHandAttacking = ! isLeftHandAttacking ;
184- }
182+ ( InputManager . GetButton ( "Fire1" ) || InputManager . GetButton ( "Attack" ) ||
183+ InputManager . GetButtonUp ( "Fire1" ) || InputManager . GetButtonUp ( "Attack" ) ) )
184+ UpdateFireInput ( ) ;
185185
186186 // Always forward
187- MovementState movementState = Vector3 . Angle ( moveDirection , PlayerCharacterEntity . CacheTransform . forward ) < 120 ?
187+ MovementState movementState = Vector3 . Angle ( moveDirection , MovementTransform . forward ) < 120 ?
188188 MovementState . Forward : MovementState . Backward ;
189189 if ( InputManager . GetButtonDown ( "Jump" ) )
190190 movementState |= MovementState . IsJump ;
191191 PlayerCharacterEntity . KeyMovement ( moveDirection , movementState ) ;
192192 }
193193
194+ protected void UpdateFireInput ( )
195+ {
196+ if ( ! ConstructingBuildingEntity )
197+ {
198+ if ( PlayerCharacterEntity . RequestAttack ( isLeftHandAttacking ) )
199+ isLeftHandAttacking = ! isLeftHandAttacking ;
200+ }
201+ else if ( InputManager . GetButtonUp ( "Fire1" ) || InputManager . GetButtonUp ( "Attack" ) )
202+ {
203+ ConfirmBuild ( ) ;
204+ }
205+ }
206+
194207 protected void UpdateLookInput ( )
195208 {
196209 bool isMobile = InputManager . useMobileInputOnNonMobile || Application . isMobilePlatform ;
@@ -203,7 +216,7 @@ protected void UpdateLookInput()
203216 else
204217 {
205218 // Turn character follow cursor
206- lookDirection = ( InputManager . MousePosition ( ) - CacheGameplayCameraControls . CacheCamera . WorldToScreenPoint ( PlayerCharacterEntity . CacheTransform . position ) ) . normalized ;
219+ lookDirection = ( InputManager . MousePosition ( ) - CacheGameplayCameraControls . CacheCamera . WorldToScreenPoint ( MovementTransform . position ) ) . normalized ;
207220 }
208221
209222 // Turn character
@@ -221,6 +234,11 @@ protected void UpdateLookInput()
221234 PlayerCharacterEntity . SetLookRotation ( Quaternion . Euler ( 0 , rotY , 0 ) ) ;
222235 }
223236 }
237+
238+ if ( ConstructingBuildingEntity )
239+ {
240+ FindAndSetBuildingAreaFromMousePosition ( ) ;
241+ }
224242 }
225243
226244 protected void ReloadAmmo ( )
@@ -361,5 +379,80 @@ public Vector3 GetMoveDirection(float horizontalInput, float verticalInput)
361379 }
362380 return moveDirection ;
363381 }
382+
383+ public void FindAndSetBuildingAreaFromMousePosition ( )
384+ {
385+ int tempCount = 0 ;
386+ Vector3 tempVector3 ;
387+ // TODO: Test and implement mobile version
388+ switch ( CurrentGameInstance . DimensionType )
389+ {
390+ case DimensionType . Dimension3D :
391+ tempCount = PhysicUtils . SortedRaycastNonAlloc3D ( CacheGameplayCameraControls . CacheCamera . ScreenPointToRay ( InputManager . MousePosition ( ) ) , raycasts , 100f , CurrentGameInstance . GetBuildLayerMask ( ) ) ;
392+ break ;
393+ case DimensionType . Dimension2D :
394+ tempVector3 = CacheGameplayCameraControls . CacheCamera . ScreenToWorldPoint ( InputManager . MousePosition ( ) ) ;
395+ tempCount = PhysicUtils . SortedLinecastNonAlloc2D ( tempVector3 , tempVector3 , raycasts2D , CurrentGameInstance . GetBuildLayerMask ( ) ) ;
396+ break ;
397+ }
398+ LoopSetBuildingArea ( tempCount ) ;
399+ }
400+
401+ /// <summary>
402+ /// Return true if found building area
403+ /// </summary>
404+ /// <param name="count"></param>
405+ /// <returns></returns>
406+ private bool LoopSetBuildingArea ( int count )
407+ {
408+ BuildingArea buildingArea ;
409+ Transform tempTransform ;
410+ Vector3 tempVector3 ;
411+ Vector3 tempOffset ;
412+ for ( int tempCounter = 0 ; tempCounter < count ; ++ tempCounter )
413+ {
414+ tempTransform = GetRaycastTransform ( tempCounter ) ;
415+ tempVector3 = GetRaycastPoint ( tempCounter ) ;
416+ tempOffset = tempVector3 - MovementTransform . position ;
417+ tempVector3 = MovementTransform . position + Vector3 . ClampMagnitude ( tempOffset , CurrentGameInstance . buildDistance ) ;
418+
419+ buildingArea = tempTransform . GetComponent < BuildingArea > ( ) ;
420+ if ( buildingArea == null ||
421+ ( buildingArea . Entity && buildingArea . GetObjectId ( ) == ConstructingBuildingEntity . ObjectId ) ||
422+ ! ConstructingBuildingEntity . buildingTypes . Contains ( buildingArea . buildingType ) )
423+ {
424+ // Skip because this area is not allowed to build the building that you are going to build
425+ continue ;
426+ }
427+
428+ ConstructingBuildingEntity . BuildingArea = buildingArea ;
429+ ConstructingBuildingEntity . CacheTransform . position = tempVector3 ;
430+ return true ;
431+ }
432+ return false ;
433+ }
434+
435+ #region Pick functions
436+ public Transform GetRaycastTransform ( int index )
437+ {
438+ if ( CurrentGameInstance . DimensionType == DimensionType . Dimension3D )
439+ return raycasts [ index ] . transform ;
440+ return raycasts2D [ index ] . transform ;
441+ }
442+
443+ public bool GetRaycastIsTrigger ( int index )
444+ {
445+ if ( CurrentGameInstance . DimensionType == DimensionType . Dimension3D )
446+ return raycasts [ index ] . collider . isTrigger ;
447+ return raycasts2D [ index ] . collider . isTrigger ;
448+ }
449+
450+ public Vector3 GetRaycastPoint ( int index )
451+ {
452+ if ( CurrentGameInstance . DimensionType == DimensionType . Dimension3D )
453+ return raycasts [ index ] . point ;
454+ return raycasts2D [ index ] . point ;
455+ }
456+ #endregion
364457 }
365458}
0 commit comments