Skip to content

Commit

Permalink
Ajuste no sistema de controle
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasVinicius314 committed Sep 15, 2022
1 parent 3f48406 commit f693e88
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 66 deletions.
2 changes: 1 addition & 1 deletion Assets/Prefabs/Player.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ MonoBehaviour:
m_ActionId: 10927b2d-68ce-430a-bce1-c5a1f1d3daf0
m_ActionName: Player/Back[/Keyboard/escape,/Mouse/backButton,/Keyboard/tab]
m_NeverAutoSwitchControlSchemes: 0
m_DefaultControlScheme: Joystick
m_DefaultControlScheme:
m_DefaultActionMap: Player
m_SplitScreenIndex: -1
m_Camera: {fileID: 0}
Expand Down
12 changes: 10 additions & 2 deletions Assets/Scenes/MainScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -663,8 +663,8 @@ MonoBehaviour:
- {fileID: 4026393600238520495, guid: 4f847d9983c9b47469670e627d94626e, type: 3}
enemySpawnSettings:
isEnemySpawnEnabled: 1
spawnBatchSize: 5
spawnInterval: 3
spawnBatchSize: 2
spawnInterval: 10
enemyPrefabs:
- {fileID: 2422363816341469345, guid: 878f06b5ada303f47b84bdd84f0d4917, type: 3}
isNavMeshBaked: 0
Expand Down Expand Up @@ -778,10 +778,18 @@ PrefabInstance:
m_Modification:
m_TransformParent: {fileID: 0}
m_Modifications:
- target: {fileID: -3976935303735240655, guid: 5d4e7d89f2ca88444a90ee28fd289237, type: 3}
propertyPath: m_DefaultControlScheme
value: Keyboard&Mouse
objectReference: {fileID: 0}
- target: {fileID: -3976935303735240655, guid: 5d4e7d89f2ca88444a90ee28fd289237, type: 3}
propertyPath: m_ActionEvents.Array.size
value: 14
objectReference: {fileID: 0}
- target: {fileID: -3976935303735240655, guid: 5d4e7d89f2ca88444a90ee28fd289237, type: 3}
propertyPath: m_NeverAutoSwitchControlSchemes
value: 0
objectReference: {fileID: 0}
- target: {fileID: -3976935303735240655, guid: 5d4e7d89f2ca88444a90ee28fd289237, type: 3}
propertyPath: m_ActionEvents.Array.data[13].m_ActionId
value: 10927b2d-68ce-430a-bce1-c5a1f1d3daf0
Expand Down
18 changes: 16 additions & 2 deletions Assets/Scripts/GameManagerScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,30 @@ public class GameManagerScript : MonoBehaviour

public bool isNavMeshBaked = false;

List<GameObject> enemies = new List<GameObject>();
// Referência.
GameObject? gamepadGroup;
GameObject? menuPanel;
GameObject? geometry;
List<GameObject> enemies = new List<GameObject>();
RoomNetwork roomNetwork = new RoomNetwork();

// State.
MenuState menuState = MenuState.closed;
ControlState controlState = ControlState.gamepad;

public static GameManagerScript instance = default!;

// Getters de state.
public MenuState GetMenuState => menuState;
public ControlState GetControlState => controlState;

// Getters de tipo primitivo.
public string GetTargetControlScheme => controlState == ControlState.keyboard
? ControlSchemes.keyboardMouse : ControlSchemes.gamepad;

// Getters de referência.
public GameObject GetGeometry => geometry ?? default!;
public RoomNetwork GetRoomNetwork => roomNetwork;

public MapGenerationSettings GetMapGenerationSettings =>
mapGenerationSettings;
public EnemySpawnSettings GetEnemySpawnSettings => enemySpawnSettings;
Expand All @@ -47,6 +57,8 @@ public void DisableGamepad()

gamepadGroup.SetActive(false);

controlState = ControlState.keyboard;

LocalPrefs.SetGamepadEnabled(false);
}

Expand All @@ -71,6 +83,8 @@ public void EnableGamepad()

gamepadGroup.SetActive(true);

controlState = ControlState.gamepad;

LocalPrefs.SetGamepadEnabled(true);
}

Expand Down
86 changes: 32 additions & 54 deletions Assets/Scripts/PlayerScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@ public class PlayerScript : MonoBehaviour
PlayerInput? playerInput;
Vector2 currentMoveInput;
Vector2 processedMoveInput;
Vector2 lastMove;
Vector3 characterVelocity;
Animator? anima;
GameObject? model;
Transform? cameraTransform;
Vector2 currentLookInput;
Vector2 lastLook;

float lastMoveTimestamp;
int killCount;
bool attackLock;
float lastLookTimestamp;

public PlayerInput? GetPlayerInput => playerInput;

System.Collections.IEnumerator AttackRoutine()
{
Expand Down Expand Up @@ -109,6 +107,16 @@ void HandleCameraTransforms(Transform cameraTransform)
);
}

public void HandleCharacterControllerUpdate()
{
var tempCharacterVelocity = controller?.velocity ?? Vector3.zero;

if (tempCharacterVelocity.magnitude > .1f)
{
characterVelocity = tempCharacterVelocity;
}
}

void HandleConfigInitialization()
{
GameManagerScript.instance.DisableMenu();
Expand Down Expand Up @@ -162,70 +170,37 @@ void HandleMovement(CharacterController controller)

public void Look(InputAction.CallbackContext context)
{
// TODO: Arrumar o flick do analógico
var value = context.ReadValue<Vector2>();

if (value.magnitude == 0)
{
if (lastLook.magnitude == 0)
{
currentLookInput = Vector2.zero;
}
}
else
if (
playerInput?.currentControlScheme !=
GameManagerScript.instance.GetTargetControlScheme
)
{
currentLookInput = value;

if (currentLookInput.magnitude > .5f && !attackLock)
{
StartCoroutine(AttackRoutine());
}
return;
}

lastLook = value;
lastLookTimestamp = Time.timeSinceLevelLoad;
}

public void Move(InputAction.CallbackContext context)
{
var value = context.ReadValue<Vector2>();
currentLookInput = value;

if (value.magnitude == 0)
if (currentLookInput.magnitude > .5f && !attackLock)
{
if (lastMove.magnitude == 0)
{
currentMoveInput = Vector2.zero;
}
}
else
{
currentMoveInput = value;
StartCoroutine(AttackRoutine());
}

lastMove = value;
lastMoveTimestamp = Time.timeSinceLevelLoad;
}

public void MoveCheck()
public void Move(InputAction.CallbackContext context)
{
// TODO: Arrumar o flick do analógico
var value = playerInput?.actions["Move"].ReadValue<Vector2>();
var value = context.ReadValue<Vector2>();

if (
value?.magnitude == 0 &&
lastMove.magnitude == 0 &&
Time.timeSinceLevelLoad - lastMoveTimestamp > .01f
playerInput?.currentControlScheme !=
GameManagerScript.instance.GetTargetControlScheme
)
{
currentMoveInput = Vector2.zero;
return;
}

var tempCharacterVelocity = controller?.velocity ?? Vector3.zero;

if (tempCharacterVelocity.magnitude > .1f)
{
characterVelocity = tempCharacterVelocity;
}
currentMoveInput = value;
}

public void OnMenuClick()
Expand Down Expand Up @@ -261,14 +236,15 @@ void Start()

void Update()
{
switch (GameManagerScript.instance.GetMenuState)
var manager = GameManagerScript.instance;

switch (manager.GetMenuState)
{
case MenuState.closed:
MoveCheck();

// TODO: Mover

if (Input.mousePresent)
if (manager.GetControlState == ControlState.keyboard)
{
var mousePosition = Mouse.current.position;

Expand All @@ -284,6 +260,8 @@ void Update()
break;
}

HandleCharacterControllerUpdate();

if (controller != null)
{
HandleMovement(controller: controller);
Expand Down
5 changes: 5 additions & 0 deletions Assets/Scripts/States/ControlState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public enum ControlState
{
keyboard,
gamepad,
}
11 changes: 11 additions & 0 deletions Assets/Scripts/States/ControlState.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/Scripts/Utils/ControlSchemes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
public class ControlSchemes
{
public static readonly string keyboardMouse = "Keyboard&Mouse";
public static readonly string gamepad = "Gamepad";
public static readonly string touch = "Touch";
public static readonly string joystick = "Joystick";
public static readonly string xr = "XR";
}
11 changes: 11 additions & 0 deletions Assets/Scripts/Utils/ControlSchemes.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions Assets/Scripts/Utils/Layers.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using UnityEngine;

class Layers
public class Layers
{
public static int defaultLayer = 0;
public static int uiLayer = 5;
public static int enemyLayer = 6;
public static int geometryLayer = 7;
public static readonly int defaultLayer = 0;
public static readonly int uiLayer = 5;
public static readonly int enemyLayer = 6;
public static readonly int geometryLayer = 7;

public static int enemyMask = ((int)Mathf.Pow(2, enemyLayer));
public static int geometryMask = ((int)Mathf.Pow(2, geometryLayer));
public static readonly int enemyMask = ((int)Mathf.Pow(2, enemyLayer));
public static readonly int geometryMask = ((int)Mathf.Pow(2, geometryLayer));
}

0 comments on commit f693e88

Please sign in to comment.