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.

camera.py 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. #zskcam
  2. import bge
  3. import mathutils
  4. from mathutils import *
  5. scene = bge.logic.getCurrentScene()
  6. import camFSM
  7. import birds
  8. import sound_man
  9. import FSM
  10. import cars
  11. def main(cont):
  12. #camFSM.main(cont)
  13. own = cont.owner
  14. dict = bge.logic.globalDict
  15. camempty = scene.objects['camEmpty.001']
  16. controlcube = scene.objects['control_cube.002']
  17. camCube = scene.objects['camCube']
  18. dropinCol = controlcube.sensors['dropinCol']
  19. LAST_GRIND = controlcube['LAST_GRIND']
  20. up = cont.sensors['up']
  21. ray = cont.sensors['up']
  22. down = cont.sensors['down']
  23. left = cont.sensors['left']
  24. right = cont.sensors['right']
  25. distance = 0.12
  26. cam = cont.actuators["Camera"]
  27. near = cont.sensors["Near"]
  28. move = own.actuators["move"]
  29. cam_def_height = dict['cam_height']
  30. cam_def_min = dict['cam_min']
  31. cam_def_max = dict['cam_max']
  32. cam_height = cam.height
  33. cam_min = cam.min
  34. cam_max = cam.max
  35. lasty = controlcube['lasty']
  36. cam_moved = 0
  37. lastCamheight = controlcube['lastCamheight']
  38. raised = 0
  39. walk = controlcube['walk']
  40. cam.axis = 4
  41. ccheight = controlcube.worldPosition[2]
  42. camwpz = own.worldPosition[2]
  43. zdist = camwpz - ccheight
  44. cam1 = scene.objects['Camera.003']
  45. cam2 = scene.objects['freecam']
  46. cam3 = scene.objects['followcam']
  47. camList = scene.cameras
  48. freecam = camList["freecam"]
  49. if 'init' not in own:
  50. own['init'] = 1
  51. own['last_move_x'] = 0
  52. own['last_move_y'] = 0
  53. own['last_move_z'] = 0
  54. own['last_rot_x'] = 0
  55. own['last_rot_y'] = 0
  56. own['last_rot_z'] = 0
  57. own['idlecampos_x'] = 0
  58. own['speed_mult'] = 1.00
  59. controlcube['driving'] = False
  60. dict['cur_ccH_min'] = dict['cam_walk_min']
  61. dict['cur_ccH_max'] = dict['cam_walk_max']
  62. cam.height = dict['cam_height']
  63. dict['cam_state'] = 'walking'
  64. controlcube['ragdoll_active'] = False
  65. dict['camera'] = cam1
  66. own['NpcPedFSM'] = FSM.NpcPed(own)
  67. own['CamFSM'] = FSM.CameraFSM(own)
  68. own['life'] = 0
  69. acam = scene.active_camera
  70. def get_cam_state():
  71. if dict['npause']:
  72. if dict['menu_idle_timer'] > 300:
  73. #dict['cam_state'] = 'pause_idle'
  74. dict['cam_state'] = 'PauseIdleCam'
  75. else:
  76. dict['cam_state'] = 'PauseCam'
  77. elif walk == 1:
  78. if controlcube['ragdoll_active']:
  79. dict['cam_state'] = 'RagdollCam'
  80. else:
  81. dict['cam_state'] = 'WalkCam'
  82. elif controlcube['driving']:
  83. dict['cam_state'] = 'driving'
  84. else:
  85. dict['cam_state'] = 'RollCam'
  86. #print(dict['cam_state'])
  87. if controlcube['driving'] == False:
  88. LAST_GRIND = False
  89. if down.triggered == True and LAST_GRIND == False and walk == 0:
  90. hitPosition = down.hitPosition
  91. distance = own.getDistanceTo(hitPosition)
  92. cam_moved = 1
  93. if down.triggered == False and LAST_GRIND == False and cam_moved == 0 and walk == 0:
  94. camempty['hitdown'] = False
  95. if cam_height > (cam_def_height + .08) and zdist > -.2 and raised == 0:
  96. cam.height = cam_height - .02
  97. #print('raising a')
  98. if cam_height < -.6 and cam_moved == 0 and LAST_GRIND == False and zdist > -.2 and raised == 0 and walk == 0:
  99. cam_height = .1
  100. #print('raising b')
  101. if LAST_GRIND == True and walk == 0:
  102. if cam.height < -.5 or zdist < -.2:
  103. cam.height = cam.height + .013
  104. #print('raising c')
  105. if cam.height >= -.5 and not down.triggered:
  106. pass
  107. controlcube['lastCamheight'] = cam.height
  108. #activating######
  109. cont.activate(own.actuators['Camera'])
  110. #################
  111. if near.triggered == True and walk == 0:
  112. #print("near")
  113. if cam.min < 1:
  114. cam.min = cam.min + .1
  115. cam.max = cam.max + .1
  116. cam.height = cam_height + .01
  117. print('moving cam up')
  118. cam.damping = .001
  119. cont.activate(move)
  120. else:
  121. #print("far")
  122. cont.deactivate(move)
  123. if cam.min > cam_def_min:
  124. cam.min = cam.min - .05
  125. cam.max = cam.max - .05
  126. cam.damping = .0
  127. if cam.damping != 0 and down.triggered == False and near.triggered == False:
  128. cam.damping = 0.91
  129. obj = cont.owner
  130. cube = controlcube
  131. to = cube
  132. to2 = to.worldPosition
  133. pos = [to.worldPosition.x, to.worldPosition.y, to.worldPosition.z]
  134. pos[2] = pos[2] - .2
  135. from2 = obj
  136. from3 = [from2.worldPosition.x, from2.worldPosition.y, from2.worldPosition.z]
  137. from3[2] = from3[2] - .2
  138. distance = 0.0
  139. property = ""
  140. face = 1
  141. xray = 0
  142. poly = 0
  143. hit = obj.rayCast( pos, from3, distance, property, face, xray, poly)
  144. control = "control_cube.002"
  145. hitobj = hit[0]
  146. hitobj = str(hitobj)
  147. if hit[0]:
  148. #if hitobj != control and walk == 0 and 'vert' not in hit[0] and 'ground' in hit[0] and controlcube['ragdoll_active'] == False:
  149. if hitobj != control and walk == 0 and 'vert' not in hit[0] and 'ground' in hit[0]:
  150. cam.damping = .01
  151. if cam.height < 2:
  152. cam.height = cam_height + .1
  153. cam.max = cam.max - .2
  154. #print('small move', cam.height, dict['cam_height'])
  155. elif cam.height >= 2 and cam.height < 4:
  156. cam.height = cam_height + .05
  157. cam.max = cam.max - .05
  158. #print('big move')
  159. if dict['menu_idle_timer'] > 300:
  160. move_len = 2048
  161. if own['idlecampos_x'] < move_len:
  162. own['idlecampos_x'] += 1
  163. if own['idlecampos_x'] == move_len:
  164. own['idlecampos_x'] = 0
  165. if own['idlecampos_x'] < (move_len / 2):
  166. move = [.0001, 0, 0]
  167. freecam.applyMovement( move, True)
  168. freecam.applyRotation([.0001, 0, .0001], True)
  169. if own['idlecampos_x'] > (move_len / 2):
  170. move = [-.0001, 0, 0]
  171. freecam.applyMovement(move, True)
  172. freecam.applyRotation([-.0001, 0, -.0001], True)
  173. if dict['npause'] == True:
  174. cont.deactivate(own.actuators['Camera'])
  175. controlcube['camera'] = 2
  176. if controlcube['camera'] == 2 and dict['joy_con'] == 1:
  177. cont.deactivate(own.actuators['Camera'])
  178. scene.active_camera = freecam
  179. cont.activate(cube.actuators['freecam'])
  180. lLR = dict['lLR'] / .082 * 100
  181. lUD = dict['lUD'] / .082 * 100 - 20 / 80
  182. rLR = dict['rLR'] / .082 * 100 - 20 / 80
  183. rUD = dict['rUD'] / .082 * 100 - 20 / 80
  184. lTrig = dict['lTrig'] / .082 * 100 - 20 / 80
  185. rTrig = dict['rTrig'] / .082 * 100 - 20 / 80
  186. if lLR < -20:
  187. lmLR = round((lLR + 20) / 80 * 100, 0)
  188. elif lLR > 20:
  189. lmLR = round((lLR - 20) / 80 * 100, 0)
  190. else: lmLR = 0
  191. if lUD > 20:
  192. lmUD = round((lUD - 20) / 80 * 100, 0)
  193. elif lUD < -20:
  194. lmUD = round((lUD + 20) / 80 * 100, 0)
  195. else: lmUD = 0
  196. if rLR < -20:
  197. rmLR = round((rLR + 20) / 80 * 100, 0)
  198. elif rLR > 20:
  199. rmLR = round((rLR - 20) / 80 * 100, 0)
  200. else: rmLR = 0
  201. if rUD > 20:
  202. rmUD = round((rUD - 20) / 80 * 100, 0)
  203. elif rUD < -20:
  204. rmUD = round((rUD + 20) / 80 * 100, 0)
  205. else: rmUD = 0
  206. if lTrig > 3:
  207. mTrig = lTrig * -1
  208. elif rTrig > 3:
  209. mTrig = rTrig
  210. else: mTrig = 0
  211. #move camera
  212. damping = .95
  213. damping2 = 1.005
  214. mult = .0005 * own['speed_mult']
  215. move_x = lmUD * mult
  216. move_y = lmLR * mult
  217. move_z = (mTrig * -mult) / 2#4
  218. rot_mult = -.00015 * own['speed_mult']
  219. rot_x = rmUD * rot_mult
  220. rot_y = rmLR * rot_mult
  221. if move_x == 0 and own['last_move_x'] != 0:
  222. move_x = own['last_move_x'] * damping
  223. if move_y == 0 and own['last_move_y'] != 0:
  224. move_y = own['last_move_y'] * damping
  225. if move_z == 0 and own['last_move_z'] != 0:
  226. move_z = own['last_move_z'] * damping
  227. if rot_x == 0 and own['last_rot_x'] != 0:
  228. rot_x = own['last_rot_x'] * damping
  229. if rot_y == 0 and own['last_rot_y'] != 0:
  230. rot_y = own['last_rot_y'] * damping
  231. move = [move_y, 0, move_x]
  232. freecam.applyMovement(move, True)
  233. freecam.applyMovement([0, 0, move_z], False)
  234. freecam.applyRotation([rot_x, 0, 0], True)
  235. freecam.applyRotation([0, 0, rot_y], False)
  236. ################
  237. multer = .02
  238. if dict['walk'] == 1 and not controlcube['ragdoll_active']:
  239. if dict['cur_ccH_targetHeight'] < dict['cam_walk_height']:
  240. dist = dict['cam_walk_height'] - dict['cur_ccH_targetHeight']
  241. dict['cur_ccH_targetHeight'] = dict['cur_ccH_targetHeight'] + (dist * multer)
  242. if dict['cur_ccH_targetHeight'] > dict['cam_walk_height']:
  243. dict['cur_ccH_targetHeight'] = dict['cam_walk_height']
  244. cam.min = dict['cam_walk_min']
  245. cam.max = dict['cam_walk_max']
  246. else:
  247. if dict['cur_ccH_targetHeight'] < dict['cch_targetHeight']:
  248. dist = dict['cur_ccH_targetHeight'] - dict['cch_targetHeight']
  249. dict['cur_ccH_targetHeight'] = dict['cur_ccH_targetHeight'] - (dist * multer)
  250. if dict['cur_ccH_targetHeight'] > dict['cch_targetHeight'] - .001:
  251. dict['cur_ccH_targetHeight'] = dict['cch_targetHeight']
  252. if dict['cur_ccH_targetHeight'] > dict['cch_targetHeight']:
  253. dist = dict['cch_targetHeight'] - dict['cur_ccH_targetHeight']
  254. dict['cur_ccH_targetHeight'] = dict['cur_ccH_targetHeight'] + (dist * multer)
  255. if dict['cur_ccH_targetHeight'] < dict['cch_targetHeight'] + .001:
  256. dict['cur_ccH_targetHeight'] = dict['cch_targetHeight']
  257. ccH_targetHeight = dict['cur_ccH_targetHeight']
  258. ccH = camCube.worldPosition.z
  259. pH = camCube.parent.worldPosition.z
  260. ccheight = (round((ccH - pH), 2) - .4)
  261. localPos = camCube.localPosition.z
  262. if localPos != ccH_targetHeight:
  263. num = ccH_targetHeight - localPos
  264. camCube.localPosition.z += num
  265. #if camHeightSet not in own:
  266. #if 1 == 1:
  267. num = ccH_targetHeight - ccheight
  268. #else:
  269. #own['camHeightSet'] = True
  270. try:
  271. if dict['npause'] == False and controlcube['ragdoll_active'] == False:
  272. cont.activate(own.actuators['Camera'])
  273. controlcube['camera'] = 0
  274. if controlcube['ragdoll_active'] == True:
  275. #print('ragdoll_camera', own.actuators['Camera'].object)
  276. own.actuators['Camera'].object = scene.objects['ragdoll_parent']
  277. cont.activate(own.actuators['Camera'])
  278. controlcube['camera'] = 0
  279. except:
  280. pass
  281. def set_lens_dist():
  282. cur_lens = cam1.lens
  283. cur_min = cam.min
  284. cur_max = cam.max
  285. inc = .025
  286. if walk == 1:
  287. #lens
  288. if dict['walk_focal_length'] > cur_lens:
  289. new_lens = cur_lens + inc
  290. else:
  291. new_lens = cur_lens - inc
  292. if cur_lens > (dict['walk_focal_length'] - .1) and cur_lens < (dict['walk_focal_length'] + .1):
  293. new_lens = dict['walk_focal_length']
  294. #distance
  295. inc = .025
  296. if cur_min > (dict['cam_walk_min'] - inc):
  297. new_min = cur_min - inc
  298. if cur_min < (dict['cam_walk_min'] + inc):
  299. new_min = cur_min + inc
  300. if cur_max > (dict['cam_walk_max'] - inc):
  301. new_max = cur_min - inc
  302. if cur_max < (dict['cam_walk_max'] + inc):
  303. new_max = cur_min + inc
  304. else:
  305. #lens
  306. if dict['focal_length'] > cur_lens:
  307. new_lens = cur_lens + inc
  308. else:
  309. new_lens = cur_lens - inc
  310. if cur_lens > (dict['focal_length'] - .1) and cur_lens < (dict['focal_length'] + .1):
  311. new_lens = dict['focal_length']
  312. #distance
  313. inc = .025
  314. if cur_min > (dict['cam_min'] - inc):
  315. new_min = cur_min - inc
  316. if cur_min < (dict['cam_min'] + inc):
  317. new_min = cur_min + inc
  318. if cur_max > (dict['cam_max'] - inc):
  319. new_max = cur_min - inc
  320. if cur_max < (dict['cam_max'] + inc):
  321. new_max = cur_min + inc
  322. focallength = new_lens
  323. cam1.lens = focallength
  324. cam2.lens = focallength
  325. cam3.lens = focallength
  326. try:
  327. cam.min = new_min
  328. cam.max = new_max
  329. except:
  330. pass
  331. #print(own.actuators['Camera'].object)
  332. set_lens_dist()
  333. get_cam_state()
  334. camFSM.main(cont)
  335. birds.main(cont, scene)
  336. sound_man.main(cont)
  337. #own['NpcPedFSM'].Execute()
  338. if own['life'] % 2 == 1:
  339. cars.Execute(cont)
  340. own['CamFSM'].Execute()
  341. own['life'] += 1