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

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