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

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