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

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