wrap player position inside of loop size
This commit is contained in:
parent
b31313ed96
commit
d851d35cac
2 changed files with 42 additions and 8 deletions
|
|
@ -13,6 +13,29 @@ var v_press_tick: int = 0 ## last time vertical axis was pressed
|
|||
|
||||
var speed_mult: float = 1.0
|
||||
|
||||
var room: Room
|
||||
var x_min_wrap: float
|
||||
var x_max_wrap: float
|
||||
var y_min_wrap: float
|
||||
var y_max_wrap: float
|
||||
var wrapping: bool
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
# TODO: better way to get current room (signal maybe?)
|
||||
room = get_parent() as Room
|
||||
assert(room != null, "no room attached")
|
||||
|
||||
if room.config.loopable:
|
||||
x_min_wrap = room.config.loop_size.position.x
|
||||
x_max_wrap = room.config.loop_size.size.x
|
||||
y_min_wrap = room.config.loop_size.position.y
|
||||
y_max_wrap = room.config.loop_size.size.y
|
||||
|
||||
wrapping = true if room.config.loopable else false
|
||||
|
||||
EventBus.connect("room_loop_toggled", _on_loop_toggled)
|
||||
|
||||
|
||||
func _input(event: InputEvent) -> void:
|
||||
if event.is_action_pressed("move_left") or event.is_action_pressed("move_right"):
|
||||
|
|
@ -28,13 +51,20 @@ func get_movement_vector() -> Vector2:
|
|||
if absf(x_strength) < DEADZONE and absf(y_strength) < DEADZONE:
|
||||
return Vector2.ZERO
|
||||
|
||||
if absf(x_strength) < DEADZONE:
|
||||
return Vector2(0, signf(y_strength))
|
||||
if absf(y_strength) < DEADZONE:
|
||||
return Vector2(signf(x_strength), 0)
|
||||
if absf(x_strength) < DEADZONE: return Vector2(0, signf(y_strength))
|
||||
if absf(y_strength) < DEADZONE: return Vector2(signf(x_strength), 0)
|
||||
|
||||
# use most recent axis press
|
||||
if h_press_tick > v_press_tick:
|
||||
return Vector2(signf(x_strength), 0)
|
||||
else:
|
||||
return Vector2(0, signf(y_strength))
|
||||
if h_press_tick > v_press_tick: return Vector2(signf(x_strength), 0)
|
||||
else: return Vector2(0, signf(y_strength))
|
||||
|
||||
|
||||
func _on_loop_toggled(enabled: bool, loop_size: Rect2i) -> void:
|
||||
if enabled:
|
||||
x_min_wrap = loop_size.position.x
|
||||
x_max_wrap = loop_size.size.x
|
||||
y_min_wrap = loop_size.position.y
|
||||
y_max_wrap = loop_size.size.y
|
||||
wrapping = true
|
||||
|
||||
else: wrapping = false
|
||||
|
|
|
|||
|
|
@ -13,6 +13,10 @@ func _state_physics_update(_delta: float) -> void:
|
|||
player.velocity = direction * player.max_speed * player.speed_mult
|
||||
|
||||
player.move_and_slide()
|
||||
|
||||
if player.wrapping:
|
||||
player.position.x = wrapf(player.position.x, player.x_min_wrap, player.x_max_wrap)
|
||||
player.position.y = wrapf(player.position.y, player.y_min_wrap, player.y_max_wrap)
|
||||
#endregion
|
||||
|
||||
if watch_state([WALKING]): return
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue