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

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