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

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