Shuvit game master repo. http://shuvit.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

walker_states.py 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. import bge
  2. from random import randint
  3. from time import clock
  4. #====================================
  5. class Transition(object):
  6. def __init__(self, toState):
  7. self.toState = toState
  8. def Execute(self):
  9. pass
  10. #====================================
  11. State = type("State", (object,), {})
  12. class State(object):
  13. def __init__(self, npcFSM):
  14. self.npcFSM = npcFSM
  15. self.timer = 0
  16. self.startTime = 0
  17. def Enter(self):
  18. pass
  19. def Execute(self):
  20. pass
  21. def Exit(self):
  22. pass
  23. class Target1(State):
  24. def __init__(self,npcFSM):
  25. super(Target1, self).__init__(npcFSM)
  26. def Enter(self):
  27. self.cont = bge.logic.getCurrentController()
  28. self.npcFSM.stateLife = 1
  29. self.own = self.cont.owner
  30. self.target = 'larryTarget'
  31. self.npcArm = self.own.children['npc']
  32. self.scene = bge.logic.getCurrentScene()
  33. nm = self.own['navMesh']
  34. self.pt = nm.findPath(self.own.worldPosition, self.scene.objects[self.target].worldPosition)
  35. print('target1')
  36. super(Target1, self).Enter()
  37. def Execute(self):
  38. self.npcFSM.stateLife += 1
  39. duration = 50000
  40. if len(self.pt) > 0:
  41. target = self.pt[0]
  42. vg = self.own.getVectTo(target)
  43. dist = vg[0]
  44. v = vg[1]
  45. self.own.alignAxisToVect(v, 0, .5)
  46. self.own.alignAxisToVect([0,0,1], 2, 1)
  47. #print(dist)
  48. if dist > 1.5:
  49. x = 10
  50. max_vel = 1.5
  51. if self.own.linearVelocity.x < max_vel:
  52. self.own.applyForce([x, 0, 5], True)
  53. else:
  54. self.pt.remove(self.pt[0])
  55. else:
  56. print('goal reached')
  57. self.npcFSM.ToTransition('toTarget2')
  58. if self.npcFSM.stateLife > duration:
  59. num = randint(1,2)
  60. else:
  61. self.npcArm.playAction('g_walk2', 1,62, layer=2, play_mode=0, speed=.5)
  62. def Exit(self):
  63. pass
  64. class Target2(State):
  65. def __init__(self,npcFSM):
  66. super(Target2, self).__init__(npcFSM)
  67. def Enter(self):
  68. self.cont = bge.logic.getCurrentController()
  69. self.npcFSM.stateLife = 1
  70. self.own = self.cont.owner
  71. self.target = 'larryTarget.001'
  72. self.npcArm = self.own.children['npc']
  73. self.scene = bge.logic.getCurrentScene()
  74. nm = self.own['navMesh']
  75. self.pt = nm.findPath(self.own.worldPosition, self.scene.objects[self.target].worldPosition)
  76. print('target2')
  77. super(Target2, self).Enter()
  78. def Execute(self):
  79. self.npcFSM.stateLife += 1
  80. duration = 10000
  81. if len(self.pt) > 0:
  82. target = self.pt[0]
  83. vg = self.own.getVectTo(target)
  84. dist = vg[0]
  85. v = vg[1]
  86. self.own.alignAxisToVect(v, 0, .5)
  87. self.own.alignAxisToVect([0,0,1], 2, 1)
  88. if dist > 1.5:
  89. x = 10
  90. max_vel = 1.5
  91. if self.own.linearVelocity.x < max_vel:
  92. self.own.applyForce([x, 0, 5], True)
  93. else:
  94. self.pt.remove(self.pt[0])
  95. else:
  96. print('goal reached')
  97. self.npcFSM.ToTransition('toTarget3')
  98. if self.npcFSM.stateLife > duration:
  99. num = randint(1,3)
  100. else:
  101. self.npcArm.playAction('g_walk2', 1,62, layer=2, play_mode=0, speed=.5)
  102. def Exit(self):
  103. pass
  104. class Target3(State):
  105. def __init__(self,npcFSM):
  106. super(Target3, self).__init__(npcFSM)
  107. def Enter(self):
  108. self.cont = bge.logic.getCurrentController()
  109. self.npcFSM.stateLife = 1
  110. self.own = self.cont.owner
  111. self.target = 'larryTarget.002'
  112. self.npcArm = self.own.children['npc']
  113. self.scene = bge.logic.getCurrentScene()
  114. nm = self.own['navMesh']
  115. self.pt = nm.findPath(self.own.worldPosition, self.scene.objects[self.target].worldPosition)
  116. print('target3')
  117. super(Target3, self).Enter()
  118. def Execute(self):
  119. self.npcFSM.stateLife += 1
  120. duration = 12000
  121. if len(self.pt) > 0:
  122. target = self.pt[0]
  123. vg = self.own.getVectTo(target)
  124. dist = vg[0]
  125. v = vg[1]
  126. self.own.alignAxisToVect(v, 0, .5)
  127. self.own.alignAxisToVect([0,0,1], 2, 1)
  128. #print(dist)
  129. if dist > 1.5:
  130. x = 10
  131. max_vel = 1.5
  132. if self.own.linearVelocity.x < max_vel:
  133. self.own.applyForce([x, 0, 5], True)
  134. else:
  135. self.pt.remove(self.pt[0])
  136. else:
  137. print('goal reached')
  138. self.npcFSM.ToTransition('toTarget1')
  139. if self.npcFSM.stateLife > duration:
  140. self.npcFSM.ToTransition('toTarget1')
  141. else:
  142. cont = bge.logic.getCurrentController()
  143. own = cont.owner
  144. npcArm = own.children['npc']
  145. npcArm.playAction('g_walk2', 1,62, layer=2, play_mode=0, speed=.5)
  146. def Exit(self):
  147. pass
  148. class Idle(State):
  149. def __init__(self,npcFSM):
  150. super(Idle, self).__init__(npcFSM)
  151. def Enter(self):
  152. self.cont = bge.logic.getCurrentController()
  153. self.own = self.cont.owner
  154. self.npcArm = self.own.children['npc']
  155. self.npcFSM.stateLife = 1
  156. super(Idle, self).Enter()
  157. def Execute(self):
  158. self.npcFSM.stateLife += 1
  159. duration = 300
  160. if self.npcFSM.stateLife > duration:
  161. num = randint(1,4)
  162. self.npcFSM.ToTransition('toTarget1')
  163. else:
  164. self.npcArm.playAction('g_idle', 1,201, layer=2, play_mode=0, speed=.5)
  165. def Exit(self):
  166. pass
  167. class Startup(State):
  168. def __init__(self,npcFSM):
  169. super(Startup, self).__init__(npcFSM)
  170. def Enter(self):
  171. self.npcFSM.stateLife = 1
  172. print('npc startup enter')
  173. super(Startup, self).Enter()
  174. def Execute(self):
  175. self.npcFSM.stateLife += 1
  176. duration = 4
  177. #print('executing', self.npcFSM.stateLife)
  178. if self.npcFSM.stateLife > duration:
  179. self.npcFSM.ToTransition('toIdle')
  180. def Exit(self):
  181. #pass
  182. print('Exiting npc startup.')
  183. class Impatient(State):
  184. def __init__(self,npcFSM):
  185. super(Impatient, self).__init__(npcFSM)
  186. def Enter(self):
  187. self.npcFSM.stateLife = 1
  188. self.cont = bge.logic.getCurrentController()
  189. self.own = self.cont.owner
  190. self.npcArm = self.own.children['npc']
  191. super(Impatient, self).Enter()
  192. def Execute(self):
  193. self.npcFSM.stateLife += 1
  194. duration = 300
  195. if self.npcFSM.stateLife > duration:
  196. self.npcFSM.ToTransition('toIdle')
  197. else:
  198. #pass
  199. self.npcArm.playAction('npcImpatient', 1,201, layer=2, play_mode=0, speed=.5)
  200. def Exit(self):
  201. pass
  202. #===================================
  203. class npcFSM(object):
  204. def __init__ (self, character):
  205. self.char = character
  206. self.states = {}
  207. self.transitions = {}
  208. self.curState = None
  209. self.prevState = None
  210. self.trans = None
  211. self.stateLife = 0
  212. def AddTransition(self, transName, transition):
  213. self.transitions[transName] = transition
  214. def AddState(self, stateName, state):
  215. self.states[stateName] = state
  216. def SetState(self, stateName):
  217. self.prevState = self.curState
  218. self.curState = self.states[stateName]
  219. def ToTransition(self, toTrans):
  220. self.trans = self.transitions[toTrans]
  221. def Execute(self):
  222. if (self.trans):
  223. self.curState.Exit()
  224. self.trans.Execute()
  225. self.SetState(self.trans.toState)
  226. self.curState.Enter()
  227. self.trans = None
  228. self.curState.Execute()
  229. #====================================
  230. Char = type("Char",(object,),{})
  231. class Walker(Char):
  232. def __init__(self):
  233. self.npcFSM = npcFSM(self)
  234. ##STATES
  235. self.npcFSM.AddState("Startup", Startup(self.npcFSM))
  236. self.npcFSM.AddState("Idle", Idle(self.npcFSM))
  237. self.npcFSM.AddState("Impatient", Impatient(self.npcFSM))
  238. self.npcFSM.AddState('Target1', Target1(self.npcFSM))
  239. self.npcFSM.AddState('Target3', Target3(self.npcFSM))
  240. self.npcFSM.AddState('Target2', Target2(self.npcFSM))
  241. #TRANSITIONS
  242. self.npcFSM.AddTransition('toStartup', Transition('Startup'))
  243. self.npcFSM.AddTransition('toIdle', Transition('Idle'))
  244. self.npcFSM.AddTransition('toImpatient', Transition('Impatient'))
  245. self.npcFSM.AddTransition('toTarget3', Transition('Target3'))
  246. self.npcFSM.AddTransition('toTarget1', Transition('Target1'))
  247. self.npcFSM.AddTransition('toTarget2', Transition('Target2'))
  248. if self.npcFSM.curState == None:
  249. self.npcFSM.SetState('Startup')
  250. def Execute(self):
  251. self.npcFSM.Execute()
  252. #====================================
  253. r = Walker()
  254. def main(cont):
  255. own = cont.owner
  256. scene = bge.logic.getCurrentScene()
  257. if 'inited' not in own:
  258. own['inited'] = True
  259. own['frame'] = 0
  260. own['state'] = 'On'
  261. own['navMesh'] = None
  262. for x in scene.objects:
  263. if 'npcNavmesh' in x:
  264. own['navMesh'] = x
  265. if own['frame'] == 40:
  266. own.worldPosition = [0,0,50]
  267. ln = own['cName'] + '_loader'
  268. if ln in scene.objects:
  269. to = scene.objects[ln]
  270. own.worldPosition = to.worldPosition
  271. own.worldOrientation = to.worldOrientation
  272. r.Execute()
  273. own['frame'] += 1
  274. yvel = own.linearVelocity.y
  275. yvel = yvel *.05
  276. if own.linearVelocity.y > .01 or own.linearVelocity.y < -.01:
  277. own.applyRotation([0,0,yvel], True)