添加2个游戏层,敌方精灵随机出现在3个层内

This commit is contained in:
walkpan
2024-08-22 22:59:43 +08:00
parent 6e9faca1a3
commit 2917c12f14
65 changed files with 394 additions and 2540 deletions

View File

@@ -0,0 +1,58 @@
/*
* @Author: dgflash
* @Date: 2021-11-18 17:47:56
* @LastEditors: dgflash
* @LastEditTime: 2022-08-04 15:43:04
*/
import { instantiate, Node, Prefab, tween, Vec3,Label } from "cc";
import { UICallbacks } from "../../../../extensions/oops-plugin-framework/assets/core/gui/layer/Defines";
import { oops } from "../../../../extensions/oops-plugin-framework/assets/core/Oops";
import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
import { UIID } from "../common/config/GameUIConfig";
import { smc } from "../common/SingletonModuleComp";
import { HeroModelComp } from "./HeroModelComp";
import { HeroSpine } from "./HeroSpine";
import { HeroViewComp } from "./HeroViewComp";
/** 角色实体 */
@ecs.register(`Hero`)
export class Hero extends ecs.Entity {
// 数据层
HeroModel!: HeroModelComp;
// 视图层
HeroView!: HeroViewComp;
protected init() {
this.addComponents<ecs.Comp>(
HeroModelComp);
}
destroy(): void {
this.remove(HeroViewComp);
super.destroy();
}
/** 加载角色 */
load(pos: Vec3 = Vec3.ZERO,profession:string = "war") {
var path = "game/heros/"+profession;
var prefab: Prefab = oops.res.get(path, Prefab)!;
var node = instantiate(prefab);
let label =node.getChildByName("top").getChildByName("lab_name")
label.getComponent(Label)!.string = profession;
var scene = smc.map.MapView.scene;
node.parent = scene.entityLayer!.node!;
var as = node.getComponent(HeroSpine);
node.setPosition(pos)
node.setScale(1, 1, 1);
var mv = node.getComponent(HeroViewComp)!;
mv.speed=smc.heros[0].speed;
this.add(mv);
//移除全局列表
// console.log(ecs.query(ecs.allOf(HeroViewComp)))
}
}

View File

@@ -0,0 +1 @@
{"ver":"4.0.23","importer":"typescript","imported":true,"uuid":"984e9949-2b16-4e46-bb30-6ac00a2a838e","files":[],"subMetas":{},"userData":{}}

View File

@@ -0,0 +1,27 @@
/*
* @Author: dgflash
* @Date: 2021-11-18 15:56:01
* @LastEditors: dgflash
* @LastEditTime: 2022-08-17 13:43:25
*/
import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
/**
* 角色属性数据
*/
@ecs.register('HeroModel')
export class HeroModelComp extends ecs.Comp {
/** 角色编号 */
id: number = -1;
/** 角色名 */
name: string = "Hero";
/** 动画名资源 */
anim: string = "Hero";
reset() {
this.id = -1;
this.name = "";
}
}

View File

@@ -0,0 +1 @@
{"ver":"4.0.23","importer":"typescript","imported":true,"uuid":"0ce59d56-a495-4b95-9405-a8a85d47a439","files":[],"subMetas":{},"userData":{}}

View File

@@ -0,0 +1,68 @@
/*
* @Author: dgflash
* @Date: 2022-08-04 15:08:35
* @LastEditors: dgflash
* @LastEditTime: 2022-08-04 15:26:26
*/
import { Color, Component, EventTouch, sp, Vec3, _decorator } from "cc";
import { LayerUtil } from "../../../../extensions/oops-plugin-framework/assets/core/utils/LayerUtil";
import { smc } from "../../../script/game/common/SingletonModuleComp";
import HeroSpineAnimator from "./HeroSpineAnimator";
const { ccclass, property } = _decorator;
/**
* SPINE角色模型
*/
@ccclass('HeroSpine')
export class HeroSpine extends Component {
@property({ type: HeroSpineAnimator, tooltip: '动画控制器' })
animator: HeroSpineAnimator = null!;
private spine!: sp.Skeleton;
onLoad() {
// 角色控制组件
this.initAnimator();
LayerUtil.setNodeLayer(LayerUtil.MAP, this.node);
}
/** 初始化动画 */
protected initAnimator() {
this.spine = this.animator.getComponent(sp.Skeleton)!;
}
setSkin(value: string): void {
console.log("HeroSpine setSkin", value);
this.spine.setSkin(value);
}
play(animName: string, loop: boolean): void {
this.spine.setAnimation(0, animName, loop);
}
setAlpha(value: number): void {
var color: Color = this.spine.color;
color.a = 255 * (value / 1);
this.spine.color = color;
}
setPos(value: Vec3): void {
this.node.position = value;
}
checkTouch(event: EventTouch): boolean {
return false;
}
onDestroy() {
this.node.destroy();
}
walk() {
}
idle() {
}
}

View File

@@ -0,0 +1 @@
{"ver":"4.0.23","importer":"typescript","imported":true,"uuid":"c66b3a64-90df-4454-b232-a18b05baed20","files":[],"subMetas":{},"userData":{}}

View File

@@ -0,0 +1,58 @@
/*
* @Author: dgflash
* @Date: 2022-08-04 15:08:35
* @LastEditors: dgflash
* @LastEditTime: 2022-08-04 15:26:38
*/
import { sp, _decorator ,Component} from "cc";
const { ccclass, property, requireComponent, disallowMultiple } = _decorator;
/**
* Spine状态机组件主状态机trackIndex为0
*/
@ccclass
@disallowMultiple
@requireComponent(sp.Skeleton)
export default class HeroSpineAnimator extends Component {
private animName: string = "move";
private loop: boolean = true;
private spine!: sp.Skeleton;
start() {
this.spine = this.getComponent(sp.Skeleton)!;
// console.log("HeroSpineAnimator start");
this.playAnimation(this.animName, this.loop);
}
lateUpdate(dt: number) {
//
}
play(animName: string, loop: boolean) {
if (animName) {
this.animName = animName;
this.loop = loop;
this.spine.setAnimation(0, this.animName, this.loop);
}
else {
}
}
/**
* 播放动画
* @override
* @param animName 动画名
* @param loop 是否循环播放
*/
protected playAnimation(animName: string, loop: boolean) {
// console.log("HeroSpineAnimator playAnimation");
if (animName) {
// console.log("HeroSpineAnimator playAnimation animName", animName);
this.animName = animName;
this.loop = loop;
this.spine.setAnimation(0, this.animName, this.loop);
}
else {
}
}
}

View File

@@ -0,0 +1 @@
{"ver":"4.0.23","importer":"typescript","imported":true,"uuid":"4f84982c-c68b-4950-b521-892438804b1a","files":[],"subMetas":{},"userData":{}}

View File

@@ -0,0 +1,90 @@
/*
* @Author: dgflash
* @Date: 2021-11-18 17:42:59
* @LastEditors: dgflash
* @LastEditTime: 2022-08-17 12:36:18
*/
import { Vec3, v3,_decorator ,Collider2D,Contact2DType,PhysicsSystem2D,IPhysics2DContact,EPhysics2DDrawFlags} 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 { HeroSpine } from "./HeroSpine";
import {BoxSet} from "../common/config/BoxSet"
import { smc } from "../common/SingletonModuleComp";
const { ccclass, property } = _decorator;
/** 角色显示组件 */
@ccclass('HeroViewComp') // 定义为 Cocos Creator 组件
@ecs.register('HeroView', false) // 定义为 ECS 组件
export class HeroViewComp extends CCComp {
/** 角色动画 */
as: HeroSpine = null!;
/** 角色控制器 */
speed: number = 100;
Tpos: Vec3 = v3(0,-60,0);
start () {
// 注册单个碰撞体的回调函数
// console.log('MonsterViewComp start');
let collider = this.getComponent(Collider2D);
if (collider) {
// console.log('hero collider',collider);
collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
}
// 注册全局碰撞回调函数
if (PhysicsSystem2D.instance) {
// console.log('PhysicsSystem2D.instance');
PhysicsSystem2D.instance.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
}
}
onBeginContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
// 只在两个碰撞体开始接触时被调用一次
// console.log('hero Contact,selfCollider',selfCollider);
switch (otherCollider.group) {
case BoxSet.MONSTER:
// console.log('hero coolider MONSTER ');
break;
case BoxSet.MONSTER_SKILL:
// console.log('hero coolider MONSTER skill');
break;
default:
break;
}
}
/** 视图层逻辑代码分离演示 */
onLoad() {
this.as = this.getComponent(HeroSpine);
}
update(dt: number){
if(this.node.position.x < 360){
this.move(dt);
}
if(this.node.position.x > 360){
smc.heros_in = smc.heros_in.filter(element => element.eid !== this.ent.eid);
this.node.destroy();
}
this.update_pos();
}
move(dt: number){
this.node.setPosition(this.node.position.x+dt*this.speed, this.node.position.y, this.node.position.z);
}
update_pos(){
smc.heros_in.forEach(element => {
if(element.eid == this.ent.eid){
element.pos_x = this.node.position.x;
}
});
// console.log('smc.heros_in',smc.heros_in);
}
reset() {
this.node.destroy();
}
}

View File

@@ -0,0 +1 @@
{"ver":"4.0.23","importer":"typescript","imported":true,"uuid":"ffc9ef3c-4de8-4996-abe0-296d8956dcfe","files":[],"subMetas":{},"userData":{}}