This commit is contained in:
2024-08-01 10:57:39 +08:00
parent 9aa9806b62
commit 6c5d417ad1
13 changed files with 119 additions and 330 deletions

View File

@@ -24,16 +24,16 @@ export class Main extends Root {
protected async run() {
smc.initialize = ecs.getEntity<Initialize>(Initialize);
smc.monsters = [
{name:'niu',profession:'m1',speed:50},
{name:'niu',profession:'m2',speed:100},
{name:'niu',profession:'m3',speed:50},
{name:'niu',profession:'m4',speed:100},
{prefab_path:'niu',name:'m1',speed:40},
{prefab_path:'niu',name:'m2',speed:80},
{prefab_path:'niu',name:'m3',speed:40},
{prefab_path:'niu',name:'m4',speed:80},
]
smc.heros = [
{name:'hero',profession:'war',speed:50},
// {profession:'magic',speed:50},
// {profession:'heath',speed:50},
// {profession:'war',speed:50},
{prefab_path:'niu',name:'war',speed:60},
{prefab_path:'niu',name:'magic',speed:60},
{prefab_path:'niu',name:'heath',speed:60},
{prefab_path:'niu',name:'war',speed:60},
]
console.log("Game start");
}

View File

@@ -3,7 +3,6 @@ import { ecs } from "../../../../../extensions/oops-plugin-framework/assets/libs
import { CCComp } from "../../../../../extensions/oops-plugin-framework/assets/module/common/CCComp";
import { smc } from "../../common/SingletonModuleComp";
import { Role } from "../../role/Role";
import { Niu } from "../../monster/niu/Niu";
import { BoxSet } from "../../common/config/BoxSet";
import { Hero } from "../../heros/Hero";
import { Monster } from "../../monster/Monster";
@@ -58,27 +57,32 @@ export class MapViewComp extends CCComp {
private addHero() {
this.scene.node.active = true
if (smc.monsters.length>0){
this.addMonster(smc.monsters[0].name,smc.monsters[0].speed)
let monster = ecs.getEntity<Monster>(Monster);
let pos:Vec3 = v3(BoxSet.RIGHT_END,-60)
let speed =smc.monsters[0].speed
let camp = -1
let prefab_path = smc.monsters[0].prefab_path
let name = smc.monsters[0].name
monster.load(pos,speed,camp,prefab_path,name);
smc.monsters.splice(0,1)
}
// if(smc.heros.length>0) {
// this.addMonster(smc.heros[0].name,100)
// }
if(smc.heros.length>0) {
console.log("heros load")
let monster = ecs.getEntity<Monster>(Monster);
let pos = v3(BoxSet.LETF_END,-60)
let speed =smc.heros[0].speed
let camp = 1
let prefab_path = smc.heros[0].prefab_path
let name = smc.heros[0].name
monster.load(pos,speed,camp,prefab_path,name);
smc.heros.splice(0,1)
}
}
private addMonster(name:string = "niu",speed:number = 100) {
let monster = null
switch (name) {
case "niu":
monster = ecs.getEntity<Niu>(Niu);
break;
case "hero":
monster = ecs.getEntity<Monster>(Monster);
break;
default:
break;
}
monster.load(v3(BoxSet.RIGHT_END,-60),speed);
}

View File

@@ -5,7 +5,7 @@
* @LastEditors: dgflash
* @LastEditTime: 2022-08-04 15:43:04
*/
import { instantiate, Node, Prefab, Vec3 ,tween, v3,Collider2D,Contact2DType,PhysicsSystem2D,IPhysics2DContact} from "cc";
import { instantiate, Node, Prefab, Vec3 ,tween, v3,Collider2D,Contact2DType,PhysicsSystem2D,IPhysics2DContact, animation,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";
@@ -36,23 +36,33 @@ export class Monster extends ecs.Entity {
}
/** 加载角色 */
load(pos: Vec3 = Vec3.ZERO,speed:number = 100) {
var path = "game/monster/monster";
load(pos: Vec3 = Vec3.ZERO,speed:number = 100,camp:number = 1,prefab_path:string = "monster",name:string="hero") {
var path = "game/monster/"+prefab_path;
var prefab: Prefab = oops.res.get(path, Prefab)!;
var node = instantiate(prefab);
var scene = smc.map.MapView.scene;
node.parent = scene.entityLayer!.node!;
var as = node.getComponent(MonsterSpine);
node.setPosition(pos)
node.getChildByName("avatar").setScale(node.getChildByName("avatar").scale.x*camp, node.getChildByName("avatar").scale.y, node.getChildByName("avatar").scale.z);
node.setPosition(pos)
var mv = node.getComponent(MonsterViewComp)!;
mv.speed = speed;
mv.ospeed = speed;
console.log('speed:'+speed)
mv.speed =mv.ospeed = speed;
mv.hero_name= name;
mv.camp = camp;
mv.Tpos = v3(0,0,0);
mv.change_name(name)
this.add(mv);
smc.monsters_in.push({name:mv.ent.name,eid:mv.ent.eid,pos_x:0}),
// node.setScale(-1, 1, 1);
oops.message.dispatchEvent("monster_load",this)
if(camp == 1){
smc.heros_in.push({name:mv.ent.name,eid:mv.ent.eid,pos_x:0})
oops.message.dispatchEvent("hero_load",this)
}else{
smc.monsters_in.push({name:mv.ent.name,eid:mv.ent.eid,pos_x:0})
oops.message.dispatchEvent("monster_load",this)
}
}
/** 移动ECS System处理逻辑分享功能独立的业务代码 */

View File

@@ -5,7 +5,7 @@
* @LastEditTime: 2022-08-17 12:36:18
*/
import { Vec3, _decorator ,tween, v3,Collider2D,Contact2DType,PhysicsSystem2D,IPhysics2DContact} from "cc";
import { Vec3, _decorator ,tween, v3,Collider2D,Contact2DType,PhysicsSystem2D,IPhysics2DContact,EPhysics2DDrawFlags,Label,Node} 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 { MonsterSpine } from "./MonsterSpine";
@@ -22,22 +22,65 @@ const { ccclass, property } = _decorator;
export class MonsterViewComp extends CCComp {
/** 角色动画 */
as: MonsterSpine = null!;
hero_name : string = "hero";
camp: number = 1;
speed: number = 100;
ospeed: number = 100;
Tpos: Vec3 = v3(0,-60,0);
timer: number = 0;
/** 视图层逻辑代码分离演示 */
start () {
// 注册单个碰撞体的回调函数
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);
}
}
onBeginContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
// 只在两个碰撞体开始接触时被调用一次
// console.log('onBeginContact otherCollider.tag :',otherCollider,selfCollider);
if ( otherCollider.tag == BoxSet.HERO && selfCollider.tag == BoxSet.MONSTER) {
this.speed = 0
this.timer = 0.5
}
}
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.tag === BoxSet.HERO && selfCollider.tag === BoxSet.MONSTER) {
// console.log('onPostSolve otherCollider.tag :'+otherCollider.tag);
// this.speed = 0;
// this.timer = 1;
}
}
onLoad() {
this.as = this.getComponent(MonsterSpine);
console.log('this.ent:',this);
console.log('hero load ent:',this);
PhysicsSystem2D.instance.debugDrawFlags = EPhysics2DDrawFlags.Aabb |
EPhysics2DDrawFlags.Pair |
EPhysics2DDrawFlags.CenterOfMass |
EPhysics2DDrawFlags.Joint |
EPhysics2DDrawFlags.Shape;
}
change_name(hero_name:string){
this.name=hero_name;
let label:any =this.node.getChildByName("top").getChildByName("lab_name")
label.getComponent(Label)!.string = hero_name;
}
update(dt: number){
if(this.timer > 0){
this.timer -= dt;
@@ -46,19 +89,30 @@ export class MonsterViewComp extends CCComp {
this.timer = 0;
}
}
if(this.node.position.x > -360){
this.move(dt);
if(this.camp == -1){
if(this.node.position.x < BoxSet.LETF_END){
console.log(this.node,this.ent)
smc.monsters_in = smc.monsters_in.filter(element => element.eid !== this.ent.eid);
this.node.destroy();
}else{
this.move(dt);
}
}
if(this.node.position.x < -300){
console.log(this.node,this.ent)
smc.monsters_in = smc.monsters_in.filter(element => element.eid !== this.ent.eid);
this.node.destroy();
if(this.camp == 1){
if(this.node.position.x > BoxSet.RIGHT_END){
console.log(this.node,this.ent)
smc.monsters_in = smc.monsters_in.filter(element => element.eid !== this.ent.eid);
this.node.destroy();
}else{
this.move(dt);
}
}
this.update_pos();
}
move(dt: number){
this.node.setPosition(this.node.position.x-dt*this.speed, this.node.position.y, this.node.position.z);
this.node.setPosition(this.node.position.x+dt*this.speed*this.camp, this.node.position.y, this.node.position.z);
}
update_pos(){
smc.monsters_in.forEach((element,index) => {

View File

@@ -1,62 +0,0 @@
import { _decorator, Component, Node,Collider2D ,Contact2DType,PhysicsSystem2D,IPhysics2DContact} from 'cc';
const { ccclass, property } = _decorator;
@ccclass('bossCom')
export class bossCom extends Component {
@property(Number)
private speed: number = 100;
@property(Number)
private ospeed: number = 100;
@property(Number)
private timer: number = 0;
protected onLoad(): void {
this.speed = this.ospeed = 100;
}
start () {
// 注册单个碰撞体的回调函数
// console.log('MonsterViewComp start');
let collider = this.getComponent(Collider2D);
// console.log('Monster collider',collider);
if (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('monster Contact,otherCollider',otherCollider);
this.speed = 0;
this.timer = 1;
}
update(dt: number){
if(this.timer > 0){
this.timer -= dt;
if(this.timer <= 0){
this.speed = this.ospeed;
this.timer = 0;
}
}
if(this.node.position.x > -360){
this.move(dt);
}
if(this.node.position.x < -360){
this.node.destroy();
}
}
move(dt: number){
this.node.setPosition(this.node.position.x-dt*this.speed, this.node.position.y, this.node.position.z);
}
}

View File

@@ -1,9 +0,0 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "28f753af-37cc-4e46-b100-445408bc547e",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -1,9 +0,0 @@
{
"ver": "1.2.0",
"importer": "directory",
"imported": true,
"uuid": "4cafacf8-51ca-4bd3-a5fb-c3b3147443d2",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -1,59 +0,0 @@
/*
* @Author: dgflash
* @Date: 2021-11-18 17:47:56
* @LastEditors: dgflash
* @LastEditTime: 2022-08-04 15:43:04
*/
import { instantiate, Node, Prefab, Vec3 ,tween, v3,Collider2D,Contact2DType,PhysicsSystem2D,IPhysics2DContact} 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 { MonsterSpine } from "./../MonsterSpine";
import { NiuViewComp } from "./NiuViewComp";
/** 角色实体 */
@ecs.register(`Niu`)
export class Niu extends ecs.Entity {
// 数据层
// 视图层
NiuView!: NiuViewComp;
// 移动
protected init() {
}
destroy(): void {
this.remove(NiuViewComp);
super.destroy();
}
/** 加载角色 */
load(pos: Vec3 = Vec3.ZERO,speed:number = 100) {
var path = "game/monster/niu";
var prefab: Prefab = oops.res.get(path, Prefab)!;
var node = instantiate(prefab);
var scene = smc.map.MapView.scene;
node.parent = scene.entityLayer!.node!;
// var as = node.getComponent(MonsterSpine);
node.setPosition(pos)
var mv = node.getComponent(NiuViewComp)!;
mv.speed = speed;
mv.ospeed = speed;
mv.Tpos = v3(0,0,0);
this.add(mv);
smc.monsters_in.push({name:mv.ent.name,eid:mv.ent.eid,pos_x:0}),
// node.setScale(-1, 1, 1);
oops.message.dispatchEvent("monster_load",this)
}
/** 移动ECS System处理逻辑分享功能独立的业务代码 */
}

View File

@@ -1,9 +0,0 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "56b8cdfc-ab72-4b6c-a182-7a06097cb4a8",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -1,121 +0,0 @@
/*
* @Author: dgflash
* @Date: 2021-11-18 17:42:59
* @LastEditors: dgflash
* @LastEditTime: 2022-08-17 12:36:18
*/
import { Vec3, _decorator ,tween, v3,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 { MonsterSpine } from "./../MonsterSpine";
import { Niu } from "./Niu";
import { MonsterModelComp } from "./../MonsterModelComp";
import { BoxSet } from "../../common/config/BoxSet";
import { smc } from "../../common/SingletonModuleComp";
const { ccclass, property } = _decorator;
/** 角色显示组件 */
@ccclass('NiuViewComp') // 定义为 Cocos Creator 组件
@ecs.register('NiuView', false) // 定义为 ECS 组件
export class NiuViewComp extends CCComp {
/** 角色动画 */
as: MonsterSpine = null!;
speed: number = 100;
@property
private _ospeed: number = 100;
@property
set ospeed(val: number) {
this._ospeed = val;
}
get ospeed() {
return this._ospeed;
}
Tpos: Vec3 = v3(0,-60,0);
timer: number = 0;
/** 视图层逻辑代码分离演示 */
start () {
// 注册单个碰撞体的回调函数
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);
}
}
onBeginContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
// 只在两个碰撞体开始接触时被调用一次
// console.log('onBeginContact otherCollider.tag :',otherCollider,selfCollider);
if ( otherCollider.tag == BoxSet.HERO || otherCollider.tag == BoxSet.MONSTER) {
console.log('onBeginContact otherCollider.tag :',otherCollider,selfCollider);
this.speed = 0;
this.timer = 1;
}
}
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.tag === BoxSet.HERO || otherCollider.tag === BoxSet.MONSTER) {
// console.log('onPostSolve otherCollider.tag :'+otherCollider.tag);
this.speed = 0;
this.timer = 1;
}
}
onLoad() {
this.as = this.getComponent(MonsterSpine);
this.ospeed= this.speed;
console.log('this.ospeed:'+this.ospeed);
PhysicsSystem2D.instance.debugDrawFlags = EPhysics2DDrawFlags.Aabb |
EPhysics2DDrawFlags.Pair |
EPhysics2DDrawFlags.CenterOfMass |
EPhysics2DDrawFlags.Joint |
EPhysics2DDrawFlags.Shape;
}
update(dt: number){
if(this.timer > 0){
this.timer -= dt;
if(this.timer <= 0){
this.speed = this.ospeed;
this.timer = 0;
}
}
if(this.node.position.x > BoxSet.LETF_END){
this.move(dt);
}
if(this.node.position.x < BoxSet.LETF_END){
console.log(this.node,this.ent)
smc.monsters_in = smc.monsters_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.monsters_in.forEach((element,index) => {
if(element.eid == this.ent.eid){
// console.log("index:"+index)
element.pos_x = this.node.position.x;
}
});
// console.log('smc.monsters_in',smc.monsters_in);
}
reset() {
this.node.destroy();
}
}

View File

@@ -1,9 +0,0 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "ab8e4649-6162-4e0b-80cc-baffecdf9733",
"files": [],
"subMetas": {},
"userData": {}
}