Advertisement
AceScottie

player.gd

Feb 18th, 2020
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.62 KB | None | 0 0
  1. extends KinematicBody
  2.  
  3.  
  4. const DECAY = 0.5
  5. const SDECAY = 1
  6. const ACC = 1
  7. const SACC = 3
  8. const LIMIT = 10
  9. const SLIMIT = 30
  10. var gravity = Vector3.DOWN * 25
  11. var jumpspeed = 10
  12. var sjumpspeed = 20
  13. var jump = 0
  14. var spin = 0.1
  15. var xacc = 0
  16. var zacc = 0
  17. var yacc = 0
  18. var sprint = false
  19. var motion = Vector3(0, 0, 0)
  20. var camera = null
  21.  
  22. func _ready():
  23.     camera = get_tree().get_root().get_camera()
  24.  
  25. func _physics_process(delta):
  26.     motion += gravity * delta
  27.     get_input()
  28.     motion = move_and_slide(motion, Vector3.UP)
  29.     run_decay()
  30.     if jump == 1  and (is_on_floor() or get_wall_jump()):
  31.         motion.y = jumpspeed
  32.     elif jump == 2  and (is_on_floor() or get_wall_jump()):
  33.         motion.y = sjumpspeed
  34.     var bodies = get_slide_collision(1)
  35.  
  36.    
  37. func get_input():
  38.     var vy = motion.y
  39.     motion = Vector3()
  40.     jump = 0
  41.     if Input.is_action_just_pressed("ui_accept"):
  42.         if Input.is_key_pressed(KEY_SHIFT):
  43.             jump = 2
  44.         else:
  45.             jump = 1
  46.     if Input.is_key_pressed(KEY_W):
  47.         if Input.is_key_pressed(KEY_SHIFT) and is_on_floor():
  48.             sprint = true
  49.             if xacc < SLIMIT and xacc > -SLIMIT:
  50.                 xacc-= SACC
  51.         else:
  52.             sprint = false
  53.             if xacc < LIMIT and xacc > -LIMIT:
  54.                 xacc-=ACC
  55.     elif Input.is_key_pressed(KEY_S):
  56.         if Input.is_key_pressed(KEY_SHIFT) and is_on_floor():
  57.             sprint = true
  58.             if xacc < SLIMIT and xacc > -SLIMIT:
  59.                 xacc+= SACC
  60.         else:
  61.             sprint = false
  62.             if xacc < LIMIT and xacc > -LIMIT:
  63.                 xacc+=ACC
  64.     if Input.is_key_pressed(KEY_D):
  65.         if Input.is_key_pressed(KEY_SHIFT) and is_on_floor():
  66.             sprint = true
  67.             if zacc < SLIMIT and zacc > -SLIMIT:
  68.                 zacc+= SACC
  69.         else:
  70.             sprint = false
  71.             if zacc < LIMIT and zacc > -LIMIT:
  72.                 zacc += ACC
  73.        
  74.     elif Input.is_key_pressed(KEY_A):
  75.         if Input.is_key_pressed(KEY_SHIFT) and is_on_floor():
  76.             sprint = true
  77.             if zacc < SLIMIT and zacc > -SLIMIT:
  78.                 zacc-= SACC
  79.         else:
  80.             sprint = false
  81.             if zacc < LIMIT and zacc > -LIMIT:
  82.                 zacc-= ACC
  83.    
  84.    
  85.     motion.y = vy
  86.     motion += transform.basis.x * (0-zacc)
  87.     motion += transform.basis.z * (0-xacc)
  88.  
  89. func _unhandled_input(event):
  90.     if event is InputEventMouseMotion:
  91.         rotate_y(-lerp(0, spin, event.relative.x/10))
  92.         if camera != null:
  93.             camera.rotate_x(lerp(0, spin, event.relative.y/50))
  94. func get_wall_jump():
  95.     var bodies = get_slide_collision(1)
  96.     print(bodies == null)
  97.     if bodies != null:
  98.         if bodies.collider.name != null and not is_on_floor():
  99.             return true
  100.         else:
  101.             return false
  102.     else:
  103.         return false
  104. func run_decay():
  105.     var d = 0
  106.     if sprint:
  107.         d = SDECAY
  108.     else:
  109.         d = DECAY
  110.     if xacc < 0:
  111.         xacc += d
  112.     elif xacc > 0:
  113.         xacc -= d
  114.     if zacc < 0:
  115.         zacc += d
  116.     elif zacc > 0:
  117.         zacc -= d
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement