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

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