Guest User

Godot Tutorials | Basics Of Coding | Godot Basics Tutorial | Ep 26: Homework

a guest
Apr 6th, 2021
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class_name Hero
  2.  
  3. extends Sprite
  4.  
  5.  
  6. """
  7. This file is under an MIT License. (Never put metadata like here in a script!)
  8. This script controls the position of the hero in relation to some screen 'anchors'.
  9. Choose the position you want your hero to be at on the game window in the inspector.
  10. Note: Modifying this position at runtime is unsupported. This can be done easily using
  11. inputs for example.
  12. """
  13.  
  14. ################################## VARIABLES ##################################
  15.  
  16. enum HERO_SCREEN_POSITION {BOTTOM_CENTER = 0,
  17.     TOP_CENTER,
  18.     CENTER_MIDDLE,
  19.     LEFT_CENTER,
  20.     RIGHT_CENTER
  21.     }
  22.  
  23. export (HERO_SCREEN_POSITION) var current_hero_position
  24.  
  25.  
  26. onready var game_screen_width: int = OS.window_size.x
  27. onready var game_screen_height: int = OS.window_size.y
  28.  
  29.  
  30. onready var hero_texture : StreamTexture = self.texture
  31. onready var hero_texture_width: int = hero_texture.get_width()
  32. onready var hero_texture_height: int = hero_texture.get_height()
  33.  
  34.  
  35. # Offset to prevent the hero being off-screen
  36. onready var hero_width_offset: int = hero_texture_width / 2
  37. onready var hero_height_offset: int = hero_texture_height / 2
  38.  
  39. ################################# RUN CODE #################################
  40.  
  41.  
  42. func _physics_process(_delta: float) -> void:
  43.     self.reposition_hero_texture()
  44.  
  45.  
  46. ################################# FUNCTIONS ################################
  47.  
  48.  
  49. func reposition_hero_texture() -> void:
  50.     match self.current_hero_position:
  51.         HERO_SCREEN_POSITION.BOTTOM_CENTER:
  52.             self.position.x = OS.window_size.x / 2
  53.             self.position.y = OS.window_size.y - (hero_height_offset)
  54.            
  55.         HERO_SCREEN_POSITION.TOP_CENTER:
  56.             self.position.x = OS.window_size.x / 2
  57.             self.position.y = 0 + (hero_height_offset)
  58.            
  59.         HERO_SCREEN_POSITION.CENTER_MIDDLE:
  60.             self.position.x = OS.window_size.x / 2
  61.             self.position.y = OS.window_size.y / 2
  62.            
  63.         HERO_SCREEN_POSITION.LEFT_CENTER:
  64.             self.position.x = 0 + (hero_width_offset)
  65.             self.position.y = OS.window_size.y / 2
  66.            
  67.         HERO_SCREEN_POSITION.RIGHT_CENTER:
  68.             self.position.x = OS.window_size.x - (hero_width_offset)
  69.             self.position.y = OS.window_size.y / 2
  70.         _:
  71.             printerr("(!) ERROR: reposition_texture() in node ", self.name, " :")
  72.             printerr("Unknown hero screeen position. Please select a valid one!")
  73.  
Advertisement
Add Comment
Please, Sign In to add comment