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

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