A 2D Farming RPG
This is just a simple 2D farming RPG game
Loading...
Searching...
No Matches
Player.cs
Go to the documentation of this file.
1using System.Collections.Generic;
2using UnityEngine;
3
7public class Player : SingletonMonobehaviour<Player>
8{
11
12 // Movement Parameters
13 private float m_XInput;
14 private float m_YInput;
15 private bool m_IsWalking;
16 private bool m_IsRunning;
17 private bool m_IsIdle;
18 private bool m_IsCarrying = false;
19 private bool m_IsUsingToolRight;
20 private bool m_IsUsingToolLeft;
21 private bool m_IsUsingToolUp;
22 private bool m_IsUsingToolDown;
24 private bool m_IsLiftingToolLeft;
25 private bool m_IsLiftingToolUp;
26 private bool m_IsLiftingToolDown;
29 private bool m_IsSwingingToolUp;
31 private bool m_IsPickingRight;
32 private bool m_IsPickingLeft;
33 private bool m_IsPickingUp;
34 private bool m_IsPickingDown;
36
37 private Camera m_MainCamera;
38
39 private Rigidbody2D m_RigidBody2D;
40
41#pragma warning disable 414
43#pragma warning restore 414
44
45 private List<CharacterAttribute> m_CharacterAttributeCustomisationList;
46
47 private float m_MovementSpeed;
48
49 [Tooltip("Should be populated in the prefab with the equipped item sprite renderer")]
50 [SerializeField] private SpriteRenderer m_EquippedItemSpriteRenderer = null;
51
52 // Player attributes that can be swapped
55
56 private bool m_PlayerInputIsDisabled = false;
57
59 {
61 set => m_PlayerInputIsDisabled = value;
62 }
63
67 private void Awake()
68 {
69 base.Awake();
70 m_RigidBody2D = GetComponent<Rigidbody2D>();
71
72 m_AnimationOverrides = GetComponentInChildren<AnimationOverrides>();
73
74 // Initialize swappable character attributes
76
77 // Initialize character attribute list
78 m_CharacterAttributeCustomisationList = new List<CharacterAttribute>();
79
80 // Get a reference to the main camera
81 m_MainCamera = Camera.main;
82 }
83
87 private void Start()
88 {
89 m_GridCursor = GameObject.FindObjectOfType<GridCursor>();
90 }
91
96 private void Update()
97 {
98 #region Player Input
99
101 {
103
105
107
109
111
112
113
114 // Send event to any listeners for player movement input
122 false, false, false, false);
123 }
124
125 #endregion
126 }
127
131 private void FixedUpdate()
132 {
134 }
135
139 private void PlayerMovement()
140 {
141 Vector2 move = new Vector2( m_XInput * m_MovementSpeed * Time.deltaTime, m_YInput * m_MovementSpeed * Time.deltaTime );
142 m_RigidBody2D.MovePosition( m_RigidBody2D.position + move );
143 }
144
149 {
150 m_IsUsingToolRight = false;
151 m_IsUsingToolLeft = false;
152 m_IsUsingToolUp = false;
153 m_IsUsingToolDown = false;
154 m_IsLiftingToolRight = false;
155 m_IsLiftingToolLeft = false;
156 m_IsLiftingToolUp = false;
157 m_IsLiftingToolDown = false;
158 m_IsSwingingToolRight = false;
159 m_IsSwingingToolLeft = false;
160 m_IsSwingingToolUp = false;
161 m_IsSwingingToolDown = false;
162 m_IsPickingRight = false;
163 m_IsPickingLeft = false;
164 m_IsPickingUp = false;
165 m_IsPickingDown = false;
167 }
168
172 private void PlayerMovementInput()
173 {
174 m_XInput = Input.GetAxisRaw( "Horizontal" );
175 m_YInput = Input.GetAxisRaw( "Vertical" );
176
177 // Handle where you press both x and y at the same time.
178 // Move Diagonally with the same speed.
179 if( ( m_XInput != 0 ) || ( m_YInput != 0 ) )
180 {
181 m_XInput = m_XInput * 0.71f;
182 m_YInput = m_YInput * 0.71f;
183 }
184
185 if( ( m_XInput != 0 ) || ( m_YInput != 0 ) )
186 {
187 m_IsRunning = true;
188 m_IsWalking = false;
189 m_IsIdle = false;
191
192 // Capture player direction for save game
193 if( m_XInput < 0 )
194 {
196 }
197 else if( m_XInput > 0 )
198 {
200 }
201 else if( m_YInput < 0 )
202 {
204 }
205 else
206 {
208 }
209 }
210 else if( ( m_XInput == 0 ) && ( m_YInput == 0 ) )
211 {
212 m_IsRunning = false;
213 m_IsWalking = false;
214 m_IsIdle = true;
215 }
216 }
217
221 private void PlayerWalkInput()
222 {
223 if( Input.GetKey( KeyCode.LeftShift ) || Input.GetKey( KeyCode.RightShift ) )
224 {
225 m_IsRunning = false;
226 m_IsWalking = true;
227 m_IsIdle = false;
229 }
230 else
231 {
232 m_IsRunning = true;
233 m_IsWalking = false;
234 m_IsIdle = false;
236 }
237 }
238
243 private void PlayerClickInput()
244 {
245 if( Input.GetMouseButton(0) )
246 {
248 {
250 }
251 }
252 }
253
258 {
260
261 // Get selected item details
262 ItemDetails itemDetails = InventoryManager.Instance.GetSelectedInventoryItemDetails( InventoryLocation.Player );
263
264 if( itemDetails != null )
265 {
266 switch( itemDetails.m_ItemType )
267 {
268 case ItemType.Seed:
269 if( Input.GetMouseButtonDown(0) )
270 {
271 ProcessPlayerClickInputSeed( itemDetails );
272 }
273 break;
274
275 case ItemType.Commodity:
276 if( Input.GetMouseButtonDown(0) )
277 {
279 }
280 break;
281
282 case ItemType.None:
283 break;
284
285 case ItemType.Count:
286 break;
287
288 default:
289 break;
290 }
291 }
292 }
293
298 private void ProcessPlayerClickInputSeed( ItemDetails itemDetails )
299 {
301 {
303 }
304 }
305
311 {
313 {
315 }
316 }
317
322 private void PlayerTestInput()
323 {
324 // Trigger advance time
325 if( Input.GetKey( KeyCode.T ) )
326 {
327 TimeManager.Instance.TestAdvanceGameMinute();
328 }
329
330 // Trigger advance day
331 if( Input.GetKey( KeyCode.G) )
332 {
333 TimeManager.Instance.TestAdvanceGameDay();
334 }
335
336 // Test scene unload / load
337 if( Input.GetKey( KeyCode.L ) )
338 {
339 SceneControllerManager.Instance.FadeAndLoadScene( SceneName.Scene1_Farm.ToString(), transform.position );
340 }
341 }
342
346 private void ResetMovement()
347 {
348 m_XInput = 0f;
349 m_YInput = 0f;
350 m_IsRunning = false;
351 m_IsWalking = false;
352 m_IsIdle = true;
353 }
354
359 {
362
363 // Send event to any listeners for player movement input
371 false, false, false, false);
372 }
373
377 public void DisablePlayerInput()
378 {
380 }
381
385 public void EnablePlayerInput()
386 {
387 PlayerInputIsDisabled = false;
388 }
389
393 public void ClearCarriedItem()
394 {
395 m_EquippedItemSpriteRenderer.sprite = null;
396 m_EquippedItemSpriteRenderer.color = new Color( 1f, 1f, 1f, 0f );
397
398 // Apply base character arms customisation.
399 m_ArmsCharacterAttribute.partVariantType = PartVariantType.NONE;
403
404 m_IsCarrying = false;
405 }
406
411 public void ShowCarriedItem( int itemCode )
412 {
413 ItemDetails itemDetails = InventoryManager.Instance.GetItemDetails( itemCode );
414
415 if( itemDetails != null )
416 {
417 m_EquippedItemSpriteRenderer.sprite = itemDetails.m_ItemSprite;
418 m_EquippedItemSpriteRenderer.color = new Color( 1f, 1f, 1f, 1f );
419
420 // Apply 'carry' character arms customisation
421 m_ArmsCharacterAttribute.partVariantType = PartVariantType.CARRY;
425
426 m_IsCarrying = true;
427 }
428 }
429
435 {
436 // Vector3 viewport position for player (0, 0) viewport bottom left, (1, 1) viewport top right
437 return m_MainCamera.WorldToViewportPoint( transform.position );
438 }
439}
Direction
Describes the player facing direction
Definition: Enums.cs:150
ToolEffect
Describes the tool effect
Definition: Enums.cs:141
PartVariantType
Describes the part variant type
Definition: Enums.cs:79
InventoryLocation
Describes the type of inventory and count Ex: player inventory or chest inventory
Definition: Enums.cs:108
SceneName
Describes different scenes.
Definition: Enums.cs:118
ItemType
Describe the item type
Definition: Enums.cs:162
PartVariantColor
Describe the part variant color Currently we don't use that because we don't have the assets with dif...
Definition: Enums.cs:70
CharacterPartAnimator
Describes the character part animator. Each body part has a separate animator
Definition: Enums.cs:55
void ApplyCharacterCustomisationParameters(List< CharacterAttribute > characterAttributesList)
Handles the animation overrides
Raise an handle event when a movement is triggered.
Definition: EventHandler.cs:15
static void CallMovementEvent(float xInput, float yInput, bool isWalking, bool isRunning, bool isIdle, bool isCarrying, ToolEffect toolEffect, bool isUsingToolRight, bool isUsingToolLeft, bool isUsingToolUp, bool isUsingToolDown, bool isLiftingToolRight, bool isLiftingToolLeft, bool isLiftingToolUp, bool isLiftingToolDown, bool isPickingRight, bool isPickingLeft, bool isPickingUp, bool isPickingDown, bool isSwingingToolRight, bool isSwingingToolLeft, bool isSwingingToolUp, bool isSwingingToolDown, bool idleRight, bool idleLeft, bool idleUp, bool idleDown)
Movement Event Call For Publishers.
Definition: EventHandler.cs:47
static void CallDropSelectedItemEvent()
Definition: EventHandler.cs:19
bool CursorIsEnabled
Definition: GridCursor.cs:23
bool CursorPositionIsValid
Definition: GridCursor.cs:20
This class will manage all the inventory related operations.
This class holds information of items.
Definition: ItemDetails.cs:8
bool m_CanBeDropped
Definition: ItemDetails.cs:21
Sprite m_ItemSprite
Definition: ItemDetails.cs:12
ItemType m_ItemType
Definition: ItemDetails.cs:17
This class has all the things related to player
Definition: Player.cs:8
bool m_IsUsingToolUp
Definition: Player.cs:21
bool m_IsIdle
Definition: Player.cs:17
void Start()
Initialize grid cursor variable.
Definition: Player.cs:87
bool m_IsUsingToolRight
Definition: Player.cs:19
bool m_IsPickingDown
Definition: Player.cs:34
void DisablePlayerInputAndResetMovement()
Disable input and reset movement.
Definition: Player.cs:358
float m_XInput
Definition: Player.cs:13
List< CharacterAttribute > m_CharacterAttributeCustomisationList
Definition: Player.cs:45
void ProcessPlayerClickInputCommodity(ItemDetails itemDetails)
Check the position is valid and the item can be dropped for commodity.
Definition: Player.cs:310
void PlayerMovement()
Calculate player movements.
Definition: Player.cs:139
bool m_IsLiftingToolDown
Definition: Player.cs:26
float m_MovementSpeed
Definition: Player.cs:47
bool m_IsLiftingToolUp
Definition: Player.cs:25
bool m_IsLiftingToolRight
Definition: Player.cs:23
Direction m_PlayerDirection
Definition: Player.cs:42
bool m_IsPickingLeft
Definition: Player.cs:32
bool m_IsUsingToolDown
Definition: Player.cs:22
void PlayerWalkInput()
Handle player walk inputs.
Definition: Player.cs:221
Camera m_MainCamera
Definition: Player.cs:37
float m_YInput
Definition: Player.cs:14
void ProcessPlayerClickInput()
Definition: Player.cs:257
Vector3 GetPlayerViewportPosition()
Check the player position in the gameworld.
Definition: Player.cs:434
bool m_IsPickingUp
Definition: Player.cs:33
CharacterAttribute m_ToolCharacterAttribute
Definition: Player.cs:54
bool m_IsSwingingToolRight
Definition: Player.cs:27
void ProcessPlayerClickInputSeed(ItemDetails itemDetails)
Check the position is valid and the item can be dropped for seed.
Definition: Player.cs:298
AnimationOverrides m_AnimationOverrides
Definition: Player.cs:9
void PlayerMovementInput()
Handle player inputs and normalized that.
Definition: Player.cs:172
SpriteRenderer m_EquippedItemSpriteRenderer
Definition: Player.cs:50
CharacterAttribute m_ArmsCharacterAttribute
Definition: Player.cs:53
void FixedUpdate()
Handle player movements.
Definition: Player.cs:131
bool m_IsWalking
Definition: Player.cs:15
bool m_IsSwingingToolLeft
Definition: Player.cs:28
bool m_IsSwingingToolDown
Definition: Player.cs:30
bool m_IsLiftingToolLeft
Definition: Player.cs:24
void ResetAnimatioTriggers()
Reset the animation triggers.
Definition: Player.cs:148
void Update()
Handle movement inputs and publish animation events.
Definition: Player.cs:96
void Awake()
Initialize members
Definition: Player.cs:67
bool m_PlayerInputIsDisabled
Definition: Player.cs:56
void ResetMovement()
Reset Movement
Definition: Player.cs:346
bool m_IsSwingingToolUp
Definition: Player.cs:29
bool m_IsCarrying
Definition: Player.cs:18
void DisablePlayerInput()
Disable Player Input
Definition: Player.cs:377
void PlayerClickInput()
If the grid cursor is enabled and player click left mouse button call ProcessPlayerClickInput()
Definition: Player.cs:243
GridCursor m_GridCursor
Definition: Player.cs:10
bool m_IsPickingRight
Definition: Player.cs:31
void PlayerTestInput()
TODO: Remove before final product.
Definition: Player.cs:322
bool m_IsUsingToolLeft
Definition: Player.cs:20
bool PlayerInputIsDisabled
Definition: Player.cs:59
bool m_IsRunning
Definition: Player.cs:16
Rigidbody2D m_RigidBody2D
Definition: Player.cs:39
void ShowCarriedItem(int itemCode)
Show the carried item.
Definition: Player.cs:411
ToolEffect m_ToolEffect
Definition: Player.cs:35
void EnablePlayerInput()
Enable Player Input
Definition: Player.cs:385
void ClearCarriedItem()
Clear the carry animation
Definition: Player.cs:393
Manage the transition between scenes
This class handle the game settings.
Definition: Settings.cs:9
const float m_WalkingSpeed
Definition: Settings.cs:23
const float m_RunningSpeed
Player Movements.
Definition: Settings.cs:22
Abstract class that inherited by all the game managers. All the game managers use singleton since gam...
A singleton manager class for time managements.
Definition: TimeManager.cs:9
Define what character variant we want to switch into.