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.0KB

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