Skip to content

Commit aae8c19

Browse files
committed
Merge branch 'main' of github.com:FZUSESPR21/SolidKnight
2 parents cdda41c + 25cd408 commit aae8c19

4 files changed

Lines changed: 273 additions & 28 deletions

File tree

PlayerController.cs

Lines changed: 162 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections;
1+
using System.Collections;
22
using System.Collections.Generic;
33
using UnityEngine;
44
using 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
}

Sword.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public class Sword : MonoBehaviour
6+
{
7+
[SerializeField] private float attackDemage;//伤害
8+
9+
public void EndAttack()
10+
{
11+
gameObject.SetActive(false);
12+
}
13+
14+
15+
private void OnTriggerEnter2D(Collider2D collision)
16+
{
17+
if (collision.gameObject.tag == "Monster")
18+
{
19+
20+
21+
/*if (!collision.gameObject.GetComponent<Enemy>().isAttacked)
22+
{
23+
//Debug.Log("attack");
24+
collision.gameObject.GetComponent<Enemy>().getAttacked(attackDemage);
25+
//Quaternion.identity不旋转
26+
27+
//击退效果 向着反方向运动 怪物位置 - 角色位置
28+
Vector2 p = collision.transform.position - transform.position;
29+
//向量标准化
30+
p = p.normalized;
31+
collision.transform.position = new Vector2(collision.transform.position.x + p.x, collision.transform.position.y + p.y);
32+
}*/
33+
}
34+
}
35+
}

Wave.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public class Wave: MonoBehaviour
6+
7+
{
8+
Rigidbody2D rb;
9+
[SerializeField]
10+
private float speed;
11+
public GameObject target;
12+
public GameObject position;
13+
public float tt = 1;
14+
private float dir;
15+
// Update is called once per frame
16+
private void Start()
17+
{
18+
transform.position = position.transform.position;
19+
dir = target.transform.localScale.x;
20+
//transform.gameObject.SetActive(true);
21+
}
22+
void Update()
23+
{
24+
transform.position += new Vector3(dir * speed * Time.deltaTime, 0, 0);
25+
tt -= Time.deltaTime;
26+
if (tt <= 0)
27+
{
28+
destroy();
29+
}
30+
}
31+
32+
33+
34+
public void destroy()
35+
{
36+
Destroy(gameObject);
37+
}
38+
}

0 commit comments

Comments
 (0)