Created
April 6, 2023 14:13
-
-
Save brettchalupa/120e232aeb41914952745eff2a24fe80 to your computer and use it in GitHub Desktop.
Using Enums with Godot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends CharacterBody2D | |
class_name Player | |
@export var weapon:PackedScene | |
const BULLET_SPEED = 400 | |
@export var fire_type := FireType.SINGLE | |
enum FireType { | |
SINGLE, | |
DOUBLE, | |
} | |
func _input(event: InputEvent) -> void: | |
if event.is_action_pressed("ui_accept"): | |
if fire_type == FireType.SINGLE: | |
fire_bullet() | |
elif fire_type == FireType.DOUBLE: | |
fire_bullet(Vector2(20, 0)) | |
fire_bullet(Vector2(-20, 0)) | |
else: | |
print_debug("player fire_type not recognized: %s" % fire_type) | |
func fire_bullet(offset:Vector2 = Vector2.ZERO) -> void: | |
var bullet = weapon.instantiate() | |
bullet.global_position = global_position + offset | |
get_tree().get_root().add_child(bullet) | |
bullet.linear_velocity = Vector2.UP.rotated(rotation) * BULLET_SPEED |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment