Skip to content

Instantly share code, notes, and snippets.

@brettchalupa
Created April 6, 2023 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brettchalupa/120e232aeb41914952745eff2a24fe80 to your computer and use it in GitHub Desktop.
Save brettchalupa/120e232aeb41914952745eff2a24fe80 to your computer and use it in GitHub Desktop.
Using Enums with Godot
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