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

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