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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. #zskcam
  2. import bge
  3. import mathutils
  4. from mathutils import *
  5. scene = bge.logic.getCurrentScene()
  6. cont = bge.logic.getCurrentController()
  7. own = cont.owner
  8. def main():
  9. dict = bge.logic.globalDict
  10. camempty = scene.objects['camEmpty.001']
  11. controlcube = scene.objects['control_cube.002']
  12. camCube = scene.objects['camCube']
  13. dropinCol = controlcube.sensors['dropinCol']
  14. LAST_GRIND = controlcube['LAST_GRIND']
  15. up = cont.sensors['up']
  16. ray = cont.sensors['up']
  17. down = cont.sensors['down']
  18. left = cont.sensors['left']
  19. right = cont.sensors['right']
  20. distance = 0.12
  21. cam = cont.actuators["Camera"]
  22. near = cont.sensors["Near"]
  23. move = own.actuators["move"]
  24. #match camera actuaotor
  25. cam_def_height = dict['cam_height'] #-.1 #-.6
  26. #cam_def_min = 1.6
  27. #cam_def_max = 2.4
  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. acam = scene.active_camera
  62. #if 1 == 1:
  63. #if own['playback'] == False:
  64. if controlcube['driving'] == False:
  65. if down.triggered == True and LAST_GRIND == False and walk == 0:
  66. hitPosition = down.hitPosition
  67. distance = own.getDistanceTo(hitPosition)
  68. cam_moved = 1
  69. if distance < .2:
  70. camempty['hitdown'] = True
  71. cam.height = cam_height + .3
  72. cam.damping = .0
  73. #print("raise cam")
  74. if zdist < -.2:
  75. cam.height = lastCamheight + .01
  76. cam.damping = .0
  77. raised = 1
  78. if (distance > .4 and distance < .6) and zdist > -.2 and raised == 0:
  79. camheight2 = cam.height - .001
  80. if cam.height < .2:
  81. cam.height = camheight2
  82. #print("slow lower")
  83. if distance >= .6 and zdist > -.2 and raised == 0:
  84. cam.height = cam.height - .02
  85. #print("slow lower2")
  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 + .06) and zdist > -.2 and raised == 0:
  89. cam.height = cam_height - .02
  90. if cam_height < -.6 and cam_moved == 0 and LAST_GRIND == False and zdist > -.2 and raised == 0 and walk == 0:
  91. cam_height = .1
  92. if LAST_GRIND == True and walk == 0:
  93. if cam.height < -.5 or zdist < -.2:
  94. cam.height = cam.height + .013
  95. if cam.height >= -.5 and not down.triggered:
  96. pass
  97. if walk == 1:
  98. if dropinCol.positive == True and lasty == True:
  99. pass
  100. else:
  101. pass
  102. controlcube['lastCamheight'] = cam.height
  103. #########
  104. #cam.min = dict['cam_min']
  105. #cam.max = dict['cam_max']
  106. cont.activate(own.actuators['Camera'])
  107. ########
  108. if near.triggered == True and walk == 0:
  109. #print("near")
  110. if cam.min < 1:
  111. cam.min = cam.min + .1
  112. cam.max = cam.max + .1
  113. cam.height = cam_height + .01
  114. cam.damping = .001
  115. cont.activate(move)
  116. else:
  117. #print("far")
  118. cont.deactivate(move)
  119. if cam.min > cam_def_min:
  120. cam.min = cam.min - .05
  121. cam.max = cam.max - .05
  122. cam.damping = .0
  123. if cam.damping != 0 and down.triggered == False and near.triggered == False:
  124. cam.damping = 0.91
  125. obj = cont.owner
  126. cube = controlcube
  127. to = cube
  128. from2 = obj
  129. distance = 0.0
  130. property = ""
  131. face = 1
  132. xray = 0
  133. poly = 0
  134. hit = obj.rayCast( to, from2, distance, property, face, xray, poly)
  135. control = "control_cube.002"
  136. hitobj = hit[0]
  137. hitobj = str(hitobj)
  138. if hitobj != control and walk == 0:
  139. cam.damping = .0
  140. if cam.height < 3:
  141. cam.height = cam_height + .1
  142. if dict['menu_idle_timer'] > 300:
  143. move_len = 2048
  144. if own['idlecampos_x'] < move_len:
  145. own['idlecampos_x'] += 1
  146. if own['idlecampos_x'] == move_len:
  147. own['idlecampos_x'] = 0
  148. if own['idlecampos_x'] < (move_len / 2):
  149. move = [.0001, 0, 0]
  150. freecam.applyMovement( move, True)
  151. freecam.applyRotation([.0001, 0, .0001],True)
  152. if own['idlecampos_x'] > (move_len / 2):
  153. move = [-.0001, 0, 0]
  154. freecam.applyMovement( move, True)
  155. freecam.applyRotation([-.0001, 0, -.0001],True)
  156. if dict['npause'] == True:
  157. cont.deactivate(own.actuators['Camera'])
  158. controlcube['camera'] = 2
  159. if controlcube['camera'] == 2 and dict['joy_con'] == 1:
  160. cont.deactivate(own.actuators['Camera'])
  161. scene.active_camera = freecam
  162. cont.activate(cube.actuators['freecam'])
  163. #print('activating camera', freecam)
  164. #####
  165. #get value 0-100
  166. lLR = dict['lLR'] / .082 * 100
  167. lUD = dict['lUD'] / .082 * 100 - 20 / 80
  168. rLR = dict['rLR'] / .082 * 100 - 20 / 80
  169. rUD = dict['rUD'] / .082 * 100 - 20 / 80
  170. lTrig = dict['lTrig'] / .082 * 100 - 20 / 80
  171. rTrig = dict['rTrig'] / .082 * 100 - 20 / 80
  172. # lBump = dict['lBump']
  173. # rBump = dict['rBump']
  174. if lLR < -20:
  175. lmLR = round((lLR + 20) / 80 * 100, 0)
  176. elif lLR > 20:
  177. lmLR = round((lLR - 20) / 80 * 100, 0)
  178. else: lmLR = 0
  179. if lUD > 20:
  180. lmUD = round((lUD - 20) / 80 * 100, 0)
  181. elif lUD < -20:
  182. lmUD = round((lUD + 20) / 80 * 100, 0)
  183. else: lmUD = 0
  184. if rLR < -20:
  185. rmLR = round((rLR + 20) / 80 * 100, 0)
  186. elif rLR > 20:
  187. rmLR = round((rLR - 20) / 80 * 100, 0)
  188. else: rmLR = 0
  189. if rUD > 20:
  190. rmUD = round((rUD - 20) / 80 * 100, 0)
  191. elif rUD < -20:
  192. rmUD = round((rUD + 20) / 80 * 100, 0)
  193. else: rmUD = 0
  194. if lTrig > 3:
  195. mTrig = lTrig * -1
  196. elif rTrig > 3:
  197. mTrig = rTrig
  198. else: mTrig = 0
  199. #move camera
  200. damping = .95
  201. damping2 = 1.005
  202. mult = .0005 * own['speed_mult']
  203. move_x = lmUD * mult
  204. move_y = lmLR * mult
  205. move_z = (mTrig * -mult) / 2#4
  206. rot_mult = -.00015 * own['speed_mult']
  207. rot_x = rmUD * rot_mult
  208. rot_y = rmLR * rot_mult
  209. if move_x == 0 and own['last_move_x'] != 0:
  210. move_x = own['last_move_x'] * damping
  211. if move_y == 0 and own['last_move_y'] != 0:
  212. move_y = own['last_move_y'] * damping
  213. if move_z == 0 and own['last_move_z'] != 0:
  214. move_z = own['last_move_z'] * damping
  215. if rot_x == 0 and own['last_rot_x'] != 0:
  216. rot_x = own['last_rot_x'] * damping
  217. if rot_y == 0 and own['last_rot_y'] != 0:
  218. rot_y = own['last_rot_y'] * damping
  219. move = [move_y, 0, move_x]
  220. freecam.applyMovement( move, True)
  221. freecam.applyMovement([0, 0, move_z], False)
  222. freecam.applyRotation([rot_x, 0, 0],True)
  223. freecam.applyRotation([0, 0, rot_y],False)
  224. ################
  225. #print(dict['walk'], 'walk')
  226. multer = .02
  227. if dict['walk'] == 1:
  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. #ccH_targetHeight = .9
  236. else:
  237. if dict['cur_ccH_targetHeight'] < dict['cch_targetHeight']:
  238. dist = dict['cur_ccH_targetHeight'] - dict['cch_targetHeight']
  239. dict['cur_ccH_targetHeight'] = dict['cur_ccH_targetHeight'] - (dist * multer)
  240. if dict['cur_ccH_targetHeight'] > dict['cch_targetHeight'] - .001:
  241. dict['cur_ccH_targetHeight'] = dict['cch_targetHeight']
  242. if dict['cur_ccH_targetHeight'] > dict['cch_targetHeight']:
  243. dist = dict['cch_targetHeight'] - dict['cur_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. ccH_targetHeight = dict['cur_ccH_targetHeight']
  248. #print(dict['cur_ccH_targetHeight'])
  249. #dict['cur_ccH_targetHeight'] =
  250. #ccH_targetHeight = .3
  251. ccH = camCube.worldPosition.z
  252. pH = camCube.parent.worldPosition.z
  253. ccheight = (round((ccH - pH), 2) - .4)
  254. #print(ccheight, 'camera target height')
  255. localPos = camCube.localPosition.z
  256. #print(localPos, 'local position')
  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. if dict['npause'] == False:
  266. cont.activate(own.actuators['Camera'])
  267. controlcube['camera'] = 0
  268. cur_lens = cam1.lens
  269. cur_min = cam.min
  270. cur_max = cam.max
  271. #print(cur_lens)
  272. inc = .025
  273. if walk == 1:
  274. #lens
  275. if dict['walk_focal_length'] > cur_lens:
  276. new_lens = cur_lens + inc
  277. else:
  278. new_lens = cur_lens - inc
  279. if cur_lens > (dict['walk_focal_length'] - .1) and cur_lens < (dict['walk_focal_length'] + .1):
  280. new_lens = dict['walk_focal_length']
  281. #distance
  282. inc = .025
  283. if cur_min > (dict['cam_walk_min'] - inc):
  284. new_min = cur_min - inc
  285. if cur_min < (dict['cam_walk_min'] + inc):
  286. new_min = cur_min + inc
  287. if cur_max > (dict['cam_walk_max'] - inc):
  288. new_max = cur_min - inc
  289. if cur_max < (dict['cam_walk_max'] + inc):
  290. new_max = cur_min + inc
  291. else:
  292. #lens
  293. if dict['focal_length'] > cur_lens:
  294. new_lens = cur_lens + inc
  295. else:
  296. new_lens = cur_lens - inc
  297. if cur_lens > (dict['focal_length'] - .1) and cur_lens < (dict['focal_length'] + .1):
  298. new_lens = dict['focal_length']
  299. #distance
  300. inc = .025
  301. if cur_min > (dict['cam_min'] - inc):
  302. new_min = cur_min - inc
  303. if cur_min < (dict['cam_min'] + inc):
  304. new_min = cur_min + inc
  305. if cur_max > (dict['cam_max'] - inc):
  306. new_max = cur_min - inc
  307. if cur_max < (dict['cam_max'] + inc):
  308. new_max = cur_min + inc
  309. focallength = new_lens
  310. cam1.lens = focallength
  311. cam2.lens = focallength
  312. cam3.lens = focallength
  313. try:
  314. cam.min = new_min
  315. cam.max = new_max
  316. #print(new_min, new_max)
  317. except:
  318. pass
  319. main()