heroofhyla

CowboyVania Player Control Script

Apr 10th, 2019
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.32 KB | None | 0 0
  1. extends KinematicBody2D
  2.  
  3. # Big messy class that manages the player character.
  4. export var MAX_WALK_SPEED = 100
  5. export var WALK_ACCELERATION = 600
  6. export var JUMP_SPEED = 200
  7. export var GRAVITY = 300
  8. export var hp = 12
  9. export var max_hp = 12
  10. var BULLET = load("res://Entities/Bullet.tscn")
  11. var DYNAMITE = load("res://Entities/Dynamite.tscn")
  12. var input = Vector2()
  13. var velocity = Vector2()
  14. var up = Vector2(0,-1)
  15. var on_floor = false
  16. var facing = 1
  17. var state = "stand"
  18. var old_facing = 1
  19. var old_state = "stand"
  20. var next_state = null
  21. var i = 1
  22. var has_gun = false
  23. var has_high_jump = false
  24. var has_dynamite = false
  25. var has_key = false
  26. onready var HEARTS = get_node("/root/Root/UI Layer/Hearts")
  27. var animations = {
  28.     "stand":{
  29.         -1: "StandLeft",
  30.         1: "StandRight"
  31.     },
  32.     "punch":{
  33.         -1:"PunchLeft",
  34.         1: "PunchRight"
  35.     },
  36.     "run":{
  37.         -1:"RunLeft",
  38.         1:"RunRight"
  39.     },
  40.     "jump":{
  41.         -1:"JumpLeft",
  42.         1:"JumpRight"
  43.     },
  44.     "shoot":{
  45.         -1: "ShootLeft",
  46.         1: "ShootRight"
  47.     },
  48.     "recoil":{
  49.         -1: "RecoilLeft",
  50.         1: "RecoilRight"
  51.     },
  52.     "dynamite":{
  53.         -1: "StandLeft",
  54.         1: "StandRight"
  55.     }
  56. }
  57.  
  58. func acquire(powerup):
  59.     if powerup == "dynamite":
  60.         has_dynamite = true
  61.     elif powerup == "revolver":
  62.         has_gun = true
  63.     elif powerup == "max_hp":
  64.         max_hp += 1
  65.         set_hp(max_hp)
  66.     elif powerup == "key":
  67.         has_key = true
  68.     elif powerup == "hp":
  69.         set_hp(min(hp + 1, max_hp))
  70. func _ready():
  71.     pass
  72.  
  73. func can_shoot():
  74.     return has_gun and can_punch()
  75.    
  76. func can_punch():
  77.     return (state == "stand"
  78.         or  state == "run"
  79.         or  state == "jump")
  80.  
  81. func can_dynamite():
  82.     return has_dynamite and (state == "stand"
  83.                          or  state == "run")
  84.  
  85. func can_move():
  86.     return (state == "stand"
  87.         or  state == "run"
  88.         or  state == "jump"
  89.         or  state == "punch" and not on_floor
  90.         or  state == "shoot" and not on_floor)
  91.  
  92. func can_interact():
  93.     return (state == "stand"
  94.         or  state == "run")
  95.  
  96. func fire():
  97.     var bullet = BULLET.instance()
  98.     bullet.position.x = self.position.x + facing * 7
  99.     bullet.position.y = self.position.y - 2
  100.     bullet.DIRECTION = self.facing
  101.     get_parent().add_child(bullet)
  102.  
  103. func dynamite():
  104.     var dynamite = DYNAMITE.instance()
  105.     dynamite.position = self.position
  106.     get_parent().add_child(dynamite)
  107.  
  108. func _physics_process(delta):
  109.     i += 1
  110.     i %= 100
  111.     input.x = 0
  112.     input.y = 0
  113.     old_state = state
  114.     old_facing = facing
  115.     if Input.is_action_pressed("move_left"):
  116.         if can_move():
  117.             input.x -= 1
  118.  
  119.         if on_floor and can_move():
  120.             state = "run"
  121.             facing = -1
  122.  
  123.     if Input.is_action_pressed("move_right"):
  124.         if can_move():
  125.             input.x = 1
  126.  
  127.         if on_floor and can_move():
  128.             state = "run"
  129.             facing = 1
  130.  
  131.     if input.x == 0 and state == "run":
  132.         state = "stand"
  133.  
  134.     if on_floor and state == "jump":
  135.         state = "stand"
  136.    
  137.     if (state == "run" or state == "stand") and not on_floor:
  138.         state = "jump"
  139.    
  140.     if Input.is_action_just_pressed("jump"):
  141.         if on_floor and can_move():
  142.             state = "jump"
  143.             if has_high_jump:
  144.                 velocity.y -= 1.5 * JUMP_SPEED
  145.             else:
  146.                 velocity.y -= JUMP_SPEED
  147.            
  148.             on_floor = false
  149.  
  150.     if Input.is_action_just_pressed("punch"):
  151.         if can_punch():
  152.             next_state = old_state
  153.             state = "punch"
  154.  
  155.     if Input.is_action_just_pressed("shoot"):
  156.         if can_shoot():
  157.             next_state = old_state
  158.             state = "shoot"
  159.    
  160.     if Input.is_action_just_released("jump"):
  161.         if velocity.y < - 50:
  162.             velocity.y /= 2
  163.    
  164.     if Input.is_action_just_pressed("dynamite"):
  165.         if can_dynamite():
  166.             state = "dynamite"
  167.             next_state = "stand"
  168.             dynamite()
  169.    
  170.     if Input.is_action_just_pressed("interact"):
  171.         if can_interact()
  172.             var areas = $ReceiveDamage.get_overlapping_areas()
  173.             for area in areas:
  174.                 if area.is_in_group("interactables"):
  175.                     area.interact()
  176.        
  177.     velocity.y += GRAVITY * delta
  178.     input = input.normalized()
  179.     velocity.x += input.x * WALK_ACCELERATION * delta
  180.  
  181.     if velocity.x < -MAX_WALK_SPEED:
  182.         velocity.x = -MAX_WALK_SPEED
  183.    
  184.     if velocity.x > MAX_WALK_SPEED:
  185.         velocity.x = MAX_WALK_SPEED
  186.    
  187.     if state != "run" and on_floor and state != "recoil" and  velocity.x != 0:
  188.         if velocity.x > 0:
  189.             velocity.x -= WALK_ACCELERATION * delta
  190.             if velocity.x < 0:
  191.                 velocity.x = 0
  192.         else:
  193.             velocity.x += WALK_ACCELERATION * delta
  194.             if velocity.x > 0:
  195.                 velocity.x = 0
  196.     velocity = move_and_slide(velocity, up)
  197.  
  198.     if state != "recoil" and $ReceiveDamage/CollisionShape2D.disabled:
  199.         $ReceiveDamage/CollisionShape2D.disabled = false
  200.     handle_states()
  201.  
  202.  
  203.     # Working around some weirdness with the built-in is_on_floor function.
  204.     if is_on_floor():
  205.         on_floor = true
  206.    
  207.     if velocity.y > 15:
  208.         on_floor = false
  209.  
  210. func handle_states():
  211.     if $AnimationPlayer.current_animation != animations[state][facing] and next_state and state == old_state:
  212.         state = next_state
  213.         next_state = null
  214.  
  215.     if state != old_state or facing != old_facing:
  216.         $AnimationPlayer.play(animations[state][facing])
  217.         if next_state:
  218.             $AnimationPlayer.queue(animations[next_state][facing])
  219.  
  220. func _on_ReceiveDamage_area_entered(area):
  221.     if area.is_in_group("damage_from_enemy"):
  222.         set_hp(hp - 1)
  223.         velocity.x = sign(position.x - area.get_parent().position.x) * 100
  224.         facing = int(sign(velocity.x)) * -1
  225.         state = "recoil"
  226.         next_state = "stand"
  227.         handle_states()
  228.  
  229. func set_hp(new_hp):
  230.     hp = new_hp
  231.     HEARTS.update()
  232.     if hp == 0:
  233.         game_over()
  234.    
  235. func game_over():
  236.     print("GAME OVER!")
  237.     get_tree().quit()
Advertisement
Add Comment
Please, Sign In to add comment