Skip to content

Instantly share code, notes, and snippets.

@jamesmcm
Created October 13, 2024 09:06
Show Gist options
  • Save jamesmcm/250f0622b9327c8b94b2d7b8115a092d to your computer and use it in GitHub Desktop.
Save jamesmcm/250f0622b9327c8b94b2d7b8115a092d to your computer and use it in GitHub Desktop.
GDScript examples
extends Camera2D
# Camera that reacts to window resizing
@export var acceleration: float = 500
@export var max_speed: float = 1000
var resolution: Vector2 = Vector2(0, 0)
var velocity: Vector2 = Vector2(0, 0)
func _ready():
get_tree().get_root().size_changed.connect(resize)
resolution = get_viewport().get_visible_rect().size
$BottomBar.position = Vector2(resolution.x / 3.0, resolution.y / 3.0)
global_position = Vector2(0, 0)
func _physics_process(delta):
# TODO: Smoothing on panning
if %BattleManager.state == %BattleManager.BattleState.PLAYER and not %BattleManager.animating:
velocity = Vector2(Input.get_axis("ui_left", "ui_right"), Input.get_axis("ui_up", "ui_down")) * max_speed
global_position += velocity * delta
if global_position.y > -resolution.y / 3.0: # 3.0 is camera scale
global_position.y = -resolution.y / 3.0
if global_position.x < 0:
global_position.x = 0
# TODO: Add restrictions from map size
%CameraPos.text = str(global_position)
func resize():
# When becoming smaller, keep the bottom left in the same place
# When becoming bigger, keep the bottom right in the same place
var new_resolution = get_viewport().get_visible_rect().size
if new_resolution.y < resolution.y and new_resolution.x == 1920:
global_position.y = new_resolution.y - resolution.y
resolution = new_resolution
$BottomBar.position = Vector2(resolution.x / 3.0, resolution.y / 3.0)
extends Resource
class_name BattleCharacterFactory
# Example of instantiating a PackedScene as a specific node type in type-checked code
# Note the root of the battle_character_scene must be a BattleCharacter node
# (you need to change the root of the scene when first creating it)
static func create_character(character_data: CharacterData) -> BattleCharacter:
var battle_character_scene = load("res://battle/character/BattleCharacter.tscn")
var baseclass = AutoloadCharacterManager.get_base_class(character_data.player_class)
baseclass.name = "BaseClass"
var character = battle_character_scene.instantiate()
character.character_data = character_data
character.base_class = baseclass
character.add_child(baseclass)
character.y_sort_enabled = true
character.battle_character_controller = character_data.controller
character.update_status_bars()
return character
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment