Jam10/Scripts/Units/DownhillAutoscrollerPlayer.gd

246 lines
8.2 KiB
GDScript3
Raw Permalink Normal View History

2023-01-27 21:55:12 +00:00
extends Player
class_name DownhillAutoscrollerPlayer
2023-01-27 22:30:20 +00:00
export var min_speed : float = 3
2023-01-28 07:13:06 +00:00
export var max_speed : float = 11
export var player_initiated_acceleration : float = 5
2023-01-30 16:26:49 +00:00
export var boost_per_second : float = 4 / 1.36 # 6 mph
2023-01-27 21:55:12 +00:00
var last_contacted_map_elem_type : int = Constants.MapElemType.SQUARE
2023-01-28 01:56:15 +00:00
var boost : float = 0 # to movement speed
2023-01-30 16:26:49 +00:00
var boost_effect: float = 0
var instant_accel: bool = false
2023-01-28 01:56:15 +00:00
2023-01-29 00:19:29 +00:00
var respawn_pos : Vector2
2023-01-30 02:39:59 +00:00
var hit_audiostream_player : AudioStreamPlayer
var spin_audiostream_player : AudioStreamPlayer
var jump_audiostream_player : AudioStreamPlayer
var land_audiostream_player : AudioStreamPlayer
var snow_audiostream_player : AudioStreamPlayer
func init_unit_w_scene(scene):
.init_unit_w_scene(scene)
hit_audiostream_player = scene.get_node("HitAudioStreamPlayer")
spin_audiostream_player = scene.get_node("SpinAudioStreamPlayer")
jump_audiostream_player = scene.get_node("JumpAudioStreamPlayer")
land_audiostream_player = scene.get_node("LandAudioStreamPlayer")
snow_audiostream_player = scene.get_node("SnowAudioStreamPlayer")
2023-01-28 01:56:15 +00:00
func execute_actions(delta):
.execute_actions(delta)
for action_num in Constants.UNIT_TYPE_ACTIONS[Constants.UnitType.PLAYER]:
if !actions[action_num]:
continue
match action_num:
# handle custom actions
Constants.ActionType.SPIN:
spin(delta)
_:
pass
func spin(delta):
2023-01-30 02:39:59 +00:00
if (get_current_action() != Constants.UnitCurrentAction.SPINNING):
spin_audiostream_player.play()
2023-01-28 01:56:15 +00:00
set_current_action(Constants.UnitCurrentAction.SPINNING)
boost += boost_per_second * delta
2023-01-29 03:21:34 +00:00
current_sprite.visible = false
get_node("SpinningSprite").visible = true
get_node("SpinningSprite").rotation += delta * 32
func move():
set_unit_condition(Constants.UnitCondition.MOVING_STATUS, Constants.UnitMovingStatus.MOVING)
if (get_current_action() == Constants.UnitCurrentAction.IDLE
and unit_conditions[Constants.UnitCondition.IS_ON_GROUND]):
if last_contacted_map_elem_type == Constants.MapElemType.SQUARE:
set_sprite(Constants.SpriteClass.WALK, 0)
elif (last_contacted_map_elem_type == Constants.MapElemType.SMALL_SLOPE_RIGHT_1
or last_contacted_map_elem_type == Constants.MapElemType.SMALL_SLOPE_RIGHT_2):
set_sprite(Constants.SpriteClass.WALK, 1)
elif last_contacted_map_elem_type == Constants.MapElemType.SLOPE_RIGHT:
set_sprite(Constants.SpriteClass.WALK, 2)
elif (last_contacted_map_elem_type == Constants.MapElemType.SMALL_SLOPE_LEFT_1
or last_contacted_map_elem_type == Constants.MapElemType.SMALL_SLOPE_LEFT_2):
set_sprite(Constants.SpriteClass.WALK, 3)
elif last_contacted_map_elem_type == Constants.MapElemType.SLOPE_LEFT:
set_sprite(Constants.SpriteClass.WALK, 4)
2023-01-28 01:56:15 +00:00
2023-01-30 02:39:59 +00:00
func jump():
if get_current_action() != Constants.UnitCurrentAction.JUMPING:
jump_audiostream_player.play()
.jump()
2023-01-28 01:56:15 +00:00
func reset_current_action():
.reset_current_action()
if get_current_action() == Constants.UnitCurrentAction.SPINNING:
if not actions[Constants.ActionType.SPIN]:
set_current_action(Constants.UnitCurrentAction.IDLE)
2023-01-29 03:21:34 +00:00
get_node("SpinningSprite").visible = false
get_node("SpinningSprite").rotation = 0
.handle_idle()
current_sprite.visible = true
2023-01-30 02:39:59 +00:00
else:
spin_audiostream_player.stop()
2023-01-28 01:56:15 +00:00
func custom_inputs():
if scene.input_table[Constants.PlayerInput.GBA_B][scene.I_T_JUST_PRESSED]:
if not get_condition(Constants.UnitCondition.IS_ON_GROUND, true):
set_action(Constants.ActionType.SPIN)
if (get_current_action() == Constants.UnitCurrentAction.SPINNING
and scene.input_table[Constants.PlayerInput.GBA_B][scene.I_T_PRESSED]):
set_action(Constants.ActionType.SPIN)
2023-01-29 03:21:34 +00:00
func handle_idle():
if boost == 0:
.handle_idle()
2023-01-30 16:26:49 +00:00
func handle_speed(delta):
if get_current_action() == Constants.UnitCurrentAction.RECOILING:
target_move_speed = min_speed
return
if scene.input_table[Constants.PlayerInput.LEFT][scene.I_T_PRESSED]:
target_move_speed = move_toward(target_move_speed, min_speed, player_initiated_acceleration * delta)
boost_effect = 0.0
return
var slope: int = Constants.ELEM_TYPE_SLOPE[last_contacted_map_elem_type]
var speed_limit: float = Constants.SLOPE_SPEED[slope]
var accel: float = player_initiated_acceleration
if not get_condition(Constants.UnitCondition.IS_ON_GROUND, true):
speed_limit = target_move_speed
if scene.input_table[Constants.PlayerInput.RIGHT][scene.I_T_PRESSED] and speed_limit < Constants.UNIT_TYPE_MOVE_SPEEDS[unit_type]:
speed_limit = Constants.UNIT_TYPE_MOVE_SPEEDS[unit_type]
speed_limit = max(min_speed, speed_limit)
if get_condition(Constants.UnitCondition.IS_ON_GROUND, true):
if scene.input_table[Constants.PlayerInput.RIGHT][scene.I_T_PRESSED]:
speed_limit += 1.0
if boost_effect > 0:
speed_limit += boost_effect
if target_move_speed < boost_effect:
boost_effect = 0.0
elif slope < 2:
boost_effect -= delta
accel = (Constants.SLOPE_ACCEL if target_move_speed < speed_limit else Constants.SLOPE_DECEL)[slope]
target_move_speed = move_toward(target_move_speed, speed_limit, accel * delta)
if target_move_speed > speed_limit + 4.0:
target_move_speed -= speed_limit
target_move_speed *= pow(0.5, delta)
target_move_speed += speed_limit
2023-01-27 21:55:12 +00:00
func process_unit(delta, time_elapsed : float):
# always be movin'
facing = Constants.Direction.RIGHT
actions[Constants.ActionType.MOVE] = true
2023-01-30 16:26:49 +00:00
handle_speed(delta)
2023-01-27 21:55:12 +00:00
2023-01-30 16:26:49 +00:00
if boost_effect > 0:
boost_effect -= delta
2023-01-30 13:54:29 +00:00
2023-01-27 21:55:12 +00:00
.process_unit(delta, time_elapsed)
# treat all collisions as right-side collisions
func hit(dir : int):
# Unit.gd implementation override
hit_queued = true
hit_dir = Constants.Direction.RIGHT
# Player.gd implementation
set_unit_condition_with_timer(Constants.UnitCondition.IS_INVINCIBLE)
start_flash()
set_action(Constants.ActionType.RECOIL)
set_current_action(Constants.UnitCurrentAction.RECOILING)
set_unit_condition(Constants.UnitCondition.MOVING_STATUS, Constants.UnitMovingStatus.IDLE)
2023-01-30 02:39:59 +00:00
get_node("SpinningSprite").visible = false
get_node("SpinningSprite").rotation = 0
hit_audiostream_player.play()
2023-01-27 21:55:12 +00:00
# override super class's RECOIL_PUSHBACK
func handle_recoil():
if not hit_queued:
return
hit_queued = false
# skip recoil pushback logic, since target_move_speed is already
# set to min_speed
2023-01-28 01:56:15 +00:00
func landed():
2023-01-29 03:21:34 +00:00
get_node("SpinningSprite").visible = false
get_node("SpinningSprite").rotation = 0
2023-01-30 02:39:59 +00:00
land_audiostream_player.play()
2023-01-28 01:56:15 +00:00
if get_current_action() == Constants.UnitCurrentAction.SPINNING:
hit(Constants.Direction.RIGHT)
boost = 0
2023-01-30 02:39:59 +00:00
spin_audiostream_player.stop()
2023-01-28 01:56:15 +00:00
return
2023-01-30 08:02:47 +00:00
if boost > 0:
scene.find_node("CanvasLayer").flash_boost = true
2023-01-30 16:26:49 +00:00
boost_effect += boost
target_move_speed += boost
2023-01-28 01:56:15 +00:00
boost = 0
2023-01-28 09:25:49 +00:00
2023-01-29 00:19:29 +00:00
func react(delta):
.react(delta)
2023-01-28 09:25:49 +00:00
2023-01-29 00:19:29 +00:00
# Check if fallen off
if last_contacted_map_elem_type == Constants.MapElemType.OOB_LOWER:
# Called when the player falls in a hole
2023-01-28 09:25:49 +00:00
2023-01-29 00:19:29 +00:00
# Calculate respawn point
var tilemap : TileMap = get_node("../Stage")
var tile : Vector2 = pos.floor()
tile.y *= -1
while tilemap.get_cellv(tile) == 22 or tilemap.get_cellv(tile) < 0:
tile += Vector2.RIGHT
while tilemap.get_cellv(tile) >= 0:
tile += Vector2.UP
respawn_pos = tile
respawn_pos.x += 0.5
respawn_pos.y *= -1
last_contacted_map_elem_type = Constants.MapElemType.SLOPE_LEFT
# =======================
# reset speed
# should other things be done here?
# - player's ongoing trick has to be reset
# - camera has to freeze its y position as player drops offscreen
# - a smooth transition has to play
# - player starts off recoiling and slowed down (they are hit)
var spectator_cam : Camera2D = get_node("../SpectatorCam")
spectator_cam.position = get_node("Camera2D").get_camera_screen_center()
spectator_cam.offset = get_node("Camera2D").offset
spectator_cam.make_current()
var anim_player : AnimationPlayer = scene.find_node("PitTransitionPlayer")
anim_player.play("PitTransition")
2023-01-30 02:39:59 +00:00
if get_condition(Constants.UnitCondition.IS_ON_GROUND, true) and h_speed > 3:
if not snow_audiostream_player.playing:
snow_audiostream_player.play()
else:
snow_audiostream_player.stop()
2023-01-29 00:19:29 +00:00
func respawn_from_pit():
pos = respawn_pos
2023-01-30 16:26:49 +00:00
#hit(Constants.Direction.RIGHT)
set_unit_condition_with_timer(Constants.UnitCondition.IS_INVINCIBLE)
start_flash()
get_node("SpinningSprite").visible = false
get_node("SpinningSprite").rotation = 0
hit_audiostream_player.play()
2023-01-29 00:19:29 +00:00
boost = 0
2023-01-30 16:26:49 +00:00
boost_effect = 0
2023-01-29 00:19:29 +00:00
get_node("Camera2D").make_current()
2023-01-28 09:25:49 +00:00