From 6fec533222e1fb4b9b0e08c8a30cd87d51d54da1 Mon Sep 17 00:00:00 2001 From: D L Date: Fri, 27 Jan 2023 13:55:12 -0800 Subject: [PATCH] =?UTF-8?q?=C2=96DownhillAutoscroller.tscn=20changes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Scenes/DownhillAutoscroller.tscn | 29 +++++++ Scripts/Constants.gd | 6 +- Scripts/GameScene.gd | 7 +- Scripts/StageEnvironment.gd | 73 ++++++++++------ Scripts/Units/DownhillAutoscrollerPlayer.gd | 94 +++++++++++++++++++++ Scripts/Units/NPCExample.gd | 2 + Units/DownhillAutoscrollerNPC.gd | 5 ++ Units/DownhillAutoscrollerNPC.tscn | 39 +++++++++ Units/DownhillAutoscrollerPlayer.tscn | 47 +++++++++++ project.godot | 12 +++ 10 files changed, 280 insertions(+), 34 deletions(-) create mode 100644 Scenes/DownhillAutoscroller.tscn create mode 100644 Scripts/Units/DownhillAutoscrollerPlayer.gd create mode 100644 Units/DownhillAutoscrollerNPC.gd create mode 100644 Units/DownhillAutoscrollerNPC.tscn create mode 100644 Units/DownhillAutoscrollerPlayer.tscn diff --git a/Scenes/DownhillAutoscroller.tscn b/Scenes/DownhillAutoscroller.tscn new file mode 100644 index 0000000..ce50d6f --- /dev/null +++ b/Scenes/DownhillAutoscroller.tscn @@ -0,0 +1,29 @@ +[gd_scene load_steps=4 format=2] + +[ext_resource path="res://Tile Sets/TestTileSet.tres" type="TileSet" id=1] +[ext_resource path="res://Units/DownhillAutoscrollerPlayer.tscn" type="PackedScene" id=2] +[ext_resource path="res://Scripts/GameScene.gd" type="Script" id=5] + +[node name="Scene" type="Node"] +script = ExtResource( 5 ) +__meta__ = { +"_edit_vertical_guides_": [ 20.0 ] +} +tile_set_name = "TestTileSet" +camera_h_offset = 2.5 +spawning = { +Vector2( 48, -9 ): "NPC" +} + +[node name="Stage" type="TileMap" parent="."] +tile_set = ExtResource( 1 ) +cell_size = Vector2( 20, 20 ) +format = 1 +tile_data = PoolIntArray( -196611, 4, 0, -196610, 4, 0, -196609, 5, 0, -131075, 4, 0, -131074, 4, 0, -131073, 5, 0, -65539, 4, 0, -65538, 4, 0, -65537, 5, 0, -3, 4, 0, -2, 4, 0, -1, 5, 0, 65533, 4, 0, 65534, 4, 0, 65535, 4, 0, 0, 1, 0, 1, 1, 0, 2, 1, 0, 3, 1, 0, 4, 1, 0, 5, 1, 0, 6, 13, 0, 7, 12, 0, 131069, 4, 0, 131070, 4, 0, 131071, 4, 0, 65536, 4, 0, 65537, 4, 0, 65538, 4, 0, 65539, 4, 0, 65540, 4, 0, 65541, 4, 0, 65542, 4, 0, 65543, 4, 0, 65544, 18, 0, 196605, 4, 0, 196606, 4, 0, 196607, 4, 0, 131072, 4, 0, 131073, 4, 0, 131074, 4, 0, 131075, 4, 0, 131076, 4, 0, 131077, 4, 0, 131078, 4, 0, 131079, 4, 0, 131080, 4, 0, 131081, 18, 0, 262141, 4, 0, 262142, 4, 0, 262143, 4, 0, 196608, 4, 0, 196609, 4, 0, 196610, 4, 0, 196611, 4, 0, 196612, 4, 0, 196613, 4, 0, 196614, 4, 0, 196615, 4, 0, 196616, 4, 0, 196617, 4, 0, 196618, 18, 0, 327677, 4, 0, 327678, 4, 0, 327679, 4, 0, 262144, 4, 0, 262145, 4, 0, 262146, 4, 0, 262147, 4, 0, 262148, 4, 0, 262149, 4, 0, 262150, 4, 0, 262151, 4, 0, 262152, 4, 0, 262153, 4, 0, 262154, 4, 0, 262155, 13, 0, 262156, 12, 0, 393213, 4, 0, 393214, 4, 0, 393215, 4, 0, 327680, 4, 0, 327681, 4, 0, 327682, 4, 0, 327683, 4, 0, 327684, 4, 0, 327685, 4, 0, 327686, 4, 0, 327687, 4, 0, 327688, 4, 0, 327689, 4, 0, 327690, 4, 0, 327691, 4, 0, 327692, 4, 0, 327693, 2, 0, 458749, 4, 0, 458750, 4, 0, 458751, 4, 0, 393216, 4, 0, 393217, 4, 0, 393218, 4, 0, 393219, 4, 0, 393220, 4, 0, 393221, 4, 0, 393222, 4, 0, 393223, 4, 0, 393224, 4, 0, 393225, 4, 0, 393226, 4, 0, 393227, 4, 0, 393228, 4, 0, 393229, 5, 0, 393236, 9, 0, 393237, 10, 0, 393238, 13, 0, 393239, 12, 0, 524285, 4, 0, 524286, 4, 0, 524287, 4, 0, 458752, 4, 0, 458753, 4, 0, 458754, 4, 0, 458755, 4, 0, 458756, 4, 0, 458757, 4, 0, 458758, 4, 0, 458759, 4, 0, 458760, 4, 0, 458761, 4, 0, 458762, 4, 0, 458763, 4, 0, 458764, 4, 0, 458765, 4, 0, 458766, 1, 0, 458767, 1, 0, 458768, 1, 0, 458769, 1, 0, 458770, 1, 0, 458771, 1, 0, 458772, 4, 0, 458773, 4, 0, 458774, 4, 0, 458775, 4, 0, 458776, 13, 0, 458777, 12, 0, 524296, 4, 0, 524297, 4, 0, 524298, 4, 0, 524299, 4, 0, 524300, 4, 0, 524301, 4, 0, 524302, 4, 0, 524303, 4, 0, 524304, 4, 0, 524305, 4, 0, 524306, 4, 0, 524307, 4, 0, 524308, 4, 0, 524309, 4, 0, 524310, 4, 0, 524311, 4, 0, 524312, 4, 0, 524313, 4, 0, 524314, 18, 0, 589833, 4, 0, 589834, 4, 0, 589835, 4, 0, 589836, 4, 0, 589837, 4, 0, 589838, 4, 0, 589839, 4, 0, 589840, 4, 0, 589841, 4, 0, 589842, 4, 0, 589843, 4, 0, 589844, 4, 0, 589845, 4, 0, 589846, 4, 0, 589847, 4, 0, 589848, 4, 0, 589849, 4, 0, 589850, 4, 0, 589851, 18, 0, 655370, 4, 0, 655371, 4, 0, 655372, 4, 0, 655373, 4, 0, 655374, 4, 0, 655375, 4, 0, 655376, 4, 0, 655377, 4, 0, 655378, 4, 0, 655379, 4, 0, 655380, 4, 0, 655381, 4, 0, 655382, 4, 0, 655383, 4, 0, 655384, 4, 0, 655385, 4, 0, 655386, 4, 0, 655387, 4, 0, 655388, 18, 0, 720908, 4, 0, 720909, 4, 0, 720910, 4, 0, 720911, 4, 0, 720912, 4, 0, 720913, 4, 0, 720914, 4, 0, 720915, 4, 0, 720916, 4, 0, 720917, 4, 0, 720918, 4, 0, 720919, 4, 0, 720920, 4, 0, 720921, 4, 0, 720922, 4, 0, 720923, 4, 0, 720924, 4, 0, 720925, 18, 0, 720937, 0, 0, 720938, 1, 0, 720939, 1, 0, 720940, 1, 0, 720941, 1, 0, 720942, 1, 0, 720943, 1, 0, 720944, 1, 0, 720945, 1, 0, 720946, 1, 0, 720947, 1, 0, 720948, 1, 0, 720949, 1, 0, 720950, 1, 0, 720951, 1, 0, 720952, 1, 0, 720953, 1, 0, 720954, 1, 0, 786445, 4, 0, 786446, 4, 0, 786447, 4, 0, 786448, 4, 0, 786449, 4, 0, 786450, 4, 0, 786451, 4, 0, 786452, 4, 0, 786453, 4, 0, 786454, 4, 0, 786455, 4, 0, 786456, 4, 0, 786457, 4, 0, 786458, 4, 0, 786459, 4, 0, 786460, 4, 0, 786461, 4, 0, 786462, 18, 0, 786468, 16, 0, 786473, 3, 0, 786474, 4, 0, 786475, 4, 0, 786476, 4, 0, 786477, 4, 0, 786478, 4, 0, 786479, 4, 0, 786480, 4, 0, 786481, 4, 0, 786482, 4, 0, 786483, 4, 0, 786484, 4, 0, 786485, 4, 0, 786486, 4, 0, 786487, 4, 0, 786488, 4, 0, 786489, 4, 0, 786490, 4, 0, 851982, 4, 0, 851983, 4, 0, 851984, 4, 0, 851985, 4, 0, 851986, 4, 0, 851987, 4, 0, 851988, 4, 0, 851989, 4, 0, 851990, 4, 0, 851991, 4, 0, 851992, 4, 0, 851993, 4, 0, 851994, 4, 0, 851995, 4, 0, 851996, 4, 0, 851997, 4, 0, 851998, 4, 0, 851999, 13, 0, 852000, 12, 0, 852002, 9, 0, 852003, 10, 0, 852004, 5, 0, 852009, 3, 0, 852010, 4, 0, 852011, 4, 0, 852012, 4, 0, 852013, 4, 0, 852014, 4, 0, 852015, 4, 0, 852016, 4, 0, 852017, 4, 0, 852018, 4, 0, 852019, 4, 0, 852020, 4, 0, 852021, 4, 0, 852022, 4, 0, 852023, 4, 0, 852024, 4, 0, 852025, 4, 0, 852026, 4, 0, 917519, 4, 0, 917520, 4, 0, 917521, 4, 0, 917522, 4, 0, 917523, 4, 0, 917524, 4, 0, 917525, 4, 0, 917526, 4, 0, 917527, 4, 0, 917528, 4, 0, 917529, 4, 0, 917530, 4, 0, 917531, 4, 0, 917532, 4, 0, 917533, 4, 0, 917534, 4, 0, 917535, 4, 0, 917536, 4, 0, 917537, 1, 0, 917538, 4, 0, 917539, 4, 0, 917540, 5, 0, 917545, 3, 0, 917546, 4, 0, 917547, 4, 0, 917548, 4, 0, 917549, 4, 0, 917550, 4, 0, 917551, 4, 0, 917552, 4, 0, 917553, 4, 0, 917554, 4, 0, 917555, 4, 0, 917556, 4, 0, 917557, 4, 0, 917558, 4, 0, 917559, 4, 0, 917560, 4, 0, 917561, 4, 0, 917562, 4, 0, 983061, 4, 0, 983062, 4, 0, 983063, 4, 0, 983064, 4, 0, 983065, 4, 0, 983066, 4, 0, 983067, 4, 0, 983068, 4, 0, 983069, 4, 0, 983070, 4, 0, 983071, 4, 0, 983072, 4, 0, 983073, 4, 0, 983074, 4, 0, 983075, 4, 0, 983076, 5, 0, 983081, 3, 0, 983082, 4, 0, 983083, 4, 0, 983084, 4, 0, 983085, 4, 0, 983086, 4, 0, 983087, 4, 0, 983088, 4, 0, 983089, 4, 0, 983090, 4, 0, 983091, 4, 0, 983092, 4, 0, 983093, 4, 0, 983094, 4, 0, 983095, 4, 0, 983096, 4, 0, 983097, 4, 0, 983098, 4, 0, 1048599, 4, 0, 1048600, 4, 0, 1048601, 4, 0, 1048602, 4, 0, 1048603, 4, 0, 1048604, 4, 0, 1048605, 4, 0, 1048606, 4, 0, 1048607, 4, 0, 1048608, 4, 0, 1048609, 4, 0, 1048610, 4, 0, 1048611, 4, 0, 1048612, 5, 0, 1048617, 3, 0, 1048618, 4, 0, 1048619, 4, 0, 1048620, 4, 0, 1048621, 4, 0, 1048622, 4, 0, 1048623, 4, 0, 1048624, 4, 0, 1048625, 4, 0, 1048626, 4, 0, 1048627, 4, 0, 1048628, 4, 0, 1048629, 4, 0, 1048630, 4, 0, 1048631, 4, 0, 1048632, 4, 0, 1048633, 4, 0, 1048634, 4, 0, 1114136, 4, 0, 1114137, 4, 0, 1114138, 4, 0, 1114139, 4, 0, 1114140, 4, 0, 1114141, 4, 0, 1114142, 4, 0, 1114143, 4, 0, 1114144, 4, 0, 1114145, 4, 0, 1114146, 4, 0, 1114147, 4, 0, 1114148, 5, 0, 1114153, 3, 0, 1114154, 4, 0, 1114155, 4, 0, 1114156, 4, 0, 1114157, 4, 0, 1114158, 4, 0, 1114159, 4, 0, 1114160, 4, 0, 1114161, 4, 0, 1114162, 4, 0, 1114163, 4, 0, 1114164, 4, 0, 1114165, 4, 0, 1114166, 4, 0, 1114167, 4, 0, 1114168, 4, 0, 1114169, 4, 0, 1114170, 4, 0, 1179675, 4, 0, 1179676, 4, 0, 1179677, 4, 0, 1179678, 4, 0, 1179679, 4, 0, 1179680, 4, 0, 1179681, 4, 0, 1179682, 4, 0, 1179683, 4, 0, 1179684, 5, 0, 1245214, 4, 0, 1245215, 4, 0, 1245216, 4, 0, 1245217, 4, 0, 1245218, 4, 0, 1245219, 4, 0, 1245220, 5, 0 ) + +[node name="Player" parent="." instance=ExtResource( 2 )] +position = Vector2( 10, -150 ) + +[node name="Camera2D" type="Camera2D" parent="Player"] +offset = Vector2( 0, -15 ) +smoothing_speed = 6.0 diff --git a/Scripts/Constants.gd b/Scripts/Constants.gd index 30dadbf..3b38dfa 100644 --- a/Scripts/Constants.gd +++ b/Scripts/Constants.gd @@ -177,7 +177,7 @@ const UNIT_SPRITES = { } const UNIT_TYPE_MOVE_SPEEDS = { - UnitType.PLAYER: 6, + UnitType.PLAYER: 5, UnitType.NPC: 3, } @@ -187,8 +187,8 @@ const UNIT_TYPE_JUMP_SPEEDS = { const SCALE_FACTOR = 2.4 const GRID_SIZE = 20 # pixels -const GRAVITY = 30 -const MAX_FALL_SPEED = -12 +const GRAVITY = 10 +const MAX_FALL_SPEED = -9 const ACCELERATION = 35 const QUANTUM_DIST = 0.001 const SPAWN_DISTANCE = 10 diff --git a/Scripts/GameScene.gd b/Scripts/GameScene.gd index 74e54ed..8779c55 100644 --- a/Scripts/GameScene.gd +++ b/Scripts/GameScene.gd @@ -9,10 +9,11 @@ extends Node class_name GameScene export var tile_set_name: String +export var camera_h_offset : float = 1 const Constants = preload("res://Scripts/Constants.gd") const Unit = preload("res://Scripts/Unit.gd") const UNIT_DIRECTORY = { - Constants.UnitType.NPC: preload("res://Units/NPC.tscn"), + Constants.UnitType.NPC: preload("res://Units/DownhillAutoscrollerNPC.tscn"), } # positions to unit string @@ -62,9 +63,9 @@ func _ready(): func _process(delta): # visual effects if (player.facing == Constants.Direction.RIGHT): - player_cam.offset_h = 1 + player_cam.offset_h = camera_h_offset else: - player_cam.offset_h = -1 + player_cam.offset_h = -camera_h_offset read_paused() if not paused: diff --git a/Scripts/StageEnvironment.gd b/Scripts/StageEnvironment.gd index 4b52c19..f25e60b 100644 --- a/Scripts/StageEnvironment.gd +++ b/Scripts/StageEnvironment.gd @@ -12,6 +12,7 @@ var colliders = [] # if unit's move vector has at least one of these directional components, # do the collision check var collision_into_direction_arrays = [] # nested array +var collider_to_map_elem_type = {} var unit_collision_bounds = {} # maps unit type to [upper, lower, left, right] @@ -68,64 +69,70 @@ func init_stage_grid(tilemap : TileMap): break match map_elem_type: Constants.MapElemType.SQUARE: - insert_grid_collider(stage_x, stage_y, Constants.Direction.UP, 1) - insert_grid_collider(stage_x, stage_y, Constants.Direction.DOWN, 1) - insert_grid_collider(stage_x, stage_y, Constants.Direction.LEFT, 1) - insert_grid_collider(stage_x, stage_y, Constants.Direction.RIGHT, 1) + insert_grid_collider(stage_x, stage_y, Constants.Direction.UP, 1, map_elem_type) + insert_grid_collider(stage_x, stage_y, Constants.Direction.DOWN, 1, map_elem_type) + insert_grid_collider(stage_x, stage_y, Constants.Direction.LEFT, 1, map_elem_type) + insert_grid_collider(stage_x, stage_y, Constants.Direction.RIGHT, 1, map_elem_type) Constants.MapElemType.SLOPE_LEFT: try_insert_collider( Vector2(stage_x, stage_y), Vector2(stage_x + 1, stage_y + 1), - [Constants.Direction.RIGHT, Constants.Direction.DOWN] + [Constants.Direction.RIGHT, Constants.Direction.DOWN], + map_elem_type ) - insert_grid_collider(stage_x, stage_y, Constants.Direction.LEFT, 1) - insert_grid_collider(stage_x, stage_y, Constants.Direction.UP, 1) + insert_grid_collider(stage_x, stage_y, Constants.Direction.LEFT, 1, map_elem_type) + insert_grid_collider(stage_x, stage_y, Constants.Direction.UP, 1, map_elem_type) Constants.MapElemType.SLOPE_RIGHT: try_insert_collider( Vector2(stage_x, stage_y + 1), Vector2(stage_x + 1, stage_y), - [Constants.Direction.LEFT, Constants.Direction.DOWN] + [Constants.Direction.LEFT, Constants.Direction.DOWN], + map_elem_type ) - insert_grid_collider(stage_x, stage_y, Constants.Direction.RIGHT, 1) - insert_grid_collider(stage_x, stage_y, Constants.Direction.UP, 1) + insert_grid_collider(stage_x, stage_y, Constants.Direction.RIGHT, 1, map_elem_type) + insert_grid_collider(stage_x, stage_y, Constants.Direction.UP, 1, map_elem_type) Constants.MapElemType.SMALL_SLOPE_LEFT_1: try_insert_collider( Vector2(stage_x, stage_y), Vector2(stage_x + 1, stage_y + .5), - [Constants.Direction.RIGHT, Constants.Direction.DOWN] + [Constants.Direction.RIGHT, Constants.Direction.DOWN], + map_elem_type ) - insert_grid_collider(stage_x, stage_y, Constants.Direction.LEFT, .5) - insert_grid_collider(stage_x, stage_y, Constants.Direction.UP, 1) + insert_grid_collider(stage_x, stage_y, Constants.Direction.LEFT, .5, map_elem_type) + insert_grid_collider(stage_x, stage_y, Constants.Direction.UP, 1, map_elem_type) Constants.MapElemType.SMALL_SLOPE_LEFT_2: try_insert_collider( Vector2(stage_x, stage_y + .5), Vector2(stage_x + 1, stage_y + 1), - [Constants.Direction.RIGHT, Constants.Direction.DOWN] + [Constants.Direction.RIGHT, Constants.Direction.DOWN], + map_elem_type ) - insert_grid_collider(stage_x, stage_y, Constants.Direction.RIGHT, .5) - insert_grid_collider(stage_x, stage_y, Constants.Direction.LEFT, 1) - insert_grid_collider(stage_x, stage_y, Constants.Direction.UP, 1) + insert_grid_collider(stage_x, stage_y, Constants.Direction.RIGHT, .5, map_elem_type) + insert_grid_collider(stage_x, stage_y, Constants.Direction.LEFT, 1, map_elem_type) + insert_grid_collider(stage_x, stage_y, Constants.Direction.UP, 1, map_elem_type) Constants.MapElemType.SMALL_SLOPE_RIGHT_1: try_insert_collider( Vector2(stage_x, stage_y + .5), Vector2(stage_x + 1, stage_y), - [Constants.Direction.LEFT, Constants.Direction.DOWN] + [Constants.Direction.LEFT, Constants.Direction.DOWN], + map_elem_type ) - insert_grid_collider(stage_x, stage_y, Constants.Direction.RIGHT, .5) - insert_grid_collider(stage_x, stage_y, Constants.Direction.UP, 1) + insert_grid_collider(stage_x, stage_y, Constants.Direction.RIGHT, .5, map_elem_type) + insert_grid_collider(stage_x, stage_y, Constants.Direction.UP, 1, map_elem_type) Constants.MapElemType.SMALL_SLOPE_RIGHT_2: try_insert_collider( Vector2(stage_x, stage_y + 1), Vector2(stage_x + 1, stage_y + .5), - [Constants.Direction.LEFT, Constants.Direction.DOWN] + [Constants.Direction.LEFT, Constants.Direction.DOWN], + map_elem_type ) - insert_grid_collider(stage_x, stage_y, Constants.Direction.RIGHT, 1) - insert_grid_collider(stage_x, stage_y, Constants.Direction.LEFT, .5) - insert_grid_collider(stage_x, stage_y, Constants.Direction.UP, 1) + insert_grid_collider(stage_x, stage_y, Constants.Direction.RIGHT, 1, map_elem_type) + insert_grid_collider(stage_x, stage_y, Constants.Direction.LEFT, .5, map_elem_type) + insert_grid_collider(stage_x, stage_y, Constants.Direction.UP, 1, map_elem_type) Constants.MapElemType.LEDGE: - insert_grid_collider(stage_x, stage_y, Constants.Direction.DOWN, 1) + insert_grid_collider(stage_x, stage_y, Constants.Direction.DOWN, 1, map_elem_type) -func insert_grid_collider(stage_x, stage_y, direction : int, fractional_height : float): +func insert_grid_collider(stage_x, stage_y, direction : int, fractional_height : float, map_elem_type : int): var check_colliders = [] var insert_colliders = [] var point_a : Vector2 @@ -143,9 +150,9 @@ func insert_grid_collider(stage_x, stage_y, direction : int, fractional_height : Constants.Direction.RIGHT: point_a = Vector2(stage_x, stage_y + (1 * fractional_height)) point_b = Vector2(stage_x, stage_y) - try_insert_collider(point_a, point_b, [direction]) + try_insert_collider(point_a, point_b, [direction], map_elem_type) -func try_insert_collider(point_a : Vector2, point_b : Vector2, directions : Array): +func try_insert_collider(point_a : Vector2, point_b : Vector2, directions : Array, map_elem_type : int): if directions.size() == 1: # aligned with grid for i in range(len(colliders)): @@ -157,6 +164,7 @@ func try_insert_collider(point_a : Vector2, point_b : Vector2, directions : Arra return colliders.append([point_a, point_b]) collision_into_direction_arrays.append(directions) + collider_to_map_elem_type[[point_a, point_b]] = map_elem_type func are_inverse_directions(d1, d2): return ((d1 == Constants.Direction.LEFT and d2 == Constants.Direction.RIGHT) @@ -205,6 +213,10 @@ func reangle_grounded_move(unit : Unit): unit.pos.y = intersects_results[1].y + Constants.QUANTUM_DIST unit.last_contacted_ground_collider = collider reangle_move(unit, collider, true) + # slope acceleration for DownhillAutoscroller: + # set the player's last_contacted_map_elem_type field + if unit is DownhillAutoscrollerPlayer: + unit.last_contacted_map_elem_type = collider_to_map_elem_type[collider] if !has_ground_collision: reangle_move(unit, unit.last_contacted_ground_collider, true) unit.set_unit_condition(Constants.UnitCondition.IS_ON_GROUND, false) @@ -249,6 +261,11 @@ func check_collision(unit : Unit, collider, collision_into_directions, delta): # landed on ground, horizontal component to become magnitude unit.v_speed = 0 reangle_move(unit, collider, false) + + # slope acceleration for DownhillAutoscroller: + # set the player's last_contacted_map_elem_type field + if unit is DownhillAutoscrollerPlayer: + unit.last_contacted_map_elem_type = collider_to_map_elem_type[collider] else: if collider[0].x == collider[1].x: # vertical wall collision diff --git a/Scripts/Units/DownhillAutoscrollerPlayer.gd b/Scripts/Units/DownhillAutoscrollerPlayer.gd new file mode 100644 index 0000000..cbc65a9 --- /dev/null +++ b/Scripts/Units/DownhillAutoscrollerPlayer.gd @@ -0,0 +1,94 @@ +extends Player + +class_name DownhillAutoscrollerPlayer + +export var min_speed : float = 2 +export var max_speed : float = 10 +export var player_initiated_acceleration : float = 6 + +var last_contacted_map_elem_type : int = Constants.MapElemType.SQUARE + +func process_unit(delta, time_elapsed : float): + # always be movin' + facing = Constants.Direction.RIGHT + actions[Constants.ActionType.MOVE] = true + + # Fine tune the player's speed + + if get_current_action() == Constants.UnitCurrentAction.RECOILING: + target_move_speed = min_speed + else: + # override player input so that leftward movement is deceleration, + # right movement is acceleration + 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) + else: + if not get_condition(Constants.UnitCondition.IS_ON_GROUND, true): + if (target_move_speed < Constants.UNIT_TYPE_MOVE_SPEEDS[unit_type] + and scene.input_table[Constants.PlayerInput.RIGHT][scene.I_T_PRESSED]): + target_move_speed = move_toward(target_move_speed, Constants.UNIT_TYPE_MOVE_SPEEDS[unit_type], player_initiated_acceleration * delta) + else: + # shallow slope: arctan(.5) = 27 degrees, sin(27) = 0.45 + # steep slope: sin(45) = 0.71 + var ground_influenced_acceleration = 0 + var is_decel : bool = false + if (last_contacted_map_elem_type == Constants.MapElemType.SMALL_SLOPE_RIGHT_1 + or last_contacted_map_elem_type == Constants.MapElemType.SMALL_SLOPE_RIGHT_2 + or last_contacted_map_elem_type == Constants.MapElemType.SMALL_SLOPE_LEFT_1 + or last_contacted_map_elem_type == Constants.MapElemType.SMALL_SLOPE_LEFT_2): + ground_influenced_acceleration = Constants.GRAVITY * 0.45 + if (last_contacted_map_elem_type == Constants.MapElemType.SMALL_SLOPE_LEFT_1 + or last_contacted_map_elem_type == Constants.MapElemType.SMALL_SLOPE_LEFT_2): + is_decel = true + elif (last_contacted_map_elem_type == Constants.MapElemType.SLOPE_RIGHT + or last_contacted_map_elem_type == Constants.MapElemType.SLOPE_LEFT): + ground_influenced_acceleration = Constants.GRAVITY * 0.71 + if last_contacted_map_elem_type == Constants.MapElemType.SLOPE_LEFT: + is_decel = true + if is_decel or ground_influenced_acceleration == 0: + var end_speed + if ground_influenced_acceleration == 0: + # flat ground + if scene.input_table[Constants.PlayerInput.RIGHT][scene.I_T_PRESSED]: + end_speed = max(target_move_speed, Constants.UNIT_TYPE_MOVE_SPEEDS[unit_type]) + else: + end_speed = target_move_speed + else: + # incline + if scene.input_table[Constants.PlayerInput.RIGHT][scene.I_T_PRESSED]: + end_speed = Constants.UNIT_TYPE_MOVE_SPEEDS[unit_type] + else: + end_speed = min_speed + if target_move_speed < end_speed: + target_move_speed = move_toward(target_move_speed, end_speed, player_initiated_acceleration * delta) + else: + target_move_speed = move_toward(target_move_speed, end_speed, ground_influenced_acceleration * delta) + else: + var acceleration = ground_influenced_acceleration + if scene.input_table[Constants.PlayerInput.RIGHT][scene.I_T_PRESSED]: + acceleration = max(acceleration, player_initiated_acceleration) + if target_move_speed < max_speed: + target_move_speed = move_toward(target_move_speed, max_speed, acceleration * delta) + + .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) + +# 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 diff --git a/Scripts/Units/NPCExample.gd b/Scripts/Units/NPCExample.gd index ca7c3b7..e718050 100644 --- a/Scripts/Units/NPCExample.gd +++ b/Scripts/Units/NPCExample.gd @@ -1,5 +1,7 @@ extends NPCUnit +class_name NPCExample + func before_tick(): if scene.rng.randf() < 0.5: diff --git a/Units/DownhillAutoscrollerNPC.gd b/Units/DownhillAutoscrollerNPC.gd new file mode 100644 index 0000000..f4e7bbb --- /dev/null +++ b/Units/DownhillAutoscrollerNPC.gd @@ -0,0 +1,5 @@ +extends NPCExample + + +func before_tick(): + pass diff --git a/Units/DownhillAutoscrollerNPC.tscn b/Units/DownhillAutoscrollerNPC.tscn new file mode 100644 index 0000000..2e9b016 --- /dev/null +++ b/Units/DownhillAutoscrollerNPC.tscn @@ -0,0 +1,39 @@ +[gd_scene load_steps=7 format=2] + +[ext_resource path="res://Units/DownhillAutoscrollerNPC.gd" type="Script" id=1] +[ext_resource path="res://Graphics/Units/NPC.png" type="Texture" id=2] +[ext_resource path="res://Graphics/Animations/NPCWalk.tres" type="SpriteFrames" id=3] +[ext_resource path="res://Graphics/Units/NPCJump2.png" type="Texture" id=4] +[ext_resource path="res://Graphics/Units/NPCJump1.png" type="Texture" id=5] + +[sub_resource type="RectangleShape2D" id=1] +extents = Vector2( 6, 14 ) + +[node name="NPC" type="Area2D"] +collision_mask = 2 +script = ExtResource( 1 ) +unit_type = 1 + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +position = Vector2( 0, -14 ) +shape = SubResource( 1 ) + +[node name="Idle" type="Sprite" parent="."] +visible = false +texture = ExtResource( 2 ) +offset = Vector2( 0, -15 ) + +[node name="Jump1" type="Sprite" parent="."] +visible = false +texture = ExtResource( 5 ) +offset = Vector2( 0, -15 ) + +[node name="Jump2" type="Sprite" parent="."] +visible = false +texture = ExtResource( 4 ) +offset = Vector2( 0, -15 ) + +[node name="Walk" type="AnimatedSprite" parent="."] +visible = false +frames = ExtResource( 3 ) +offset = Vector2( 0, -15 ) diff --git a/Units/DownhillAutoscrollerPlayer.tscn b/Units/DownhillAutoscrollerPlayer.tscn new file mode 100644 index 0000000..95585fa --- /dev/null +++ b/Units/DownhillAutoscrollerPlayer.tscn @@ -0,0 +1,47 @@ +[gd_scene load_steps=8 format=2] + +[ext_resource path="res://Scripts/Units/DownhillAutoscrollerPlayer.gd" type="Script" id=1] +[ext_resource path="res://Graphics/Units/Player.png" type="Texture" id=2] +[ext_resource path="res://Graphics/Units/PlayerJump2.png" type="Texture" id=3] +[ext_resource path="res://Graphics/Units/PlayerJump1.png" type="Texture" id=4] +[ext_resource path="res://Graphics/Animations/PlayerWalk.tres" type="SpriteFrames" id=5] +[ext_resource path="res://Graphics/Units/PlayerRecoil.png" type="Texture" id=6] + +[sub_resource type="RectangleShape2D" id=1] +extents = Vector2( 6, 14 ) + +[node name="Player" type="Area2D"] +z_index = 1 +collision_layer = 0 +script = ExtResource( 1 ) + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +position = Vector2( 0, -14 ) +shape = SubResource( 1 ) + +[node name="Idle" type="Sprite" parent="."] +visible = false +texture = ExtResource( 2 ) +offset = Vector2( 0, -15 ) + +[node name="Walk" type="AnimatedSprite" parent="."] +visible = false +frames = ExtResource( 5 ) +offset = Vector2( 0, -15 ) + +[node name="Jump1" type="Sprite" parent="."] +visible = false +texture = ExtResource( 4 ) +offset = Vector2( 0, -15 ) + +[node name="Jump2" type="Sprite" parent="."] +visible = false +texture = ExtResource( 3 ) +offset = Vector2( 0, -15 ) + +[node name="Recoil" type="Sprite" parent="."] +visible = false +texture = ExtResource( 6 ) +offset = Vector2( 0, -15 ) + +[connection signal="area_entered" from="." to="." method="_on_Player_area_entered"] diff --git a/project.godot b/project.godot index fcd7401..2bbd980 100644 --- a/project.godot +++ b/project.godot @@ -9,11 +9,21 @@ config_version=4 _global_script_classes=[ { +"base": "Player", +"class": "DownhillAutoscrollerPlayer", +"language": "GDScript", +"path": "res://Scripts/Units/DownhillAutoscrollerPlayer.gd" +}, { "base": "Node", "class": "GameScene", "language": "GDScript", "path": "res://Scripts/GameScene.gd" }, { +"base": "NPCUnit", +"class": "NPCExample", +"language": "GDScript", +"path": "res://Scripts/Units/NPCExample.gd" +}, { "base": "Unit", "class": "NPCUnit", "language": "GDScript", @@ -30,7 +40,9 @@ _global_script_classes=[ { "path": "res://Scripts/Unit.gd" } ] _global_script_class_icons={ +"DownhillAutoscrollerPlayer": "", "GameScene": "", +"NPCExample": "", "NPCUnit": "", "Player": "", "Unit": ""