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

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