Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class_name Hero
- extends Sprite
- """
- This file is under an MIT License. (Never put metadata like here in a script!)
- This script controls the position of the hero in relation to some screen 'anchors'.
- Choose the position you want your hero to be at on the game window in the inspector.
- Note: Modifying this position at runtime is unsupported. This can be done easily using
- inputs for example.
- """
- ################################## VARIABLES ##################################
- enum HERO_SCREEN_POSITION {BOTTOM_CENTER = 0,
- TOP_CENTER,
- CENTER_MIDDLE,
- LEFT_CENTER,
- RIGHT_CENTER
- }
- export (HERO_SCREEN_POSITION) var current_hero_position
- onready var game_screen_width: int = OS.window_size.x
- onready var game_screen_height: int = OS.window_size.y
- onready var hero_texture : StreamTexture = self.texture
- onready var hero_texture_width: int = hero_texture.get_width()
- onready var hero_texture_height: int = hero_texture.get_height()
- # Offset to prevent the hero being off-screen
- onready var hero_width_offset: int = hero_texture_width / 2
- onready var hero_height_offset: int = hero_texture_height / 2
- ################################# RUN CODE #################################
- func _physics_process(_delta: float) -> void:
- self.reposition_hero_texture()
- ################################# FUNCTIONS ################################
- func reposition_hero_texture() -> void:
- match self.current_hero_position:
- HERO_SCREEN_POSITION.BOTTOM_CENTER:
- self.position.x = OS.window_size.x / 2
- self.position.y = OS.window_size.y - (hero_height_offset)
- HERO_SCREEN_POSITION.TOP_CENTER:
- self.position.x = OS.window_size.x / 2
- self.position.y = 0 + (hero_height_offset)
- HERO_SCREEN_POSITION.CENTER_MIDDLE:
- self.position.x = OS.window_size.x / 2
- self.position.y = OS.window_size.y / 2
- HERO_SCREEN_POSITION.LEFT_CENTER:
- self.position.x = 0 + (hero_width_offset)
- self.position.y = OS.window_size.y / 2
- HERO_SCREEN_POSITION.RIGHT_CENTER:
- self.position.x = OS.window_size.x - (hero_width_offset)
- self.position.y = OS.window_size.y / 2
- _:
- printerr("(!) ERROR: reposition_texture() in node ", self.name, " :")
- printerr("Unknown hero screeen position. Please select a valid one!")
Advertisement
Add Comment
Please, Sign In to add comment