碰撞系统重做
This commit is contained in:
@@ -25,7 +25,7 @@ export enum BoxSet {
|
||||
MONSTER_START = 300,
|
||||
END_POINT = 360,
|
||||
//游戏地平线
|
||||
GAME_LINE = 0,
|
||||
GAME_LINE = 10,
|
||||
CSKILL_X = 280,
|
||||
CSKILL_Y = 420,
|
||||
}
|
||||
@@ -66,44 +66,7 @@ export class MonsterViewComp extends CCComp {
|
||||
|
||||
private timer:Timer = new Timer(0.1); //计时器
|
||||
is_dead:boolean = false; //是否摧毁
|
||||
|
||||
/** 视图层逻辑代码分离演示 */
|
||||
start () {
|
||||
this.sprite = this.node.getChildByName("avatar").getChildByName("TNode").getChildByName("bb").getComponent(Sprite);
|
||||
this.orginalFlashMaterial = this.sprite.getRenderMaterial(0);
|
||||
// 注册单个碰撞体的回调函数
|
||||
let collider = this.getComponent(Collider2D);
|
||||
if (collider) {
|
||||
collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
|
||||
collider.on(Contact2DType.END_CONTACT, this.onEndContact, this);
|
||||
// collider.on(Contact2DType.PRE_SOLVE, this.onPreSolve, this);
|
||||
collider.on(Contact2DType.POST_SOLVE, this.onPostSolve, this);
|
||||
}
|
||||
// this.node.getChildByName("level").getChildByName("level").getComponent(Label).string = this.level.toString();
|
||||
|
||||
|
||||
}
|
||||
onBeginContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
|
||||
|
||||
|
||||
}
|
||||
onEndContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
|
||||
// 只在两个碰撞体结束接触时被调用一次
|
||||
// console.log('onEndContact');
|
||||
}
|
||||
// onPreSolve (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
|
||||
// // 每次将要处理碰撞体接触逻辑时被调用
|
||||
// console.log('onPreSolve');
|
||||
// }
|
||||
onPostSolve (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
|
||||
if(otherCollider.group== BoxSet.HERO_SKILL || otherCollider.group== BoxSet.MONSTER_SKILL){
|
||||
let skill = otherCollider.node.getComponent(SkillCom)!;
|
||||
// console.log('onPostSolve',skill);
|
||||
this.in_atked();
|
||||
this.hp_change(skill.atk);
|
||||
}
|
||||
}
|
||||
|
||||
is_stop:boolean = false;
|
||||
|
||||
onLoad() {
|
||||
this.as = this.getComponent(MonsterSpine);
|
||||
@@ -112,7 +75,68 @@ export class MonsterViewComp extends CCComp {
|
||||
// EPhysics2DDrawFlags.CenterOfMass |
|
||||
// EPhysics2DDrawFlags.Joint |
|
||||
// EPhysics2DDrawFlags.Shape;
|
||||
} /** 视图层逻辑代码分离演示 */
|
||||
start () {
|
||||
this.sprite = this.node.getChildByName("avatar").getChildByName("TNode").getChildByName("bb").getComponent(Sprite);
|
||||
this.orginalFlashMaterial = this.sprite.getRenderMaterial(0);
|
||||
// 注册单个碰撞体的回调函数
|
||||
let collider = this.getComponent(Collider2D);
|
||||
console.log(collider);
|
||||
if (collider) {
|
||||
collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
|
||||
collider.on(Contact2DType.END_CONTACT, this.onEndContact, this);
|
||||
collider.on(Contact2DType.PRE_SOLVE, this.onPreSolve, this);
|
||||
collider.on(Contact2DType.POST_SOLVE, this.onPostSolve, this);
|
||||
}
|
||||
// this.node.getChildByName("level").getChildByName("level").getComponent(Label).string = this.level.toString();
|
||||
|
||||
|
||||
}
|
||||
onBeginContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
|
||||
}
|
||||
onEndContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
|
||||
// 只在两个碰撞体结束接触时被调用一次
|
||||
// console.log('onEndContact');
|
||||
}
|
||||
onPreSolve (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
|
||||
if(selfCollider.group != otherCollider.group){
|
||||
if(otherCollider.tag != 8){
|
||||
this.stop_cd = 0.5;
|
||||
}
|
||||
}
|
||||
}
|
||||
onPostSolve (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
|
||||
if(otherCollider.group== BoxSet.HERO_SKILL || otherCollider.group== BoxSet.MONSTER_SKILL){
|
||||
let skill = otherCollider.node.getComponent(SkillCom)!;
|
||||
// console.log('onPostSolve',skill);
|
||||
this.in_atked();
|
||||
this.hp_change(skill.atk);
|
||||
}
|
||||
|
||||
if(selfCollider.group == otherCollider.group){
|
||||
console.log('group 相同');
|
||||
if(otherCollider.tag != 8){
|
||||
let self_pos=selfCollider.node.getPosition();
|
||||
let other_pos=otherCollider.node.getPosition();
|
||||
if(selfCollider.group == BoxSet.HERO){
|
||||
if(self_pos.x < other_pos.x&&Math.abs(self_pos.x-other_pos.x) <= 20){
|
||||
this.stop_cd=0.1
|
||||
}
|
||||
}
|
||||
if(selfCollider.group == BoxSet.MONSTER){
|
||||
if(self_pos.x > other_pos.x&&Math.abs(self_pos.x-other_pos.x) <= 20){
|
||||
this.stop_cd=0.1
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
change_name(hero_name:string='hero',scale:number=1){
|
||||
this.name=hero_name;
|
||||
let label:any =this.node.getChildByName("lab_name")
|
||||
@@ -235,7 +259,7 @@ export class MonsterViewComp extends CCComp {
|
||||
});
|
||||
}
|
||||
load_skill(skill_name:string){
|
||||
// console.log("load_skill");
|
||||
console.log("load_skill");
|
||||
let skill = ecs.getEntity<Skill>(Skill);
|
||||
let x=30
|
||||
if(this.scale==1){
|
||||
|
||||
@@ -45,9 +45,9 @@ export class Skill extends ecs.Entity {
|
||||
sv.angle = angle;
|
||||
sv.t_pos = t_pos;
|
||||
if(scale == 1){
|
||||
sv.change_collider_group(BoxSet.HERO_SKILL)
|
||||
sv.box_group=BoxSet.HERO_SKILL
|
||||
}else{
|
||||
sv.change_collider_group(BoxSet.MONSTER_SKILL)
|
||||
sv.box_group=BoxSet.MONSTER_SKILL
|
||||
}
|
||||
this.add(sv);
|
||||
}
|
||||
|
||||
@@ -24,15 +24,13 @@ export class SkillCom extends CCComp {
|
||||
angle:number = 0;
|
||||
t_pos:Vec3 = null;
|
||||
is_destroy:boolean = false;
|
||||
box_group:number = 0;
|
||||
start() {
|
||||
this.node.active=true
|
||||
this.node.angle = this.angle;
|
||||
//根据目标坐标、欧拉角、预设距离增量speed 来计算新的x增量,y增量
|
||||
// this.x_speed = Math.cos(this.angle * Math.PI / 180) * this.speed;
|
||||
// this.y_speed = Math.sin(this.angle * Math.PI / 180) * this.speed;
|
||||
|
||||
|
||||
let collider = this.getComponent(Collider2D);
|
||||
collider.group = this.box_group;
|
||||
if (collider) {
|
||||
// collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
|
||||
collider.on(Contact2DType.POST_SOLVE, this.onPostSolve, this);
|
||||
@@ -86,11 +84,7 @@ export class SkillCom extends CCComp {
|
||||
|
||||
}
|
||||
|
||||
change_collider_group(group:number){
|
||||
let collider = this.getComponent(Collider2D);
|
||||
collider.group = group;
|
||||
}
|
||||
update(deltaTime: number) {
|
||||
update(deltaTime: number) {
|
||||
// this.node.setScale(v3(this.scale,this.node.scale.y,this.node.scale.z))
|
||||
this.move(deltaTime)
|
||||
// if(Math.abs(this.node.position.x) > this.dis)
|
||||
|
||||
@@ -41,10 +41,7 @@ export class baseCom extends Component {
|
||||
reset() {
|
||||
this.node.destroy();
|
||||
}
|
||||
change_collider_group(group:number){
|
||||
let collider = this.getComponent(Collider2D);
|
||||
collider.group = group;
|
||||
}
|
||||
|
||||
update(deltaTime: number) {
|
||||
this.node.setScale(v3(this.scale,this.node.scale.y,this.node.scale.z))
|
||||
this.node.setPosition(v3(this.node.position.x+deltaTime*this.speed*this.scale,this.node.position.y,this.node.position.z))
|
||||
|
||||
Reference in New Issue
Block a user