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 12KB

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