ChaseNpc.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. import Sprite3D = Laya.Sprite3D;
  2. import Vector3 = Laya.Vector3;
  3. import { AssetManager } from "../Tools/AssetManager";
  4. import { Transform3DHelper, GameTools } from "../Tools/GameTools";
  5. import { EventManager, StageState } from "../Tools/EventManager";
  6. import GamePool from "../Tools/GamePool";
  7. import { Hostage } from "./Hostage";
  8. import { AccountManager } from "../Net/AccountManager";
  9. import { PlayerControl } from "./PlayerControl";
  10. import { NpcState } from "./NpcRunner";
  11. import { GameManager } from "./GameManager";
  12. import InGameView from "../View/InGameView";
  13. import { StageControl } from "./StageControl";
  14. import { AudioManager, MusicType } from "../Tools/AudioManager";
  15. export enum ChaseNpcState{Idle,Run,Die,Atk};
  16. export class ChaseNpc extends Laya.Script{
  17. private static ins:ChaseNpc;
  18. //人质
  19. public m_hostage:Sprite3D;
  20. public m_chasenpcmodel:Sprite3D;
  21. public m_chasenpcanimator:Laya.Animator;
  22. public m_chasenpcstate:ChaseNpcState;
  23. public frontdie:boolean = false;
  24. public reardie:boolean = false;
  25. public canmove:boolean = false;
  26. public isdead:boolean = false;
  27. public atked:boolean = false;
  28. //游戏是否开始
  29. public begin:boolean = false;
  30. //npc速度
  31. public m_chasenpcspeed:number = 0;
  32. //开始追赶的距离
  33. public pursuediatance:number = 0;
  34. //开始攻击的距离
  35. public atkdistance:number = 10;
  36. //npc扑向人质时的速度
  37. public NPCRushSpeed:number = 0;
  38. //npc距离玩家的距离
  39. public Distance:number = 0;
  40. //npc追出去的距离
  41. public ChaseDistance:number = 0;
  42. //npc初始位置
  43. public NpcStartPos:Vector3 = new Vector3();
  44. constructor(){
  45. super();
  46. ChaseNpc.ins = this;
  47. EventManager.StageOn(StageState.Start,this,this.Begin)
  48. EventManager.StageOn(StageState.Lose,this,this.GameOver);
  49. EventManager.StageOn(StageState.Win,this,this.GameOver);
  50. EventManager.StageOn(StageState.Press,this,this.AddSpeed)
  51. EventManager.StageOn(StageState.UnPress,this,this.SlowSpeed)
  52. }
  53. public static get Instance():ChaseNpc{
  54. if(ChaseNpc.ins){
  55. return ChaseNpc.ins
  56. }
  57. else{
  58. return new ChaseNpc();
  59. }
  60. }
  61. onAwake(){
  62. this.m_chasenpcmodel = this.owner as Laya.Sprite3D;
  63. }
  64. onStart()
  65. {
  66. this.m_chasenpcanimator = this.m_chasenpcmodel.getComponent(Laya.Animator) as Laya.Animator;
  67. this.m_hostage = this.owner.parent.parent.getChildByName("player").getChildAt(0) as Laya.Sprite3D;
  68. this.NpcStartPos = this.m_chasenpcmodel.transform.position.clone();
  69. this.m_chasenpcspeed = StageControl.Instance.curstage.npcspeed;
  70. this.Distance = AccountManager.Instance.curplayerData.NpcChaseDistance*1.65;
  71. this.ChaseDistance = AccountManager.Instance.curplayerData.NpcRushDistance*1.65;
  72. this.NPCRushSpeed = AccountManager.Instance.curplayerData.NPCRushSpeed;
  73. }
  74. Begin(){
  75. this.ChangeState(ChaseNpcState.Run);
  76. this.begin = true;
  77. }
  78. dir = new Vector3();
  79. currotV = new Vector3();
  80. currot = new Laya.Quaternion;
  81. NPCMoveDir:Vector3;
  82. CurrectRotate = new Laya.Quaternion();
  83. onUpdate(){
  84. if(!this.begin)return
  85. if(this.isdead)return
  86. let _host = this.m_hostage.getComponent(Hostage) as Hostage;
  87. if(_host.isdead){
  88. this.ChangeState(ChaseNpcState.Idle)
  89. return
  90. }
  91. if(this.frontdie || this.reardie){
  92. this.ChangeState(ChaseNpcState.Die);
  93. return
  94. }
  95. var distance = Vector3.distance(PlayerControl.Instance.Player.transform.position,this.m_chasenpcmodel.transform.position);
  96. if(distance < this.Distance && !this.atked){
  97. Vector3.subtract(this.m_hostage.transform.position,this.m_chasenpcmodel.transform.position,this.currotV);
  98. Vector3.normalize(this.currotV,this.currotV);
  99. this.m_chasenpcmodel.transform.lookAt(new Vector3(this.m_chasenpcmodel.transform.position.x-this.currotV.x,this.m_chasenpcmodel.transform.position.y,this.m_chasenpcmodel.transform.position.z-this.currotV.z),new Vector3(0,1,0));
  100. this.NPCMoveDir = this.currotV;
  101. this.canmove = true;
  102. _host.canmove = true;
  103. // this.ChangeState(ChaseNpcState.Run);
  104. let _dis = this.m_chasenpcmodel.transform.position.z - this.NpcStartPos.z;
  105. if(_dis>= this.ChaseDistance){
  106. this.atked = true;
  107. this.m_chasenpcspeed = this.NPCRushSpeed;
  108. this.ChangeState(ChaseNpcState.Atk);
  109. }
  110. }
  111. // else if(distance <= this.atkdistance && !this.atked){
  112. // Vector3.subtract(this.m_hostage.transform.position,this.m_chasenpcmodel.transform.position,this.currotV);
  113. // Vector3.normalize(this.currotV,this.currotV);
  114. // this.m_chasenpcmodel.transform.lookAt(new Vector3(this.m_chasenpcmodel.transform.position.x-this.currotV.x,this.m_chasenpcmodel.transform.position.y,this.m_chasenpcmodel.transform.position.z-this.currotV.z),new Vector3(0,1,0));
  115. // this.NPCMoveDir=this.currotV;
  116. // this.canmove = false;
  117. // this.atked = true;
  118. // this.ChangeState(ChaseNpcState.Atk);
  119. // }
  120. this.Move(this.NPCMoveDir,this.m_chasenpcspeed);
  121. this.RayCheck();
  122. }
  123. curspeed = 0;
  124. offest = new Vector3();
  125. curpos = new Vector3();
  126. Move(_dir:Vector3,_speed:number){
  127. let stage = Number(AccountManager.Instance.curplayerData.GainAccount("stage"));
  128. if(stage <= 2)return
  129. if(this.canmove ){
  130. var sp = GameTools.Instance.lerp(this.curspeed,_speed,0.1);
  131. this.curspeed = sp;
  132. Vector3.scale(_dir,this.curspeed,this.offest);
  133. Vector3.add(this.m_chasenpcmodel.transform.position,this.offest,this.curpos);
  134. Vector3.lerp(this.m_chasenpcmodel.transform.position,this.curpos,0.15,this.curpos);
  135. this.curpos = new Vector3(this.curpos.x,this.curpos.y,this.curpos.z);
  136. this.m_chasenpcmodel.transform.position = this.curpos;
  137. }
  138. }
  139. AddSpeed(){
  140. this.m_chasenpcspeed = StageControl.Instance.curstage.npcspeed;
  141. }
  142. SlowSpeed(){
  143. this.m_chasenpcspeed = StageControl.Instance.curstage.npcspeed/2;
  144. }
  145. GameOver(){
  146. EventManager.StageOff(StageState.Press,this,this.AddSpeed);
  147. EventManager.StageOff(StageState.UnPress,this,this.SlowSpeed);
  148. EventManager.StageOff(StageState.Start,this,this.Begin);
  149. EventManager.StageOff(StageState.Lose,this,this.GameOver);
  150. EventManager.StageOff(StageState.Win,this,this.GameOver);
  151. // this.canmove = false;
  152. }
  153. ChangeState(_state:ChaseNpcState){
  154. if(this.m_chasenpcstate == _state) return
  155. this.m_chasenpcstate = _state;
  156. switch (this.m_chasenpcstate) {
  157. case ChaseNpcState.Atk:
  158. this.Attack();
  159. break;
  160. case ChaseNpcState.Idle:
  161. this.Idle();
  162. break;
  163. case ChaseNpcState.Run:
  164. this.Run();
  165. break;
  166. case ChaseNpcState.Die:
  167. this.Die();
  168. break;
  169. }
  170. }
  171. //站立
  172. Idle(){
  173. this.m_chasenpcanimator.play("idle");
  174. }
  175. //攻击
  176. Attack(){
  177. this.m_chasenpcanimator.play("atk");
  178. // this.canmove = false;
  179. // Laya.timer.frameLoop(1,this,this.AnimatorState);
  180. Laya.timer.once(500,this,()=>{
  181. this.canmove = false;
  182. })
  183. }
  184. //获胜
  185. Win(){
  186. this.m_chasenpcanimator.play("win");
  187. }
  188. //奔跑
  189. Run(){
  190. this.m_chasenpcanimator.play("run");
  191. }
  192. die = false;
  193. Die(){
  194. if(this.die)return
  195. this.die = true;
  196. AudioManager.playMusic(MusicType.die);
  197. this.PiaoZi();
  198. // GameManager.Instance.KillNum++;
  199. // InGameView.Instance.InitUI();
  200. this.isdead = true;
  201. EventManager.StageOff(StageState.Start,this,this.Begin);
  202. // Laya.timer.clearAll(this);
  203. if(this.frontdie){
  204. this.m_chasenpcanimator.play("diefront");
  205. }
  206. if(this.reardie){
  207. this.m_chasenpcanimator.play("dierear");
  208. }
  209. this.m_chasenpcanimator.play("diefront");
  210. this.canmove = false;
  211. }
  212. PiaoZi(){
  213. let headshoot = new Laya.Image();
  214. headshoot.skin = "Game/res/textrue/headshoot.png";
  215. headshoot.scaleX = 0.5;
  216. headshoot.scaleY = 0.5;
  217. headshoot.anchorX = 0.5;
  218. headshoot.anchorY = 0.5;
  219. Laya.stage.addChild(headshoot);
  220. Laya.timer.once(1000,this,()=>{
  221. headshoot.visible = false;
  222. Laya.timer.clear(this,this.UIFollow);
  223. })
  224. GameManager.Instance.UIArray.push(headshoot);
  225. let UIPos = this.m_chasenpcmodel.getChildByName("UIPos") as Laya.Sprite3D;
  226. let screenpos2 = new Vector3();
  227. Laya.timer.frameLoop(1,this,this.UIFollow,[UIPos,headshoot,screenpos2]);
  228. }
  229. UIFollow(UIPos:Laya.Sprite3D,UI:Laya.Image,POS:Laya.Vector3){
  230. AssetManager.Instance.maincamera.worldToViewportPoint(UIPos.transform.position,POS);
  231. UI.pos(POS.x,POS.y);
  232. }
  233. // UIFollow(UIPos:Laya.Sprite3D,UI:Laya.Image,POS:Laya.Vector3){
  234. // AssetManager.Instance.maincamera.worldToViewportPoint(UIPos.transform.position,POS);
  235. // UI.pos(POS.x,POS.y);
  236. // }
  237. RayCheck(){
  238. // if(this.wined)return;
  239. let startpos = new Vector3(this.m_chasenpcmodel.transform.position.x,this.m_chasenpcmodel.transform.position.y+0.5,this.m_chasenpcmodel.transform.position.z);
  240. let dir = Transform3DHelper.getForward(this.m_chasenpcmodel.transform);
  241. let ray = new Laya.Ray(startpos,dir);
  242. let hitres = new Laya.HitResult();
  243. let isHit = AssetManager.Instance.mainscene.physicsSimulation.rayCast(ray,hitres,3,200);
  244. if(isHit){
  245. let target = hitres.collider.owner.parent as Laya.Sprite3D;
  246. if(target.name == "hostage"){
  247. let _hostage = target.getComponent(Hostage) as Hostage;
  248. _hostage.isdead = true;
  249. // this.canmove = false;
  250. }
  251. else if(target.name == "bonfire"){
  252. this.ChangeState(ChaseNpcState.Die);
  253. }
  254. else if(target.name == "stone"){
  255. this.m_chasenpcspeed = 0;
  256. }
  257. else if(target.name == ""){
  258. }
  259. }
  260. }
  261. }