extends Node2D
onready var root = get_tree().get_root()
onready var base_size = root.get_rect().size
func _ready():
OS.set_window_maximized(true)
get_tree().connect("screen_resized", self, "_on_screen_resized")
root.set_as_render_target(true)
root.set_render_target_update_mode(root.RENDER_TARGET_UPDATE_ALWAYS)
root.set_render_target_to_screen_rect(root.get_rect())
func _on_screen_resized():
var new_window_size = OS.get_window_size()
var scale_w = max(int(new_window_size.x / base_size.x), 1)
var scale_h = max(int(new_window_size.y / base_size.y), 1)
var scale = min(scale_w, scale_h)
var diff = new_window_size - (base_size * scale)
var diffhalf = (diff * 0.5).floor()
root.set_rect(Rect2(diffhalf, base_size))
root.set_render_target_to_screen_rect(Rect2(diffhalf, base_size * scale))
This is the code for my game's world scene.
extends KinematicBody2D
###GRAB THE PLAYER SPRITE###
onready var player = get_node("MegaMan")
onready var player_offset = player.get_offset()
onready var pos = player.get_global_pos()
onready var debug_fps = get_node("Debug/FPS")
onready var debug_xpos = get_node("Debug/XPos")
onready var debug_ypos = get_node("Debug/YPos")
onready var debug_offset = get_node("Debug/Offset")
onready var debug_anim = get_node("Debug/Anim")
onready var debug_xspd = get_node("Debug/XSpeed")
onready var debug_yspd = get_node("Debug/YSpeed")
###DEBUG MODE VARIABLES###
var debug_mode = 0
###SET PLAYER VARIABLES###
var player_pos = pos
var direction = 0
var last_dir = 0
var anim = "idle"
var speed = Vector2()
var velocity = Vector2()
var jump_count = 0
var jump_hold = false
var final_grav
var allow_move = false
var beam_speed = 8
var weapon = 0
var charge = 0
var shoot_frame = 0
const X_SPEED = 1.3
const SLIDE_SPEED = 2.5
const GRAVITY = 0.25
const JUMP_FORCE = -5.25
const MAX_YSPEED = 7
const MAX_JUMPS = 1
###SET PROCESSES TO TRUE###
func _ready():
set_process(true)
set_fixed_process(true)
set_process_input(true)
###HANDLE ONE TIME ANIMATIONS###
func _on_anim_finished():
if anim == "teleport" and allow_move == false:
allow_move = true
anim = "idle"
player.set_flip_h(true)
player.play(anim)
if anim == "lilstep" and direction != 0:
anim = "run"
player.play(anim)
if anim == "lilstep" and direction == 0:
anim = "idle"
player.play(anim)
###ONE TIME EVENTS###
func _input(event):
if allow_move == true:
if jump_count < MAX_JUMPS and event.is_action_pressed("jump"):
jump_hold = true
speed.y = JUMP_FORCE
jump_count += 1
anim = "jump"
player.play(anim)
func _process(delta):
debug_fps.set_text(str(OS.get_frames_per_second()))
debug_offset.set_text(str(player_offset))
debug_xpos.set_text(str(pos.x))
debug_ypos.set_text(str(pos.y))
debug_anim.set_text(str(anim))
debug_xspd.set_text(str(speed.x))
debug_yspd.set_text(str(speed.y))
###HANDLE PLAYER INPUT AND MOVEMENT###
func _fixed_process(delta):
###TELEPORT THE PLAYER IN###
if allow_move == false and player_offset.y < 0:
player_offset.y += 8
player.edit_set_pivot(player_offset)
if allow_move == false and player_offset.y == 0:
anim = "teleport"
player.play(anim)
if allow_move == true:
###SET THE DIRECTION OF THE PLAYER###
if Input.is_action_pressed("move_left"):
direction = -1
player.set_flip_h(false)
elif Input.is_action_pressed("move_right"):
direction = 1
player.set_flip_h(true)
else:
direction = 0
###ACTIVATE THE LITTLE STEP###
if anim == "idle" and direction == -1 and last_dir != direction:
anim = "lilstep"
last_dir = direction
move(Vector2(-1, 0))
player.play(anim)
elif anim == "idle" and direction == 1 and last_dir != direction:
anim = "lilstep"
last_dir = direction
move(Vector2(1, 0))
player.play(anim)
elif anim == "run" and direction == -1 and last_dir != direction:
anim = "lilstep"
last_dir = direction
move(Vector2(-1, 0))
player.play(anim)
elif anim == "run" and direction == 1 and last_dir != direction:
anim = "lilstep"
last_dir = direction
move(Vector2(1, 0))
player.play(anim)
elif anim == "lilstep" and direction == -1 and last_dir != direction:
anim = "lilstep"
last_dir = direction
move(Vector2(-1, 0))
player.play(anim)
elif anim == "lilstep" and direction == 1 and last_dir != direction:
anim = "lilstep"
last_dir = direction
move(Vector2(1, 0))
player.play(anim)
###RESET THE DIRECTION VALUE###
if direction == 0 and last_dir != direction:
last_dir = 0
###CHECK TO SEE IF JUMP IS BEING HELD###
if Input.is_action_pressed("jump"):
jump_hold = true
else:
jump_hold = false
###SET PLAYER RUNNING SPEED###
if direction == -1 and anim != "lilstep":
speed.x = -X_SPEED
elif direction == 1 and anim != "lilstep":
speed.x = X_SPEED
else:
speed.x = 0
###SET JUMP VELOCITY###
speed.y += GRAVITY
if speed.y > MAX_YSPEED:
speed.y = MAX_YSPEED
###IF PLAYER RELEASED JUMP, START TO FALL###
if jump_hold == false and speed.y < -1:
speed.y = (speed.y - int(speed.y))
###MOVE THE PLAYER###
velocity = Vector2(speed.x, speed.y)
var move_remainder = move(velocity)
var colliding = is_colliding()
if is_colliding():
var normal = get_collision_normal()
var final_move = normal.slide(move_remainder)
speed = normal.slide(speed)
move(final_move)
if normal == Vector2(0, -1) and direction != 0 and anim == "jump":
jump_count = 0
anim = "run"
player.play(anim)
elif normal == Vector2(0, -1) and direction == 0 and anim == "jump":
jump_count = 0
anim = "idle"
player.play(anim)
###HANDLE ANIMATION CHANGES###
if allow_move == true and direction == 0 and anim == "run":
anim = "lilstep"
player.play(anim)
if allow_move == true and colliding == false and anim != "jump":
jump_count = MAX_JUMPS
anim = "jump"
player.play(anim)
This is from my player script.
extends TextureFrame
###PAUSE SCREEN STATE###
var pause_state = 0
func _ready():
set_process_input(true)
func _input(event):
###ACTIVATE/DEACTIVATE PAUSE###
if event.is_action_pressed("ui_accept"):
pause_state +=1
###SHOW/HIDE PAUSE MENU###
if pause_state == 1:
get_tree().set_pause(true)
show()
else:
get_tree().set_pause(false)
hide()
if pause_state > 1:
pause_state = 0
And finally, the start of the pause menu functions.