From d120c82ff647bbfcf7e5bd760855bca5f17cb496 Mon Sep 17 00:00:00 2001 From: Fisch Date: Tue, 5 Nov 2024 08:18:14 +0100 Subject: [PATCH] add checkpoint area handling --- project.godot | 6 +++--- scenes/car.tscn | 3 ++- scenes/map_01.tscn | 29 +++++++++++++++++++++++++- scripts/car.gd | 29 ++++++++++++++++++++++---- scripts/car_node.gd | 6 +++++- scripts/game.gd | 20 ++++++++++++++---- sprites/spritesheet_road/markings.tres | 9 ++------ 7 files changed, 81 insertions(+), 21 deletions(-) diff --git a/project.godot b/project.godot index 01e969e..300e742 100644 --- a/project.godot +++ b/project.godot @@ -10,7 +10,7 @@ config_version=5 [application] -config/name="CustomInputTest" +config/name="One-Button Racing" run/main_scene="res://scenes/menu.tscn" config/features=PackedStringArray("4.3", "Forward Plus") config/icon="res://icon.svg" @@ -27,5 +27,5 @@ window/stretch/mode="canvas_items" 2d_physics/layer_1="walls" 2d_physics/layer_2="walldetection" -2d_physics/layer_3="Finish" -2d_physics/layer_4="Checkpoint" +2d_physics/layer_3="Marking" +2d_physics/layer_4="Layer 4" diff --git a/scenes/car.tscn b/scenes/car.tscn index def33ee..9e15165 100644 --- a/scenes/car.tscn +++ b/scenes/car.tscn @@ -45,7 +45,8 @@ one_shot = true [node name="RayCast_Car" type="RayCast2D" parent="CharacterBody_Car"] position = Vector2(-15, 0) target_position = Vector2(31, 0) -collision_mask = 4 +collision_mask = 12 +collide_with_areas = true [node name="collisionEnableTimer" type="Timer" parent="CharacterBody_Car"] wait_time = 0.1 diff --git a/scenes/map_01.tscn b/scenes/map_01.tscn index 1454826..d54cd66 100644 --- a/scenes/map_01.tscn +++ b/scenes/map_01.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=5 format=4 uid="uid://dghq8c8asg7h1"] +[gd_scene load_steps=6 format=4 uid="uid://dghq8c8asg7h1"] [ext_resource type="TileSet" uid="uid://beswbm12qkkxk" path="res://sprites/spritesheet_road/bg_road.tres" id="2_i1338"] [ext_resource type="TileSet" uid="uid://buu8w8n61kbxf" path="res://sprites/spritesheet_road/markings.tres" id="3_ktygs"] @@ -7,6 +7,9 @@ [sub_resource type="TileSet" id="TileSet_5knbn"] tile_size = Vector2i(128, 128) +[sub_resource type="RectangleShape2D" id="RectangleShape2D_daqun"] +size = Vector2(23, 106.5) + [node name="Map" type="Node"] [node name="Background" type="TileMapLayer" parent="."] @@ -37,3 +40,27 @@ tile_set = ExtResource("4_r58io") [node name="road_l" type="TileMapLayer" parent="."] tile_map_data = PackedByteArray("AAAFAP//AAADAAAAAHAFAAAAAAACAAAAAHAEAP//AAADAAEAAHAHAAAAAAAHAAIAAHAHAAEAAAAGAAIAAHAGAAAAAAAHAAMAAHAHAAIAAAAHAAMAACAHAAMAAAAHAAIAACAGAAMAAAAGAAIAACAFAAMAAAAHAAEAAGAFAAQAAAAGAAEAAGAEAAQAAAAGAAAAAGADAAQAAgABAAAAAEACAAQAAgABAAAAAEABAAQAAgABAAAAAEAAAAQAAgADAAEAAED+/wQAAAAHAAMAAED9/wMAAAAGAAIAAED9/wQAAAAHAAIAAED//wQAAgABAAAAAED9/wIAAgADAAAAABD+/wIAAgABAAAAAHD//wIAAgABAAAAAHAAAAIAAgABAAAAAHABAAIAAgABAAAAAHABAAEAAgABAAAAAFAAAAEAAgABAAAAAFD//wEAAgABAAAAAFD+/wEAAgABAAAAAFD9/wEAAgABAAAAAFD8/wEAAgAGAAIAAED8/wIAAgAAAAIAAAD7/wIAAgAGAAEAAAD7/wMAAgAAAAIAAAD6/wMAAgAEAAIAAAADAP//AgABAAAAAHACAP//AgABAAAAAHABAP//AgABAAAAAHAAAP//AgABAAAAAHD/////AgABAAAAAHD+////AgABAAAAAHD9////AgABAAAAAHD8////AgABAAAAAHD7////AgABAAAAAHD6////AgAFAAIAAED5////AgAAAAIAAHD4/wAAAgAAAAIAAHD3/wEAAgAAAAIAAHD5/wAAAgAGAAEAAHD4/wEAAgAGAAEAAHD3/wIAAgADAAIAAAD7/wQAAAAAAAUAAED7/wUAAAABAAUAAED6/wQAAAAAAAQAAED9/wUAAAADAAAAAHD9/wYAAAACAAAAAHD8/wUAAAADAAEAAHD+/wYAAgAFAAAAAFD+/wUAAgADAAAAABD//wUAAgABAAAAAHAAAAUAAgABAAAAAHABAAUAAgABAAAAAHACAAUAAgADAAAAAAACAAYAAgADAAAAAFAAAAgAAAAGAAIAAFAAAAkAAAAHAAIAAFD//wkAAAAHAAMAAFABAAYAAAAAAAQAABABAAcAAAAAAAUAABAAAAcAAAABAAUAABD+/wkAAgABAAAAAFD9/wkAAgABAAAAAFD8/wkAAAAGAAAAAHD7/wgAAAAHAAEAAHD7/wkAAAAGAAEAAHD6/wgAAgABAAAAAFD5/wgAAgABAAAAAFD4/wgAAAAGAAIAADD3/wcAAAAHAAMAADD3/wgAAAAHAAIAADD3/wQAAgADAAEAADD3/wYAAgABAAAAADD3/wUAAgABAAAAADD3/wMAAgABAAAAADACAAIAAgAFAAAAAFACAAEAAgAFAAAAAHA=") tile_set = ExtResource("4_r58io") + +[node name="area_finish" type="Area2D" parent="."] +collision_layer = 4 +collision_mask = 4 + +[node name="CollisionShape2D" type="CollisionShape2D" parent="area_finish"] +position = Vector2(89.5, -0.5) +shape = SubResource("RectangleShape2D_daqun") + +[node name="area_cp1" type="Area2D" parent="."] +collision_layer = 4 +collision_mask = 4 + +[node name="CollisionShape2D" type="CollisionShape2D" parent="area_cp1"] +position = Vector2(267, 575.5) +shape = SubResource("RectangleShape2D_daqun") + +[node name="area_cp2" type="Area2D" parent="."] +collision_layer = 4 +collision_mask = 4 + +[node name="CollisionShape2D" type="CollisionShape2D" parent="area_cp2"] +position = Vector2(235, 833.75) +shape = SubResource("RectangleShape2D_daqun") diff --git a/scripts/car.gd b/scripts/car.gd index 8d562b6..71840d2 100644 --- a/scripts/car.gd +++ b/scripts/car.gd @@ -6,6 +6,9 @@ extends CharacterBody2D const COLLISIONMASK_FINISH=3 #set in road_overlay const COLLISIONMASK_CHECKPOINT=4 #set in road_overlay +var checkpoints :Array[String]=[] #gets set on car creation +var checkpointtimes :Array[float]=[] + const ROAD_R_NAME="road_r" const ROAD_L_NAME="road_l" @@ -64,6 +67,8 @@ var playerid=0 func _ready() -> void: collision_shape.disabled=true #disable collisions on start. also to avoid collision when initially setting position + + func _physics_process(delta: float) -> void: acceleration=Vector2.ZERO @@ -200,10 +205,26 @@ func calculate_steering(delta:float): func check_markers(): if ray_cast_car.is_colliding(): #print("Marker: "+str(ray_cast_car.get_collider())) - if ray_cast_car.get_collision_mask_value(COLLISIONMASK_FINISH): - print("Player "+str(playerid)+" Finished") - if ray_cast_car.get_collision_mask_value(COLLISIONMASK_CHECKPOINT): - print("Player "+str(playerid)+" Checkpint") + #if ray_cast_car.get_collision_mask_value(COLLISIONMASK_FINISH): + # print("Player "+str(playerid)+" Finished") + #if ray_cast_car.get_collision_mask_value(COLLISIONMASK_CHECKPOINT): + var rcc_collidername=ray_cast_car.get_collider().name + if rcc_collidername=="area_finish": + + var num_cp_collected=0 + for cpt in checkpointtimes: + if cpt>0: + num_cp_collected+=1 + #print("Player "+str(playerid)+" drove through Finish") + if num_cp_collected==checkpointtimes.size(): + print("Player "+str(playerid)+" Finished") + #TODO: get final time + checkpointtimes.fill(0) + elif rcc_collidername.begins_with("area_cp"): + var checkpoint_i=checkpoints.find(rcc_collidername) + if checkpoint_i>=0 and checkpointtimes[checkpoint_i]==0: #found and no time for this cp yet + checkpointtimes[checkpoint_i]=10 # TODO: set actual time here + print("Player "+str(playerid)+" Checkpoint "+str(ray_cast_car.get_collider().name)) func constrain(val,a,b): var vmin=min(a,b) diff --git a/scripts/car_node.gd b/scripts/car_node.gd index 04df5e3..1f659a5 100644 --- a/scripts/car_node.gd +++ b/scripts/car_node.gd @@ -14,8 +14,12 @@ func setPosition(p:Vector2): func setPlayerinformation(playerid, playercolor): cbcar.playerid=playerid - #TODO set playercolor carbody.modulate = playercolor + +func setCheckpoints(cps): + cbcar.checkpoints=cps + cbcar.checkpointtimes.resize(cps.size()) + cbcar.checkpointtimes.fill(0.0) func move_and_slide(): cbcar.move_and_slide() diff --git a/scripts/game.gd b/scripts/game.gd index 536bc8e..61faa40 100644 --- a/scripts/game.gd +++ b/scripts/game.gd @@ -16,6 +16,21 @@ var zoom_normal=2 # Called when the node enters the scene tree for the first time. func _ready() -> void: + + + #Load Map + #var num_checkpoints=0 + var checkpoints :Array[String]=[] + var mapscene=load("res://scenes/map_01.tscn") + var mapsceneinstance=mapscene.instantiate() + add_child(mapsceneinstance) + for mapc in mapsceneinstance.get_children(): + if mapc.name.begins_with("area_cp"): + #num_checkpoints+=1 + checkpoints.append(mapc.name) + + print(str(checkpoints.size())+" Checkpoints found") + var i=0 #for playerkey in Gamestate.getPlayerkeys(): #create all players for player in Gamestate.getPlayers(): @@ -25,16 +40,13 @@ func _ready() -> void: newcarinstance.setPlayerinformation(i,player.color) newcarinstance.setPosition(Vector2(0,ceil(i/2.0)*(fmod(i,2)-0.5)*2.0*caroffset)) + newcarinstance.setCheckpoints(checkpoints) #print("Position car "+str(i)+" = "+str(ceil(i/2.0)*(fmod(i,2)-0.5)*2.0*caroffset)) print("carpos is ="+str(newcarinstance.getPosition())+" instancepos="+str(newcarinstance.position)) i+=1 - #Load Map - var mapscene=load("res://scenes/map_01.tscn") - var mapsceneinstance=mapscene.instantiate() - add_child(mapsceneinstance) # Called every frame. 'delta' is the elapsed time since the previous frame. diff --git a/sprites/spritesheet_road/markings.tres b/sprites/spritesheet_road/markings.tres index 24db0cb..ef60db2 100644 --- a/sprites/spritesheet_road/markings.tres +++ b/sprites/spritesheet_road/markings.tres @@ -6,16 +6,11 @@ texture = ExtResource("1_hbu2l") texture_region_size = Vector2i(128, 128) 0:0/0 = 0 -0:0/0/physics_layer_0/polygon_0/points = PackedVector2Array(-64, -35, 56, -35, 56, -13, -64, -13) 1:0/0 = 0 -1:0/0/physics_layer_0/polygon_0/points = PackedVector2Array(-64, -35, 64, -35, 64, -13, -64, -13) 2:0/0 = 0 -2:0/0/physics_layer_1/polygon_0/points = PackedVector2Array(-64, -2, 64, -2, 64, 4, -64, 4) [resource] tile_size = Vector2i(128, 128) -physics_layer_0/collision_layer = 4 -physics_layer_0/collision_mask = 4 -physics_layer_1/collision_layer = 8 -physics_layer_1/collision_mask = 8 +physics_layer_0/collision_layer = 0 +physics_layer_0/collision_mask = 0 sources/1 = SubResource("TileSetAtlasSource_3gx66")