123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910 |
- import FObject, { GroupType } from "./FObject";
- import CMath from "../../../util/CMath";
- import BObject from "../../fight/bullet/BObject";
- import FPanel from "./FPanel";
- import { FFAttr } from "../../data/FFCalAttr";
- import AIBase from "./AI/AIBase";
- import FPanelIcon from "./FPanelIcon";
- import Equip from "../../home/equip/Equip";
- import { __SkillData } from "../../data/sdata/SManage";
- const { ccclass, property } = cc._decorator;
- export const SpriteType = cc.Enum({
- NONE: 0,
- MOVE: 1,
-
- });
- export const SpriteActionType = cc.Enum({
- stand: 'idle',
- move: 'move',
- run: 'run',
- atk: 'atk',
- atk1: 'atk1',
- dead: 'dead',
- yun: 'yun',
- yun2: 'yun2',
- dang: 'dang',
- chuizi: 'chuizi',
- shiqu: 'shiqu'
- });
- export interface ColliderSprite {
- sprite: FSprite,
- dis: number,
- }
- @ccclass
- export default class FSprite extends FObject {
-
- public spine: sp.Skeleton = null;
-
-
- mBullet: cc.Prefab = null;
-
-
- public mAtkSite: sp.spine.Bone = null;
- public mWeapon1: sp.spine.Bone = null;
- mPanels: Array<FPanel> = [];
- public mRigidBody: cc.RigidBody;
- public status = SpriteType.NONE;
-
- public hp: number = -1;
-
- public attrData: FFAttr = null;
-
- public isActive = true;
- public SPEED_WALK = 300;
-
- public SPEED_SHOOT = 100;
-
- private dir = { x: 0, y: 0 };
- public moveV2: cc.Vec2 = cc.v2(1, 0);
- public isWalk = false;
- public isRuning = false;
- public isShooting = false;
- public wAngle = 0;
-
- public gamePause: boolean = false;
- public hitCount = 0;
- private yanwu: cc.Node = null;
-
- public hasDun = false;
-
- public hasWudi = false;
- onLoad() {
- let spriteNode = this.node.getChildByName('juese01');
- let spineNode0 = spriteNode.getChildByName('spineRight');
- if (this.node.name == "s1") {
- this.yanwu = spriteNode.getChildByName("yanwu_gs");
- }
-
-
- this.mRigidBody = this.node.getComponent(cc.RigidBody);
- this.mRigidBody.allowSleep = false;
- this.spine = spineNode0.getComponent(sp.Skeleton);
- this.mAtkSite = this.spine.findBone('zidan');
- this.mWeapon1 = this.spine.findBone('control');
- }
- start() {
- if (this.id > 0) {
- let main = this.map.ff.main;
- let attrData = main.sManage.getMonsterData(this.id);
- this.setAttrData(attrData);
- this.hp = this.attrData.hp;
- for (let i = 0; i < this.mPanels.length; i++) {
- const element = this.mPanels[i];
- element.updatePanel()
- }
- }
- this.setYanwu(false);
- this.updateSkin()
- }
-
- public useHammer() {
- Equip.chaneWeapon(this.spine, '1000')
- }
- public updateSkin() {
-
-
-
-
-
-
-
-
-
- }
- update(dt) {
- if (this.gamePause) {
- return;
- }
- this.updateMove(dt)
- }
- public setPause(pause: boolean) {
- this.gamePause = pause
- if (this.gamePause) {
- this.setDir({ x: 0, y: 0 });
- this.setWalk(false);
- }
- }
-
- public setPosition(pos) {
- this.node.setPosition(pos);
- }
-
- public setBullet(mBullet) {
- this.mBullet = mBullet;
- }
-
- public setAttrData(attrData: FFAttr) {
- this.attrData = attrData;
-
- if (this.mBullet) {
- let node = cc.instantiate(this.mBullet);
- node.destroy();
- } else {
- if (!attrData.bullet) {
- attrData.bullet = '1001'
- }
- attrData.bullet = '1101'
- cc.resources.load('prefab/bullet/' + attrData.bullet, cc.Prefab, (err, prefab: cc.Prefab) => {
- if (err) {
- cc.error(err);
- } else {
-
- this.mBullet = prefab;
- let node = cc.instantiate(this.mBullet);
- node.destroy();
- }
- });
- }
- let asset = 'prefab/common/hp_monter';
- if (this.node.group == 'B') {
- asset = 'prefab/common/hp_monter_red'
- }
- cc.resources.load(asset, cc.Prefab, (err, prefab: cc.Prefab) => {
- if (err) {
- cc.error(err);
- } else {
-
- let node = cc.instantiate(prefab);
- let panel = node.getComponent(FPanel);
- panel.sprite = this;
- this.mPanels.push(panel);
- node.y = -10;
- node.parent = this.node;
- panel.updatePanel();
- }
- });
- }
- public flushAttrData(attrData: FFAttr) {
- this.attrData = attrData;
- this.updateSkin()
- if (!attrData.bullet) {
- attrData.bullet = '1001'
- }
- cc.resources.load('prefab/bullet/' + attrData.bullet, cc.Prefab, (err, prefab: cc.Prefab) => {
- if (err) {
- cc.error(err);
- } else {
-
- this.mBullet = prefab;
- let node = cc.instantiate(this.mBullet);
- node.destroy();
- }
- });
- }
-
- public addPanelHUD() {
- let res = this.ff.fres;
- let node = cc.instantiate(res.mHudPrefab)
- node.parent = res.mHudNode;
- let panelIcon = node.getComponent(FPanelIcon)
- panelIcon.sprite = this
- panelIcon.setIcon()
- this.mPanels.push(panelIcon)
- }
- protected setWeaponAngle(angle: number) {
- if (this.mWeapon1) {
- this.wAngle = angle * 180 / Math.PI;
-
- this.mWeapon1.rotation = this.wAngle + 180;
- }
- }
-
- protected updateWeaponAngle(angle: number) {
- if (this.mWeapon1) {
-
- this.mWeapon1.rotation = (angle - 90) * this.spine.node.scaleX
-
- }
- }
-
- public updateAction() {
- if (this.isWalk) {
- if (this.isShooting) {
- this.createBullet();
- this.playAction(SpriteActionType.atk, false, () => {
- this.updateSpine();
-
-
- this.updateAction();
- });
- } else {
- this.playAction(SpriteActionType.move, true);
- }
- } else {
- if (this.isShooting) {
- this.createBullet();
- this.playAction(SpriteActionType.atk1, false, () => {
- this.updateSpine();
-
-
- this.updateAction();
- });
- } else {
- this.playAction(SpriteActionType.stand, true);
- }
- }
- }
-
- public updateSpine(): boolean {
- let csprite: FSprite = this.findEnemy(1300).sprite;
- let p1 = this.node.getPosition();
- if (csprite && csprite.isValid) {
- let p2 = csprite.node.getPosition();
- if (p1.x == p2.x) {
- } else if (p1.x - p2.x > 3) {
- this.setLR(-1);
- } else {
- this.setLR(1);
- }
-
-
-
-
- } else {
-
-
- }
- return false;
- }
- private updateSpineByDir() {
- if (this.dir.x >= 0) {
- this.setLR(1);
- } else {
- this.setLR(-1);
- }
- }
-
- public setDir(dir): boolean {
- if (this.gamePause) {
- return;
- }
- if (dir.x == 0 && dir.y == 0) {
- this.setWalk(false);
- return false;
- }
- if (this.isWalk && this.dir.x == dir.x && this.dir.y == dir.y) {
- return true;
- }
- this.dir.x = dir.x;
- this.dir.y = dir.y;
- this.setWalk(true);
- return true;
- }
-
- public setWalk(isWalk: boolean) {
- this.isWalk = isWalk;
-
-
- if(!isWalk){
- this.spine.setAnimation(0, SpriteActionType.stand, true)
- }
- }
-
-
-
-
-
-
-
- public setShooting(isShooting: boolean) {
- this.isShooting = isShooting;
- if (isShooting && this.status == SpriteType.NONE) {
-
-
- this.updateAction();
- }
- }
- public updateMove(dt) {
- if (this.isWalk) {
-
- let speed = 0;
- if (this.isShooting) {
- speed = this.SPEED_SHOOT;
- } else {
- speed = this.SPEED_WALK;
- }
- this.node.x += this.dir.x * dt * speed;
- this.node.y += this.dir.y * dt * speed;
- this.setYanwu(true);
- } else {
-
- this.setYanwu(false);
- }
- }
-
- public moveTo(pos: cc.Vec2, dt): boolean {
-
-
- let speed = 1200;
- let px = this.node.x > pos.x ? -speed : speed;
- let py = this.node.y > pos.y ? -speed : speed;
- this.node.x += dt * px;
- this.node.y += dt * py;
- if (px > 0) {
- if (this.node.x > pos.x) {
- this.node.x = pos.x;
- }
- } else {
- if (this.node.x < pos.x) {
- this.node.x = pos.x;
- }
- }
- if (py > 0) {
- if (this.node.y > pos.y) {
- this.node.y = pos.y;
- }
- } else {
- if (this.node.y < pos.y) {
- this.node.y = pos.y;
- }
- }
- return false;
- }
-
-
-
-
-
-
-
-
-
-
-
-
- public setLR(lr: number) {
- let scaleX = Math.abs(this.spine.node.scaleX);
- if (lr == -1) {
- this.spine.node.scaleX = -scaleX;
- } else {
- this.spine.node.scaleX = scaleX;
- }
- }
- public getLR(): number {
- if (this.spine.node.scaleX > 0) {
- return 1
- } else {
- return -1;
- }
- }
-
- public fireCallback: () => void;
- public setFireCallback(fireCallback: () => void) {
- this.fireCallback = fireCallback;
- }
-
- public addSendEffect(prefab: cc.Prefab, target: cc.Node) {
- if (prefab) {
- let node = cc.instantiate(prefab);
- let x = this.mAtkSite.worldX * this.spine.node.scaleX
- let y = this.mAtkSite.worldY
- let pos = cc.v2(x, y);
- node.setPosition(pos);
- node.parent = this.node;
-
-
-
-
- if (target) {
- let p1 = this.node.getPosition();
- let p2 = target.getPosition();
-
- let angle = Math.floor(CMath.getAngle(p1, p2) * (180 / Math.PI));
- node.angle = angle
- } else {
- node.angle = this.wAngle
- }
- let spine = node.getComponent(sp.Skeleton)
- spine.setCompleteListener(() => {
- node.destroy()
- });
- spine.setAnimation(0, 'atk', false)
- }
- }
- createBullet() {
- if (this.isValid && !this.gamePause) {
- let node = cc.instantiate(this.mBullet);
- node.group = 'bullet';
- let x = this.node.x + this.mAtkSite.worldX * this.spine.node.scaleX
- let y = this.node.y + this.mAtkSite.worldY
- let pos = cc.v2(x, y);
- node.setPosition(pos);
- let bObject = node.getComponent(BObject);
- bObject.setSprite(this);
- let csprite: FSprite = this.findEnemy(2000).sprite;
- node.parent = this.map.mSprites;
- if (csprite && csprite.isValid) {
- bObject.fire(csprite.node);
- this.addSendEffect(bObject.mStartEffect, csprite.node);
- } else {
- bObject.fireAngle1(this.wAngle);
- this.addSendEffect(bObject.mStartEffect, null);
- }
- if (this.fireCallback) {
- this.fireCallback();
- }
- if (this.node == this.ff.mainSprite.node) {
- if (this.hitCount > 4) {
- this.ff.shockMap();
- this.hitCount = 0;
- } else {
- this.hitCount++;
- }
- }
- this.ff.main.playerEffectByPath('music/magic_1001_fs');
- }
- }
- public tmpActionType = null;
- public playAction(nActionType, loop?: boolean, finishBack?: () => void): boolean {
- if (!this.isActive) {
- return false;
- }
- if (!this.spine) {
- return false;
- }
-
- if (nActionType == this.tmpActionType) {
- return false;
- }
- if (finishBack) {
- this.spine.setCompleteListener(() => {
- this.spine.setCompleteListener(null);
- this.tmpActionType = null;
- finishBack();
- });
- }
-
-
- this.tmpActionType = nActionType;
- this.spine.setAnimation(0, nActionType, loop);
- return true
- }
- public playAction2(nActionType, loop?: boolean, finishBack?: () => void) {
- this.status = SpriteType.NONE
- if (finishBack) {
- this.spine.setCompleteListener(() => {
- this.spine.setCompleteListener(null);
- this.tmpActionType = null;
- finishBack();
- });
- }
- this.spine.setEventListener(null);
- this.spine.setAnimation(0, nActionType, loop);
- }
-
- public atkjs(target: FSprite, _skillData?: __SkillData) {
- let reslut = this.calculate(this.attrData, target.attrData, _skillData);
- target.reduce(reslut);
- if (this == this.ff.mainSprite as FSprite) {
- this.ff.flushHP(target);
- } else {
- this.ff.updateHP(target);
- }
-
-
- if (target.hp > 0) {
- let baseAI = target.node.getComponent(AIBase);
- if (baseAI) {
- baseAI.setTarget(this);
- }
-
-
-
-
-
-
-
-
-
- }
- }
-
- public bAtkjs(attrData: FFAttr) {
- let reslut = this.calculate(attrData, this.attrData);
- this.reduce(reslut);
- }
-
- private calculate(A: FFAttr, B: FFAttr, _skillData?: __SkillData): any {
- let xx = A.atk - B.def;
- if (_skillData) {
-
-
-
-
-
-
-
- if (_skillData.value4 == 1) {
- xx += A.water
- } else if (_skillData.value4 == 2) {
- xx += A.fire
- } else if (_skillData.value4 == 3) {
- xx += A.earth
- } else if (_skillData.value4 == 4) {
- xx += A.wind
- } else if (_skillData.value4 == 5) {
- xx += A.thunder
- }
- }
- if (xx <= 0) {
- xx = 1;
- }
- let hp = xx * 500 / (B.def + 300);
- if (hp < 1) {
- hp = 1;
- }
- let bj = false
- let sk = false
- let rand = CMath.getRandom(1, 100)
- if (rand < 30) {
- bj = true
- hp += hp * 3 / 10
- }
- if (_skillData) {
- hp = hp * _skillData.value1 / 100
- sk = true
- }
- let xd = hp / 10;
- let xxd = CMath.getRandom(-xd, xd);
- hp += xxd;
- if (hp < 10) {
- hp = CMath.getRandom(1, 10);
- }
- hp = Math.floor(hp)
- return {
- hp: Math.floor(hp),
- bj: bj,
- sk: sk
- }
- }
-
- public getEnemyGroup() {
- return this.node.group == 'A' ? 'B' : 'A';
- }
-
- public findEnemy(radius: number): ColliderSprite {
- let mGroup = this.getEnemyGroup();
- let minDis = 0
- let targetSprite = null;
- if (this.map) {
- let nodes = this.map.getSprites();
- for (let i = 0; i < nodes.length; i++) {
- const node = nodes[i];
- let sobj = node.getComponent(FSprite);
- if (sobj && node.active && sobj.isActive && sobj.hp > 0 && sobj.node.group == mGroup) {
- let dis = cc.Vec2.distance(this.node.getPosition(), node.getPosition());
- if (dis < radius) {
- if (targetSprite == null) {
- targetSprite = sobj;
- minDis = dis;
- } else if (dis < minDis) {
- targetSprite = sobj;
- minDis = dis;
- }
- }
- }
- }
- }
- return {
- sprite: targetSprite,
- dis: minDis
- };
- }
- public wudi(time) {
- this.hasWudi = true
- cc.tween(this.node).sequence(
- cc.delayTime(time),
- cc.callFunc(() => {
- this.hasWudi = false
- })
- ).start()
- }
- public reduce(result: any) {
- let hp = result.hp
- if (hp <= 0) {
- return
- }
- if (this.hasWudi) {
- return
- }
- if (this.hp <= 0) {
- return
- }
- if (!this.ff.lockCamera) {
- return
- }
- let bj = result.bj
- let sk = result.sk
- this.playHit();
- if (this.hasDun) {
- hp = Math.floor(hp / 2);
- }
- if (sk) {
- this.showNumber('/' + hp, this.ff.mSkillFont);
- } else if (bj) {
- this.showNumber('/' + hp, this.ff.mBjFont, null, true);
- } else {
- this.showNumber('/' + hp, this.ff.mRed);
- }
- this.hp -= hp;
- if (this.hp <= 0) {
- this.hp = 0;
- this.isActive = false;
- if (this.ff.clearCallback) {
- this.ff.clearCallback(this);
- }
- if (this.ff.removeCallback) {
- this.ff.removeCallback(this);
- }
- if (this == this.ff.mainSprite as FSprite) {
-
- } else {
- this.removeSelf();
- }
- }
- this.updatePanel();
- }
- public updatePanel() {
- for (let i = 0; i < this.mPanels.length; i++) {
- const element = this.mPanels[i];
- element.updatePanel();
- }
- }
- public removeSelf() {
- this.gamePause = true;
- this.playAction2(SpriteActionType.dead, false, () => {
- this.node.destroy();
- });
- }
-
- private showNumber(str: string, font: cc.Font, color?: cc.Color, bs?: boolean) {
- let node: cc.Node = new cc.Node('hp');
- node.group = 'map';
- let lable: cc.Label = node.addComponent(cc.Label);
- lable.fontSize = 30;
- if (font) {
- lable.font = font;
- }
- if (color) {
- node.color = color;
- }
- lable.string = str;
- node.x = this.node.x;
- node.y = this.node.y + this.node.height * 3 / 4;
- this.map.node.addChild(node);
- let moveBy = cc.moveBy(2, cc.v2(0, 150)).easing(cc.easeSineOut());
- let fadeout = cc.fadeOut(2);
- let spawn = null
- if (bs) {
- let seq = cc.sequence(cc.scaleTo(0.05, 1.5), cc.scaleTo(0.2, 1.2));
- spawn = cc.spawn(moveBy, fadeout, seq);
- } else {
- spawn = cc.spawn(moveBy, fadeout);
- }
- let seq = cc.sequence(cc.delayTime(0.1), spawn, cc.callFunc(() => {
- node.removeFromParent(true);
- node.destroy();
- }));
- node.runAction(seq);
- }
-
- public playHit() {
- let spriteNode = this.node.getChildByName('juese01');
- if (spriteNode) {
- cc.tween(spriteNode).sequence(
- cc.scaleTo(0.06, 1.1, 0.95),
- cc.scaleTo(0.06, 1, 1.1),
- cc.scaleTo(0.06, 1, 1),
- ).start();
-
-
-
-
-
-
- }
- }
- public addActiveIcon(): cc.Node {
- let node = new cc.Node('activeIcon');
- let sprite = node.addComponent(cc.Sprite);
- sprite.spriteFrame = this.ff.mActiveIcon;
- node.parent = this.node;
- node.y = this.node.height + 20
- return node;
- }
- public removeActiveIcon() {
- let node = this.node.getChildByName('activeIcon');
- if (node) {
- node.destroy();
- }
- }
-
- public nextSprite: FSprite;
- public setNext(sprite: FSprite) {
- this.nextSprite = sprite;
- }
-
- public tangentMove(target: cc.Node) {
- }
- public setYanwu(status: boolean) {
- if (this.yanwu) {
- if (this.yanwu.active == status) return
- this.yanwu.active = status;
- }
- }
- public dead(){
- }
- }
|