Advertisement
WarCR

Untitled

Jan 28th, 2021
1,026
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends Node2D;
  2.  
  3. const SHIP_SPEED = 0.3; #Usually 1 should be slow enough but fsr it isnt
  4. const SHOT_COOLDOWN = 300; #The value has to be this big cuz it's pretty much measured in frames
  5. var bullet_id = preload("res://Scenes/Shot.tscn");
  6. var shot_timer = 0;
  7.  
  8. func _ready():
  9.     set_process(true);
  10.  
  11. func shoot(node):
  12.     var bullet_obj = bullet_id.instance();
  13.     bullet_obj.set_global_position(node.get_global_position());
  14.     get_owner().add_child(bullet_obj);
  15.  
  16. func _process(delta):
  17.     #Comment more often >:I
  18.     #(Don't over-do it tho, not everything needs it)
  19.     #Also eu escrevo tudo em ingles pra ser mais consistente mesmo (e pra praticar também)
  20.    
  21.     #Movement
  22.     var left_input = float(Input.is_action_pressed("left"));
  23.     var right_input = float(Input.is_action_pressed("right"));
  24.     var xdir = right_input - left_input;
  25.    
  26.     translate(Vector2(xdir * SHIP_SPEED,0));
  27.     position.x = clamp(position.x,50,640-50);
  28.    
  29.     #Shooting
  30.     if Input.is_action_pressed("shot"):
  31.         if shot_timer <= 0:
  32.             shoot(get_node("Pos_Gun1"));
  33.             shoot(get_node("Pos_Gun2"));
  34.             shot_timer = SHOT_COOLDOWN;
  35.    
  36.     shot_timer -= 1;
  37.     shot_timer = max(0,shot_timer); #Makes so shot_timer can't go below 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement