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

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