161 lines
5.7 KiB
GDScript
161 lines
5.7 KiB
GDScript
extends Position2D
|
|
|
|
export(String) var LINE_COLOR_HEX = '#FFEB3B'
|
|
|
|
var grid_position = Vector2()
|
|
var grid_size = Vector2()
|
|
|
|
onready var parent = PlayerInfo
|
|
onready var timer: Timer = $Timer
|
|
#onready var parent = $Player
|
|
|
|
##TODO: Quick and dirty because we need to pause these
|
|
onready var player_node :KinematicBody2D = $"../Player"
|
|
#onready var level_node = $"../Level"
|
|
|
|
func _ready():
|
|
# If you drag the camera from the OffsetPivot node,
|
|
# its position will not be (0, 0)
|
|
#$Camera2D.global_position = Vector2(160,90) # when doing grid snapping
|
|
#$Left.position = Vector2(160,90)
|
|
#$Camera2D.position = Vector2(0,-4)
|
|
#grid_size = OS.get_screen_size()
|
|
grid_size = get_node("/root").size
|
|
#grid_size = get_node("/root").get_visible_rect().size
|
|
print(grid_size)
|
|
set_as_toplevel(true)
|
|
#update_grid_position()
|
|
##TODO: Can't seem to get first load grid snap not to happen.
|
|
grid_position = calculate_grid_position()
|
|
jump_to_grid_position()
|
|
print("Camera Set: ",grid_position, position)
|
|
print("Ray Left: ", $Left.global_position, $Left.cast_to)
|
|
|
|
|
|
|
|
func _physics_process(delta):
|
|
#print(parent.position.x)
|
|
update_grid_position()
|
|
#draw_line( Vector2(0, -half_line_length), Vector2(0, half_line_length), color, 3.0)
|
|
|
|
#func _draw():
|
|
# var color = Color(LINE_COLOR_HEX)
|
|
# draw_line( $Left.position, $Left.cast_to, color, 3.0)
|
|
# draw_line( Vector2(0,0), Vector2(200,200), color, 3.0)
|
|
|
|
|
|
func update_grid_position():
|
|
var new_grid_position = calculate_grid_position()
|
|
# if $Left.is_colliding():
|
|
# print("Oh God! No!")
|
|
# if grid_position == new_grid_position:
|
|
# return
|
|
# if the y value has changed we can flip transition the camera
|
|
##TODO: this logic is working but I don't like it.
|
|
if grid_position.y != new_grid_position.y:
|
|
if !$Up.is_colliding() and grid_position.y > new_grid_position.y:
|
|
get_tree().paused = true
|
|
grid_position = new_grid_position
|
|
timer.start()
|
|
$Camera2D.smoothing_enabled = true
|
|
jump_to_grid_position()
|
|
#wait for stability.
|
|
player_node.global_translate(Vector2(0,-10))
|
|
var a_tenth_of_the_time = int((timer.time_left / timer.wait_time) * 10)
|
|
while a_tenth_of_the_time != 0:
|
|
#print(a_tenth_of_the_time)
|
|
var time_ticks = int((timer.time_left / timer.wait_time) * 10)
|
|
if time_ticks != a_tenth_of_the_time:
|
|
print("Shifting Player Downward :", a_tenth_of_the_time)
|
|
a_tenth_of_the_time = time_ticks
|
|
player_node.global_translate(Vector2(0,-5))
|
|
# WEIRD!? You have to add this at the end of a while loop or it gets stuck.
|
|
yield(get_tree(), "idle_frame")
|
|
#yield(timer, "timeout")
|
|
$Camera2D.smoothing_enabled = false
|
|
get_tree().paused = false
|
|
elif !$Down.is_colliding() and grid_position.y < new_grid_position.y:
|
|
get_tree().paused = true
|
|
grid_position = new_grid_position
|
|
timer.start()
|
|
$Camera2D.smoothing_enabled = true
|
|
jump_to_grid_position()
|
|
# FINALLY Got one that works. I tried all sorts of stuff to move this thing!?
|
|
var a_tenth_of_the_time = int((timer.time_left / timer.wait_time) * 10)
|
|
while a_tenth_of_the_time != 0:
|
|
#print(a_tenth_of_the_time)
|
|
var time_ticks = int((timer.time_left / timer.wait_time) * 10)
|
|
if time_ticks != a_tenth_of_the_time:
|
|
print("Shifting Player Downward :", a_tenth_of_the_time)
|
|
a_tenth_of_the_time = time_ticks
|
|
player_node.global_translate(Vector2(0,2))
|
|
# WEIRD!? You have to add this at the end of a while loop or it gets stuck.
|
|
yield(get_tree(), "idle_frame")
|
|
#yield(timer, "timeout")
|
|
$Camera2D.smoothing_enabled = false
|
|
get_tree().paused = false
|
|
else:
|
|
# Only allow horizontal camera moving
|
|
grid_position.x = new_grid_position.x
|
|
jump_to_grid_position()
|
|
else:
|
|
grid_position = new_grid_position
|
|
jump_to_grid_position()
|
|
#$Left.position = Vector2(grid_position * grid_size)
|
|
#print("Ray Left: ", $Left.global_position, $Left.cast_to)
|
|
|
|
|
|
func calculate_grid_position() -> Vector2:
|
|
# this needs to heavily round off the parents position
|
|
var x_offset = floor(parent.player_position.x / grid_size.x)
|
|
var y_offset = floor(parent.player_position.y / grid_size.y)
|
|
var x = round(x_offset)
|
|
#var x = (((grid_position.x * grid_size.x) + ((grid_position.x + 1) * grid_size.x))/2)
|
|
var y = round(y_offset)
|
|
#var y = round(parent.position.y / grid_size.y)
|
|
return Vector2(x, y)
|
|
|
|
|
|
func jump_to_grid_position():
|
|
#var x_pos = (((grid_position.x * grid_size.x) + ((grid_position.x + 1) * grid_size.x))/2)
|
|
# rounding or flooring position seems to just cause a lot of jitter
|
|
var x_pos = (parent.player_position.x - grid_size.x/2)
|
|
# if (x_pos - grid_size.x/2) < 0:
|
|
# x_pos = grid_size.x/2
|
|
# if x_pos < 0:
|
|
# x_pos = 0
|
|
if $Left.is_colliding():
|
|
#print("Oh God! No! ", $Left.get_collision_point().x, $Left.global_position)
|
|
$Camera2D.limit_left = $Left.get_collision_point().x
|
|
# attempts to just change the position to the collision point created a
|
|
# ghosting behavior, now I just set the limit of the camera to the point
|
|
|
|
#x_pos = $Left.get_collision_point().x+1
|
|
#position = Vector2($Left.get_collision_point().x+1, grid_position.y * grid_size.y)
|
|
else:
|
|
$Camera2D.limit_left = -10000000
|
|
if $Right.is_colliding():
|
|
#print("Oh God! No! ", $Right.get_collision_point().x, $Right.global_position)
|
|
$Camera2D.limit_right = $Right.get_collision_point().x
|
|
else:
|
|
$Camera2D.limit_right = 10000000
|
|
|
|
if $Up.is_colliding():
|
|
$Camera2D.limit_top = $Up.get_collision_point().y
|
|
else:
|
|
$Camera2D.limit_top = -10000000
|
|
if $Down.is_colliding():
|
|
$Camera2D.limit_bottom = $Down.get_collision_point().y
|
|
else:
|
|
$Camera2D.limit_bottom = 10000000
|
|
|
|
position = Vector2( x_pos ,grid_position.y * grid_size.y)
|
|
#$Camera2D.force_update_scroll()
|
|
#position = Vector2( grid_position * grid_size)
|
|
#print("Camera Move: ",grid_position, position)
|
|
|
|
|
|
#func _physics_process(delta):
|
|
#
|
|
# $Sprite2D.position = $A.position.lerp($B.position, t)
|