-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cs
More file actions
198 lines (148 loc) · 3.96 KB
/
Copy pathtest.cs
File metadata and controls
198 lines (148 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerMovement : MonoBehaviour
{
public Sprite northSprite;
public Sprite southSprite;
public Sprite eastSprite;
public Sprite westSprite;
public Sprite northWalk1;
public Sprite northWalk2;
public Sprite southWalk1;
public Sprite southWalk2;
public Sprite eastWalk1;
public Sprite eastWalk2;
public Sprite westWalk1;
public Sprite westWalk2;
public float walkSpeed = 5f;
public bool isAllowedToMove;
/// <summary>
/// Determine if the player is currently in the moving animation
/// </summary>
///
private bool IsMoving = false;
private bool Walk = false;
private SpriteRenderer spriteRender;
// Check if player entered scene fot the first time.
//private Boolean FirstTime = true;
private void Awake()
{
spriteRender = GetComponent<SpriteRenderer>();
isAllowedToMove = true;
}
private void Update()
{
if(isAllowedToMove == true)
InputMovement();
}
private void InputMovement()
{
if (IsMoving) return;
Vector2 input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
if (input == Vector2.zero) return;
if (Mathf.Abs(input.x) > Mathf.Abs(input.y)) input.y = 0; else input.x = 0;
Vector2 dir = input.normalized;
dir = new Vector2(dir.x / 1, dir.y / 1);
SwapSpriteByDirection(dir);
PerformMovement(dir);
}
private void SwapSpriteByDirection(Vector2 direction)
{
if (direction.x > 0)
{
if (Walk) {
spriteRender.sprite = eastWalk1;
Walk = false;
} else {
spriteRender.sprite = eastWalk2;
Walk = true;
}
}
else if (direction.x < 0)
{
if (Walk) {
spriteRender.sprite = westWalk1;
Walk = false;
} else {
spriteRender.sprite = westWalk2;
Walk = true;
}
}
else if (direction.y > 0)
{
if (Walk) {
spriteRender.sprite = northWalk1;
Walk = false;
} else {
spriteRender.sprite = northWalk2;
Walk = true;
}
}
else if (direction.y < 0)
{
if (Walk) {
spriteRender.sprite = southWalk1;
Walk = false;
} else {
spriteRender.sprite = southWalk2;
Walk = true;
}
}
}
private void PerformMovement(Vector2 direction)
{
// check if the player can even move there
RaycastHit2D[] ray = Physics2D.RaycastAll(gameObject.transform.position, direction, 1f); //player moves by 0.5
bool pathBlocked = ray.Any(i => i.collider.GetComponent<Collidable>() != null);
if (pathBlocked) return;
StartCoroutine(MoveAnimation(direction));
}
private IEnumerator MoveAnimation(Vector2 direction)
{
Vector2 goal = (Vector2)gameObject.transform.position + direction;
IsMoving = true;
float t = 0;
float rate = 1 / 0.3f;
Vector2 start = gameObject.transform.position;
while (Vector2.Distance(gameObject.transform.position, goal) > Mathf.Epsilon)
{
t += Time.deltaTime * rate;
gameObject.transform.position = Vector2.MoveTowards(start, goal, t);
yield return new WaitForFixedUpdate();
}
IsMoving = false;
switch (spriteRender.sprite.name)
{
case "North_1":
spriteRender.sprite = northSprite;
break;
case "North_2":
spriteRender.sprite = northSprite;
break;
case "East_1":
spriteRender.sprite = eastSprite;
break;
case "East_2":
spriteRender.sprite = eastSprite;
break;
case "West_1":
spriteRender.sprite = westSprite;
break;
case "West_2":
spriteRender.sprite = westSprite;
break;
case "South_1":
spriteRender.sprite = southSprite;
break;
case "South_2":
spriteRender.sprite = southSprite;
break;
default:
break;
}
}
}