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

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