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 10KB

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