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

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