123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- import { _decorator, Component, Node, CCInteger, AnimationComponent, find, instantiate, Vec3, director } from 'cc';
- import { GameModel } from '../../../models/GameModel';
- import { EventDispatcher } from '../../../../engines/events/EventDispatcher';
- const { ccclass, property } = _decorator;
- export class WeaponBase extends EventDispatcher{
- /**
- * 子弹数量更改
- */
- public static EVENT_BULLET_CHANGE:string="EVENT_BULLET_CHANGE";
- protected leftHand:AnimationComponent;
- protected rightHand:AnimationComponent;
- protected leftSocket:Node;
- protected rightSocket:Node;
- protected leftWeapon:AnimationComponent;
- protected rightWeapon:AnimationComponent;
- protected fireKey:number=0;
- protected lastFireTime:number=0;
- protected isLeftFire:boolean;
- /**
- * 开火中
- */
- protected isFire:boolean=false;
- protected isReload:boolean=false;
- constructor(leftHand:AnimationComponent,rightHand:AnimationComponent){
- super();
- this.leftHand=leftHand;
- this.rightHand=rightHand;
- this.leftSocket=find("RootNode/Arm/upper_arm_R/lower_arm_R/transform1/hand_R/transform2/Guns",this.leftHand.node);
- this.rightSocket=find("RootNode/Arm/upper_arm_R/lower_arm_R/transform1/hand_R/transform2/Guns",this.rightHand.node);
- this.leftWeapon=instantiate(GameModel.single.currentWeaponPrefab).getComponent(AnimationComponent);
- this.leftSocket.addChild(this.leftWeapon.node);
- this.rightWeapon=instantiate(GameModel.single.currentWeaponPrefab).getComponent(AnimationComponent);
- this.rightSocket.addChild(this.rightWeapon.node);
- this.Idle(true);
- }
- StartFire(fireKey:number):void{
- this.fireKey=fireKey;
- this.isFire=true;
- this.MoveFirePosition();
- }
- StopFire():void{
- this.isFire=false;
- this.Idle();
- }
- Update(dt:number):void{
- if(this.isFire==false||this.isReload){
- return;
- }
- let currentTime=director.getCurrentTime();
- if(currentTime-this.lastFireTime>GameModel.single.currentWeaponConfig.firingRate*1000){
- this.Fire();
- this.bulletCount--;
- this.lastFireTime=currentTime;
- }
- //换弹
- if(this.bulletCount<=0){
- this.Reload();
- }
- }
- /**
- * 换弹
- */
- Reload():void{
- this.isReload=true;
- this.leftHand.crossFade("Reload");
- this.rightHand.crossFade("Reload");
- this.leftHand.on(AnimationComponent.EventType.FINISHED,this.ReloadComplete,this,true);
- }
- /**
- * 换弹完成
- */
- ReloadComplete():void{
- this.bulletCount=this.maxBulletCount;
- this.isReload=false;
- }
- /**
- * 将手移动到指定位置
- */
- MoveFirePosition():void{
- if(this.fireKey==0){
- return;
- }
- let Pos:Vec3;
- if(this.fireKey==1){
- Pos=this.leftHand.node.position;
- Pos.x=-0.3;
- this.leftHand.node.setPosition(Pos);
-
- }else if(this.fireKey==3){
- Pos=this.rightHand.node.position;
- Pos.x=0.3;
- this.rightHand.node.setPosition(Pos);
- }else if(this.fireKey==2){
- Pos=this.leftHand.node.position;
- Pos.x=-0.06;
- this.leftHand.node.setPosition(Pos);
- Pos=this.rightHand.node.position;
- Pos.x=0.06;
- this.rightHand.node.setPosition(Pos);
- }
- }
-
- /**
- * 开火
- */
- private Fire():void{
- if(this.fireKey==1){
- this.leftHand.crossFade("Fire");
- this.isLeftFire=true;
- }else if(this.fireKey==3){
- this.rightHand.crossFade("Fire");
- this.isLeftFire=false;
- }else{
- this.isLeftFire=!this.isLeftFire;
- if(this.isLeftFire){
- this.leftHand.crossFade("Fire");
- }else{
- this.rightHand.crossFade("Fire");
- }
- }
- }
- /**
- * 空闲
- */
- private Idle(isAll?:boolean):void{
- if(isAll){
- this.leftHand.crossFade("Idle",1.5);
- this.rightHand.crossFade("Idle",1.5);
- }else{
- if(this.isLeftFire){
- this.leftHand.crossFade("Idle",1.5);
- }else{
- this.rightHand.crossFade("Idle",1.5);
- }
- }
- }
- /**
- * 剩余子弹数量
- */
- get bulletCount():number{
- return this._bulletCount;
- }
- private _bulletCount:number=0;
- set bulletCount(value:number){
- this._bulletCount=value;
- this.DispatchEvent(WeaponBase.EVENT_BULLET_CHANGE);
- }
- /**
- * 弹夹数量
- */
- get maxBulletCount():number{
- return GameModel.single.currentWeaponConfig.clip;
- }
- }
|