feat: 完成碰撞以及攻击逻辑

This commit is contained in:
richarjiang
2025-09-22 16:41:31 +08:00
parent 1f0771b271
commit dd263b6481
42 changed files with 2648 additions and 75 deletions

View File

@@ -1,4 +1,4 @@
import { _decorator, Component, Node, Vec3, input, Input, EventTouch, Camera, view, tween, Animation } from 'cc';
import { _decorator, Component, Node, Vec3, input, Input, EventTouch, Camera, view, tween, Animation, Collider2D, Contact2DType } from 'cc';
import { TiledMapPathfinder } from './TiledMapPathfinder';
const { ccclass, property } = _decorator;
@@ -24,6 +24,7 @@ export class PlayerController extends Component {
mapHeight: number = 2560; // 地图高度
private isMoving: boolean = false;
private isAttacking: boolean = false;
private currentPath: Vec3[] = [];
private currentPathIndex: number = 0;
private originalPosition: Vec3 = new Vec3();
@@ -33,6 +34,12 @@ export class PlayerController extends Component {
onLoad() {
// 注册触摸事件
input.on(Input.EventType.TOUCH_START, this.onTouchStart, this);
let collider = this.player.getComponent(Collider2D);
if (collider) {
// 监听碰撞事件
collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
}
}
onDestroy() {
@@ -47,7 +54,7 @@ export class PlayerController extends Component {
}
private onTouchStart(event: EventTouch) {
if (!this.player || !this.camera || !this.pathfinder) return;
if (!this.player || !this.camera || !this.pathfinder || this.isAttacking) return;
// 获取触摸点的UI坐标
const touchLocation = event.getUILocation();
@@ -205,13 +212,19 @@ export class PlayerController extends Component {
* 移动到路径中的下一个路径点
*/
private moveToNextWaypoint() {
if (this.currentAnimation === 'attack') {
return
}
if (!this.player || this.currentPath.length === 0 || this.currentPathIndex >= this.currentPath.length) {
this.isMoving = false;
this.player.getComponent(Animation)?.play('stand');
this.switchAnimation('stand');
console.log('路径移动完成');
return;
}
const targetPos = this.currentPath[this.currentPathIndex];
const currentPos = this.player.position;
@@ -260,4 +273,18 @@ export class PlayerController extends Component {
// 更新逻辑现在主要由缓动系统处理
// 这里可以添加其他需要每帧更新的逻辑
}
onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D) {
if (otherCollider.node.name.startsWith('guai_')) {
this.isAttacking = true;
// 怪物攻击
const animation = otherCollider.node.getComponent(Animation);
if (animation) {
animation.play(`${otherCollider.node.name}_attack`);
}
// player 攻击
this.switchAnimation('attack');
}
}
}