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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. 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.FSM.stateLife)
  52. self.FSM.stateLife += 1
  53. duration = 500
  54. if self.FSM.stateLife > duration:
  55. num = randint(1,2)
  56. if num == 1:
  57. self.FSM.ToTransition('toTarget2')
  58. elif num ==2:
  59. self.FSM.ToTransition('toTarget3')
  60. else:
  61. self.FSM.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,FSM):
  70. super(Target2, self).__init__(FSM)
  71. def Enter(self):
  72. #print('Preparing to walk towards target 2.')
  73. self.cont = bge.logic.getCurrentController()
  74. self.FSM.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.FSM.stateLife)
  90. self.FSM.stateLife += 1
  91. duration = 1000
  92. if self.FSM.stateLife > duration:
  93. num = randint(1,3)
  94. if num == 1:
  95. self.FSM.ToTransition('toTarget1')
  96. elif num ==2:
  97. self.FSM.ToTransition('toTarget3')
  98. else:
  99. self.FSM.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,FSM):
  108. super(Target3, self).__init__(FSM)
  109. def Enter(self):
  110. #print('Preparing to walk towards target 3.')
  111. self.cont = bge.logic.getCurrentController()
  112. self.FSM.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.FSM.stateLife += 1
  128. duration = 1200
  129. if self.FSM.stateLife > duration:
  130. if not (randint(1,3) % 2):
  131. self.FSM.ToTransition('toTarget1')
  132. else:
  133. self.FSM.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,FSM):
  144. super(Idle, self).__init__(FSM)
  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.FSM.stateLife = 1
  151. super(Idle, self).Enter()
  152. def Execute(self):
  153. #print('Idleing.', self.FSM.stateLife)
  154. self.FSM.stateLife += 1
  155. duration = 300
  156. if self.FSM.stateLife > duration:
  157. #num = randint(1,4)
  158. self.FSM.ToTransition('toTarget1')
  159. # if num == 1:
  160. # self.FSM.ToTransition('toTarget1')
  161. # elif num == 2:
  162. # self.FSM.ToTransition('toTarget2')
  163. # elif num == 3:
  164. # self.FSM.ToTransition('toTarget3')
  165. # elif num == 4 or num == 5 or num == 6:
  166. # self.FSM.ToTransition('toImpatient')
  167. # else:
  168. # self.FSM.ToTransition('toIdle')
  169. else:
  170. #pass
  171. #print(self.own.children)
  172. self.npcArm.playAction('g_idle', 1,201, layer=2, play_mode=0, speed=.5)
  173. def Exit(self):
  174. pass
  175. ##print('Waking up from idle.')
  176. class Startup(State):
  177. def __init__(self,FSM):
  178. super(Startup, self).__init__(FSM)
  179. def Enter(self):
  180. self.FSM.stateLife = 1
  181. super(Startup, self).Enter()
  182. def Execute(self):
  183. self.FSM.stateLife += 1
  184. duration = 4
  185. if self.FSM.stateLife > duration:
  186. self.FSM.ToTransition('toIdle')
  187. def Exit(self):
  188. #pass
  189. print('Exiting npc startup.')
  190. class Impatient(State):
  191. def __init__(self,FSM):
  192. super(Impatient, self).__init__(FSM)
  193. def Enter(self):
  194. #print('Starting to idle.')
  195. self.FSM.stateLife = 1
  196. self.cont = bge.logic.getCurrentController()
  197. self.own = self.cont.owner
  198. self.npcArm = self.own.children['npc']
  199. super(Impatient, self).Enter()
  200. def Execute(self):
  201. ##print('being Impatient.', self.FSM.stateLife)
  202. self.FSM.stateLife += 1
  203. duration = 300
  204. if self.FSM.stateLife > duration:
  205. self.FSM.ToTransition('toIdle')
  206. else:
  207. pass
  208. #self.npcArm.playAction('npcImpatient', 1,201, layer=2, play_mode=0, speed=.5)
  209. def Exit(self):
  210. pass
  211. #print('stopping bening impatient.')
  212. #===================================
  213. class FSM(object):
  214. def __init__ (self, character):
  215. self.char = character
  216. self.states = {}
  217. self.transitions = {}
  218. self.curState = None
  219. self.prevState = None
  220. self.trans = None
  221. self.stateLife = 0
  222. def AddTransition(self, transName, transition):
  223. self.transitions[transName] = transition
  224. def AddState(self, stateName, state):
  225. self.states[stateName] = state
  226. def SetState(self, stateName):
  227. self.prevState = self.curState
  228. self.curState = self.states[stateName]
  229. def ToTransition(self, toTrans):
  230. self.trans = self.transitions[toTrans]
  231. def Execute(self):
  232. if (self.trans):
  233. self.curState.Exit()
  234. self.trans.Execute()
  235. self.SetState(self.trans.toState)
  236. self.curState.Enter()
  237. self.trans = None
  238. self.curState.Execute()
  239. #====================================
  240. Char = type("Char",(object,),{})
  241. class Walker(Char):
  242. def __init__(self):
  243. self.FSM = FSM(self)
  244. #cont = bge.logic.getCurrentController()
  245. #own = cont.owner
  246. #self.LightOn = own['state']
  247. ##STATES
  248. self.FSM.AddState("Startup", Startup(self.FSM))
  249. self.FSM.AddState("Idle", Idle(self.FSM))
  250. self.FSM.AddState("Impatient", Impatient(self.FSM))
  251. self.FSM.AddState('Target1', Target1(self.FSM))
  252. self.FSM.AddState('Target3', Target3(self.FSM))
  253. self.FSM.AddState('Target2', Target2(self.FSM))
  254. #TRANSITIONS
  255. self.FSM.AddTransition('toStartup', Transition('Startup'))
  256. self.FSM.AddTransition('toIdle', Transition('Idle'))
  257. self.FSM.AddTransition('toImpatient', Transition('Impatient'))
  258. self.FSM.AddTransition('toTarget3', Transition('Target3'))
  259. self.FSM.AddTransition('toTarget1', Transition('Target1'))
  260. self.FSM.AddTransition('toTarget2', Transition('Target2'))
  261. if self.FSM.curState == None:
  262. self.FSM.SetState('Startup')
  263. #self.FSM.ToTransition('toIdle')
  264. #print('setting none')
  265. def Execute(self):
  266. self.FSM.Execute()
  267. #====================================
  268. r = Walker()
  269. def main(cont):
  270. own = cont.owner
  271. scene = bge.logic.getCurrentScene()
  272. if 'inited' not in own:
  273. own['inited'] = True
  274. own['frame'] = 0
  275. own['state'] = 'On'
  276. own['navMesh'] = None
  277. for x in scene.objects:
  278. if 'npcNavmesh' in x:
  279. own['navMesh'] = x
  280. #print('initing')
  281. if own['frame'] == 40:
  282. own.worldPosition = [0,0,50]
  283. ln = own['cName'] + '_loader'
  284. if ln in scene.objects:
  285. to = scene.objects[ln]
  286. own.worldPosition = to.worldPosition
  287. own.worldPosition.z += 50
  288. r.Execute()
  289. ##print(r.FSM.curState)
  290. own['frame'] += 1
  291. yvel = own.linearVelocity.y
  292. yvel = yvel *.05
  293. if own.linearVelocity.y > .01 or own.linearVelocity.y < -.01:
  294. own.applyRotation([0,0,yvel], True)