9f67067e973e72ab398dd82c5825bd0b646e8c97 6.6 KB

123456789101112
  1. {
  2. "code": "var Vector3 = Laya.Vector3;\r\nimport { EventManager, StageState } from \"../Tools/EventManager\";\r\nimport { GameTools, Transform3DHelper } from \"../Tools/GameTools\";\r\nimport { PlayerControl } from \"./PlayerControl\";\r\nimport { AssetManager } from \"../Tools/AssetManager\";\r\nimport { Runner } from \"./Runner\";\r\nexport var NpcState;\r\n(function (NpcState) {\r\n NpcState[NpcState[\"Attack\"] = 0] = \"Attack\";\r\n NpcState[NpcState[\"Idle\"] = 1] = \"Idle\";\r\n NpcState[NpcState[\"Win\"] = 2] = \"Win\";\r\n NpcState[NpcState[\"Pursue\"] = 3] = \"Pursue\";\r\n NpcState[NpcState[\"Run\"] = 4] = \"Run\";\r\n NpcState[NpcState[\"Die\"] = 5] = \"Die\";\r\n})(NpcState || (NpcState = {}));\r\nexport class NpcRunner extends Laya.Script {\r\n constructor() {\r\n super();\r\n this.m_distance = 50;\r\n this.m_distanceatk = 5;\r\n this.DIE = false;\r\n this.m_npcspeed = 1;\r\n this.atked = false;\r\n this.frontdie = false;\r\n this.reardie = false;\r\n this.canmove = false;\r\n this.dir = new Vector3();\r\n this.currotV = new Vector3();\r\n this.currot = new Laya.Quaternion;\r\n this.CurrectRotate = new Laya.Quaternion();\r\n this.offest = new Vector3();\r\n this.curpos = new Vector3();\r\n NpcRunner.ins = this;\r\n EventManager.StageOn(StageState.Start, this, this.Begin);\r\n }\r\n static get Instance() {\r\n if (NpcRunner.ins) {\r\n return NpcRunner.ins;\r\n }\r\n else {\r\n return new NpcRunner();\r\n }\r\n }\r\n onAwake() {\r\n this.m_npcmodel = this.owner;\r\n this.m_npcanimator = this.owner.getComponent(Laya.Animator);\r\n this.player = PlayerControl.Instance.Player;\r\n }\r\n Begin() {\r\n this.ChangeState(NpcState.Idle);\r\n }\r\n onUpdate() {\r\n if (this.frontdie || this.reardie) {\r\n this.ChangeState(NpcState.Die);\r\n return;\r\n }\r\n var distance = Vector3.distance(this.player.transform.position, this.m_npcmodel.transform.position);\r\n if (distance < this.m_distance && distance > this.m_distanceatk && !this.atked) {\r\n Vector3.subtract(this.player.transform.position, this.m_npcmodel.transform.position, this.currotV);\r\n Vector3.normalize(this.currotV, this.currotV);\r\n this.m_npcmodel.transform.lookAt(new Vector3(this.m_npcmodel.transform.position.x - this.currotV.x, this.m_npcmodel.transform.position.y, this.m_npcmodel.transform.position.z - this.currotV.z), new Vector3(0, 1, 0));\r\n this.NPCMoveDir = this.currotV;\r\n this.canmove = true;\r\n this.ChangeState(NpcState.Run);\r\n }\r\n else if (distance <= this.m_distanceatk) {\r\n Vector3.subtract(this.player.transform.position, this.m_npcmodel.transform.position, this.currotV);\r\n Vector3.normalize(this.currotV, this.currotV);\r\n this.m_npcmodel.transform.lookAt(new Vector3(this.m_npcmodel.transform.position.x - this.currotV.x, this.m_npcmodel.transform.position.y, this.m_npcmodel.transform.position.z - this.currotV.z), new Vector3(0, 1, 0));\r\n this.NPCMoveDir = this.currotV;\r\n this.canmove = false;\r\n this.atked = true;\r\n this.ChangeState(NpcState.Attack);\r\n }\r\n this.Move(this.NPCMoveDir, 2);\r\n this.RayCheck();\r\n }\r\n Move(_dir, _speed) {\r\n if (this.canmove && !this.atked) {\r\n var sp = GameTools.Instance.lerp(this.m_npcspeed, _speed, 0.1);\r\n this.m_npcspeed = sp;\r\n Vector3.scale(_dir, this.m_npcspeed, this.offest);\r\n Vector3.add(this.m_npcmodel.transform.position, this.offest, this.curpos);\r\n Vector3.lerp(this.m_npcmodel.transform.position, this.curpos, 0.15, this.curpos);\r\n this.curpos = new Vector3(this.curpos.x, this.curpos.y, this.curpos.z);\r\n this.m_npcmodel.transform.position = this.curpos;\r\n }\r\n }\r\n ChangeState(_state) {\r\n if (this.npc_state == _state)\r\n return;\r\n this.npc_state = _state;\r\n switch (this.npc_state) {\r\n case NpcState.Attack:\r\n this.Attack();\r\n break;\r\n case NpcState.Pursue:\r\n this.Pursue();\r\n break;\r\n case NpcState.Win:\r\n this.Win();\r\n break;\r\n case NpcState.Idle:\r\n this.Idle();\r\n break;\r\n case NpcState.Run:\r\n this.Run();\r\n break;\r\n case NpcState.Die:\r\n this.Die();\r\n break;\r\n }\r\n }\r\n Idle() {\r\n this.m_npcanimator.play(\"idle\");\r\n }\r\n Attack() {\r\n this.m_npcanimator.play(\"atk\");\r\n }\r\n Win() {\r\n this.m_npcanimator.play(\"win\");\r\n }\r\n Pursue() {\r\n }\r\n Run() {\r\n this.m_npcanimator.play(\"run\");\r\n }\r\n Die() {\r\n if (this.frontdie) {\r\n this.m_npcanimator.play(\"diefront\");\r\n return;\r\n }\r\n if (this.reardie) {\r\n this.m_npcanimator.play(\"dierear\");\r\n return;\r\n }\r\n this.canmove = false;\r\n }\r\n RayCheck() {\r\n let startpos = new Vector3(this.m_npcmodel.transform.position.x, this.m_npcmodel.transform.position.y + 1, this.m_npcmodel.transform.position.z);\r\n let dir = Transform3DHelper.getForward(this.m_npcmodel.transform);\r\n let ray = new Laya.Ray(startpos, dir);\r\n let hitres = new Laya.HitResult();\r\n let isHit = AssetManager.Instance.mainscene.physicsSimulation.rayCast(ray, hitres, 3, 200);\r\n if (isHit) {\r\n let target = hitres.collider.owner.parent;\r\n if (target.name == \"player\") {\r\n let runner = target.getComponent(Runner);\r\n let ani = target.getComponent(Laya.Animator);\r\n runner.isdead = true;\r\n this.ChangeState(NpcState.Win);\r\n }\r\n }\r\n }\r\n}\r\n",
  3. "references": [
  4. "E:/LayaProject/ArcherWorrior/src/Game/GameManager.ts",
  5. "E:/LayaProject/ArcherWorrior/src/Tools/EventManager.ts",
  6. "E:/LayaProject/ArcherWorrior/src/Tools/GameTools.ts",
  7. "E:/LayaProject/ArcherWorrior/src/Game/PlayerControl.ts",
  8. "E:/LayaProject/ArcherWorrior/src/Tools/AssetManager.ts",
  9. "E:/LayaProject/ArcherWorrior/src/Game/Runner.ts"
  10. ]
  11. }