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

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