Files
heros/assets/script/game/Role/RoleViewComp.ts
2024-09-02 09:03:43 +08:00

139 lines
4.9 KiB
TypeScript

/*
* @Author: dgflash
* @Date: 2021-11-18 17:42:59
* @LastEditors: dgflash
* @LastEditTime: 2022-08-17 12:36:18
*/
import { Vec3, v3,_decorator ,Collider2D,Contact2DType,IPhysics2DContact,Material,Sprite,ProgressBar, Prefab, instantiate} from "cc";
import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
import { CCComp } from "../../../../extensions/oops-plugin-framework/assets/module/common/CCComp";
import { RoleSpine } from "./RoleSpine";
import {BoxSet} from "../common/config/BoxSet"
import { smc } from "../common/SingletonModuleComp";
import { SkillCom } from "../skills/SkillCom";
import { Skill } from "../skills/Skill";
import { SkillSet } from "../common/config/SkillSet";
import { Timer } from "../../../../extensions/oops-plugin-framework/assets/core/common/timer/Timer";
import { RandomManager } from "../../../../extensions/oops-plugin-framework/assets/core/common/random/RandomManager";
import { oops } from "../../../../extensions/oops-plugin-framework/assets/core/Oops";
import { MoveToComp } from "../common/ecs/position/MoveTo";
const { ccclass, property } = _decorator;
/** 角色显示组件 */
@ccclass('RoleViewComp') // 定义为 Cocos Creator 组件
@ecs.register('RoleView', false) // 定义为 ECS 组件
export class RoleViewComp extends CCComp {
@property(Material)
hitFlashMaterial: Material;
orginalFlashMaterial: Material;
sprite: Sprite;
/** 角色动画 */
as: RoleSpine = null!;
/** 角色属性 */
hp: number = 1000;
hp_max:number = 1000;
power: number = 0;
stop_cd:number = 0;
atk_cd:number = 3;
atk:number = 10;
skill_uuid:number = 9003;
max_skill_uuid:number = 1001;
shield:number = 0;
shield_max:number = 200;
skin="Character01";
private atk_time:Timer = new Timer(1);
onLoad() {
this.as = this.getComponent(RoleSpine);
}
start () {
// let x = RandomManager.instance.getRandomInt(1,9,2)
// this.as.setSkin("Character0"+x);
this.atk_time = new Timer(this.atk_cd);
this.sprite = this.node.getChildByName("avatar").getChildByName("TNode").getChildByName("bb").getComponent(Sprite);
this.orginalFlashMaterial = this.sprite.getRenderMaterial(0);
console.log("Role view start")
let collider = this.getComponent(Collider2D);
if (collider) {
collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
}
}
onBeginContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
if(otherCollider.tag==BoxSet.SKILL_TAG){
if(selfCollider.group != otherCollider.group){
let skill = otherCollider.node.getComponent(SkillCom)!;
// console.log('onPostSolve',skill);
this.in_atked();
if(this.hp <= 0 ){
return
}
this.hp_change(skill.atk);
}
}
}
// onEndContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {}
// onPreSolve (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {}
// onPostSolve (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {}
update(dt: number){
if (this.atk_time.update(dt)) {
this.toAtk(this.skill_uuid);
}
}
add_hp(hp:number){
console.log("role add hp",hp);
}
setSkin(){
this.as.setSkin(this.skin);
}
reset() {
this.node.destroy();
}
toAtk(uuid) {
this.as.atk();
this.scheduleOnce(()=>{
this.shoot(this.skill_uuid);
},0.5)
}
shoot(skill_uuid:number){
// console.log("monster shoot");
let skill = ecs.getEntity<Skill>(Skill);
let pos = v3(35,55)
let scale = 1
let speed =smc.skills[skill_uuid].speed;
let dis = smc.skills[skill_uuid].dis;
let atk = smc.skills[skill_uuid].atk+this.atk;
let uuid = skill_uuid;
skill.load(pos,speed,dis,scale,this.node,uuid,atk,2);
}
in_atked() {
this.sprite.setSharedMaterial(this.hitFlashMaterial, 0);
this.scheduleOnce(() => {
this.sprite.setSharedMaterial(this.orginalFlashMaterial, 0);
}, 0.1);
// var path = "game/skills/atked";
// var prefab: Prefab = oops.res.get(path, Prefab)!;
// var node = instantiate(prefab);
// let pos = v3(0,30)
// node.setPosition(pos)
// node.parent = this.node;
}
hp_change(hp: number){
this.hp -= hp;
if(this.hp > this.hp_max){
this.hp = this.hp_max;
}
let hp_progress= this.hp/this.hp_max;
this.node.getChildByName("hp").getComponent(ProgressBar)!.progress = hp_progress;
if(this.hp <= 0){
console.log("dead");
}
}
}