1- using System . Collections ;
1+ using System . Collections ;
22using System . Collections . Generic ;
33using UnityEngine ;
44using UnityEngine . UI ;
@@ -7,7 +7,27 @@ public class PlayerController : MonoBehaviour
77 private Rigidbody2D rb ;
88 private Collider2D coll ;
99 private Animator anim ;
10-
10+
11+ public GameObject sword ;
12+ public GameObject below_sword ;
13+ public GameObject up_sword ;
14+ private GameObject currentSword ;
15+ public GameObject hit ;
16+
17+ //蓄力时间
18+ private float attackTime ;
19+
20+ private float hp ;
21+ private float mp ;
22+
23+ [ Header ( "生命" ) ]
24+ [ SerializeField ]
25+ private float maxHp ;
26+
27+ [ Header ( "能量" ) ]
28+ [ SerializeField ]
29+ private float maxMp ;
30+
1131 private float horizontalMove ;
1232
1333 [ Header ( "速度" ) ]
@@ -49,8 +69,7 @@ public class PlayerController : MonoBehaviour
4969
5070 bool jumpPressed ;
5171
52- [ Header ( "最大跳跃次数" ) ]
53- [ SerializeField ]
72+
5473 private int jumpCount ;
5574
5675 [ Header ( "Better Jump重力系数" ) ]
@@ -68,29 +87,49 @@ public class PlayerController : MonoBehaviour
6887 private float dashTimeLeft ;
6988 public float dashSpeed ;
7089 private float tsped ;
90+ [ Header ( "CD的UI软件" ) ]
91+ public Image cdImage ;
7192
72- [ Header ( "Hurt" ) ]
93+
94+ [ Header ( "Hurt无敌" ) ]
7395 public float hurtLength ; //MARK 自定义计数器长度
7496 private float hurtCount ; //MARK 计数器 每帧减少
7597 [ HideInInspector ] public bool isAttacked ;
7698
7799 // Start is called before the first frame update
78100 void Start ( )
79101 {
80-
81-
82- hp = maxHp ;
83- mp = maxMp ;
84- currentSpeedX = 0 ;
85102 rb = GetComponent < Rigidbody2D > ( ) ;
86103 coll = GetComponent < Collider2D > ( ) ;
87104 anim = GetComponent < Animator > ( ) ;
105+ attackTime = 0 ;
106+ /* sword = transform.Find("wavePosition/sword").gameObject;
107+ below_sword = transform.Find("wavePosition/below_sword").gameObject;
108+ up_sword= transform.Find("wavePosition/up_sword").gameObject;
109+ hit = transform.Find("wavePosition/attack").gameObject;*/
110+ hp = maxHp ;
111+ mp = maxMp ;
112+ currentSpeedX = 0 ;
113+ hurtCount = hurtLength ;
114+ isAttacked = false ;
115+
116+ Debug . Log ( rb ) ;
117+
88118 }
89119
90120 // Update is called once per frame
91121 void Update ( )
92122 {
93-
123+ //受击无敌
124+ if ( isAttacked )
125+ {
126+ hurtCount -= Time . deltaTime ;
127+ if ( hurtCount <= 0 )
128+ {
129+ isAttacked = false ;
130+ }
131+ }
132+
94133 if ( Input . GetKeyDown ( KeyCode . A ) )
95134 {
96135 wave ( ) ;
@@ -104,7 +143,25 @@ void Update()
104143 {
105144 if ( Time . time >= ( dashInterval + lastDash ) )
106145 ReadyToDash ( ) ;
107- }
146+ }
147+
148+ if ( Input . GetKeyDown ( KeyCode . Z ) )
149+ {
150+ if ( Input . GetKey ( KeyCode . DownArrow ) && ! isGround )
151+ {
152+ currentSword = below_sword ;
153+ // Debug.Log("down");
154+ }
155+ else if ( Input . GetKey ( KeyCode . UpArrow ) )
156+ {
157+ currentSword = up_sword ;
158+ }
159+ else
160+ {
161+ currentSword = sword ;
162+ }
163+ Attack ( ) ;
164+ }
108165 if ( Input . GetKey ( KeyCode . Z ) )
109166 {
110167 attackTime += Time . deltaTime ;
@@ -123,9 +180,11 @@ void Update()
123180
124181 private void FixedUpdate ( )
125182 {
183+ //Debug.Log(rb);
126184 tsped = rb . velocity . y ;
127185 isGround = Physics2D . OverlapBox ( groundCheck . position , boxSize , 0 , ground ) ;
128186 isClimbing = Physics2D . OverlapCircle ( wallCheck . position , .1f , ground ) ;
187+ Debug . Log ( isClimbing ) ;
129188
130189 if ( isGround )
131190 {
@@ -157,7 +216,11 @@ private void FixedUpdate()
157216 SwitchAnim ( ) ;
158217 }
159218
160-
219+ private void OnDrawGizmos ( )
220+ {
221+ Gizmos . DrawWireCube ( groundCheck . position , boxSize ) ;
222+ Gizmos . color = Color . black ;
223+ }
161224
162225 void GroundMovement ( )
163226 {
@@ -170,8 +233,27 @@ void GroundMovement()
170233 rb . velocity = new Vector2 ( horizontalMove * speed + currentSpeedX , rb . velocity . y ) ;
171234
172235
173- transform . localScale = new Vector3 ( horizontalMove , 1 , 1 ) ;
174-
236+ if ( currentSword != null && currentSword . activeSelf )
237+ {
238+ /* if (transform.localScale.x != horizontalMove)
239+ {
240+
241+ }*/
242+ //horizontalMove = 0;
243+ }
244+ else
245+ {
246+ if ( horizontalMove != 0 )
247+ {
248+ if ( horizontalMove > 0 )
249+ transform . localScale = new Vector3 ( Mathf . Abs ( transform . localScale . x ) , transform . localScale . y , 1 ) ;
250+ else
251+ {
252+
253+ transform . localScale = new Vector3 ( - Mathf . Abs ( transform . localScale . x ) , transform . localScale . y , 1 ) ;
254+ }
255+ }
256+ }
175257
176258
177259 if ( currentSpeedX > 0 )
@@ -189,6 +271,10 @@ void GroundMovement()
189271
190272 }
191273
274+ void Attack ( )
275+ {
276+ currentSword . SetActive ( true ) ;
277+ }
192278 void ClimbJump ( )
193279 {
194280
@@ -206,17 +292,19 @@ void ClimbJump()
206292 {
207293 rb . velocity = new Vector2 ( speedX , jumpForce ) ;
208294 currentSpeedX = speedX * transform . localScale . x ;
295+ Debug . Log ( "current" + currentSpeedX ) ;
209296 }
210297 }
298+
211299 jumpPressed = false ;
212300
213301 if ( rb . velocity . x > 0 )
214302 {
215- transform . localScale = new Vector3 ( 1 , 1 , 1 ) ;
303+ transform . localScale = new Vector3 ( Mathf . Abs ( transform . localScale . x ) , transform . localScale . y , 1 ) ;
216304 }
217305 else if ( rb . velocity . x < 0 )
218306 {
219- transform . localScale = new Vector3 ( - 1 , 1 , 1 ) ;
307+ transform . localScale = new Vector3 ( - Mathf . Abs ( transform . localScale . x ) , transform . localScale . y , 1 ) ;
220308 }
221309
222310
@@ -269,6 +357,28 @@ void BetterJump()//跳跃
269357
270358 }
271359
360+ void SwitchAnim ( ) //动画切换
361+ {
362+ /*anim.Play("Idle");
363+ anim.SetFloat("running", Mathf.Abs(rb.velocity.x));
364+
365+ if (isGround&& Mathf.Abs(rb.velocity.x)!=0)
366+ {
367+ anim.Play("Run");
368+
369+
370+ }
371+ else if (!isGround && rb.velocity.y > 0)
372+ {
373+ anim.Play("Jump");
374+
375+ }
376+ else if (!isGround && rb.velocity.y < 0)
377+ {
378+ anim.Play("Fall");
379+
380+ }*/
381+ }
272382
273383 void ReadyToDash ( )
274384 {
@@ -277,7 +387,7 @@ void ReadyToDash()
277387 lastDash = Time . time ;
278388 cdImage . fillAmount = 1.0f ;
279389 }
280- void Dash ( )
390+ void Dash ( )
281391 {
282392 if ( isDashing )
283393 {
@@ -305,4 +415,38 @@ void Dash()
305415 }
306416 }
307417 }
418+
419+ void wave ( )
420+ {
421+ if ( mp > 0 )
422+ {
423+ mp -= 1 ;
424+ Transform a = Instantiate ( transform . Find ( "wavePosition/wave" ) ) ;
425+ a . gameObject . SetActive ( true ) ;
426+ }
427+ }
428+
429+ public void getAttacked ( float damage )
430+ {
431+ if ( ! isAttacked && ! isDashing )
432+ {
433+
434+ hp -= damage ;
435+ maxHp = hp ;
436+
437+ if ( hp <= 0 )
438+ {
439+ //death
440+ }
441+ else
442+ {
443+ //hurt
444+
445+ }
446+
447+ hurtCount = hurtLength ;
448+ isAttacked = true ;
449+ }
450+
451+ }
308452}
0 commit comments