diff --git a/OpponentCar.gd b/OpponentCar.gd index 1cfb4bf..7355c72 100644 --- a/OpponentCar.gd +++ b/OpponentCar.gd @@ -1,6 +1,6 @@ extends Node2D -onready var track = get_node("/root/Track2/Track/TrackOpponent/") +onready var track = get_node("/root/Track2/RaceTrack/TrackOpponent/") export (float) var speed = 0.125 diff --git a/PlayerCar.gd b/PlayerCar.gd index 1f4f90e..6ce506a 100644 --- a/PlayerCar.gd +++ b/PlayerCar.gd @@ -7,7 +7,7 @@ enum { RIGHT, DOWN_RIGHT, DOWN, DOWN_LEFT, LEFT, UP_LEFT, UP, UP_RIGHT } # TODO: # - Move track related logic inside TrackPlayer script ? -onready var track = get_node("/root/Track2/Track/TrackPlayer/") +onready var track = get_node("/root/Track2/RaceTrack/TrackPlayer/") var position_on_track := 0.0 var target_on_track := 0.0 diff --git a/RaceTrack.gd b/RaceTrack.gd new file mode 100644 index 0000000..aa3738a --- /dev/null +++ b/RaceTrack.gd @@ -0,0 +1,86 @@ +extends Path2D + +enum { UP, RIGHT, DOWN, LEFT } +const TRACK_TILES = [ + Vector2(7,1), Vector2(8,1), Vector2(9,1), Vector2(9,2), Vector2(9,3), + Vector2(8,3), Vector2(7,3), Vector2(7,4), Vector2(7,5), Vector2(7,6), + Vector2(6,6), Vector2(6,5), Vector2(6,4), Vector2(6,3), Vector2(5,3), + Vector2(4,3), Vector2(4,2), Vector2(4,1), Vector2(5,1), Vector2(6,1), +] + +var tile_size : int = 128 +var half_size : int = tile_size / 2 +onready var map = get_node("/root/Track2/TileMap") +# Called when the node enters the scene tree for the first time. +func _ready(): + _build_track(TRACK_TILES) + +func _draw(): + draw_polyline(curve.get_baked_points(), Color.red, 4.0) + for idx in range(0,curve.get_point_count()): + draw_circle(curve.get_point_in(idx), 2.0, Color.green) + draw_circle(curve.get_point_out(idx), 2.0, Color.blue) + +func _build_track(tiles): + """ Build the track curve given a tile path """ + var _curve = get_curve() + _curve.clear_points() + # Set start point + var start_point = map.map_to_world(tiles[0]) + start_point.y += half_size + start_point.x += half_size + _point_at(_curve, start_point, RIGHT) + for idx in range(0, tiles.size()): + var next_idx = idx + 1 if idx < tiles.size() - 1 else 0 + var dir = _direction_to(tiles[idx], tiles[next_idx]) + # Find position of next point + var pos = map.map_to_world(tiles[idx]) + match dir: + # We start from upper left corner + UP: pos = Vector2(pos.x + half_size, pos.y) + RIGHT: pos = Vector2(pos.x + tile_size, pos.y + half_size) + DOWN: pos = Vector2(pos.x + half_size, pos.y + tile_size) + LEFT: pos = Vector2(pos.x, pos.y + half_size) + _point_at(_curve, pos, dir) + _point_at(_curve, start_point, RIGHT) + set_curve(_curve) + print_debug("Built track !", _curve) +# Called every frame. 'delta' is the elapsed time since the previous frame. +#func _process(delta): +# pass + +func _point_at(_curve, pos, dir): + # Use only control_in since it gives smoother turns + var half_size = tile_size / 2 + var control_in + match dir: + DOWN: + control_in = Vector2(0, - half_size) + #control_out = Vector2(0, half_size) + UP: + control_in = Vector2(0, half_size) + #control_out = Vector2(0, -half_size) + RIGHT: + control_in = Vector2( - half_size, 0) + #control_out = Vector2(half_size, 0) + LEFT: + control_in = Vector2(half_size, 0) + #control_out = Vector2(-half_size, 0) + #print_debug("Add point: ", pos, ", in=", control_in), " out=", control_out, " | ", dir) + _curve.add_point(pos, control_in) #, control_out) + + +func _direction_to(from_tile, to_tile): + var x_dir = to_tile.x - from_tile.x + var y_dir = to_tile.y - from_tile.y + if abs(x_dir + y_dir) == 1: + if x_dir == 1: + return RIGHT + elif x_dir == -1: + return LEFT + elif y_dir == 1: + return DOWN + elif y_dir == -1: + return UP + else: + print_debug("Panic!! Path is not contiguous between", from_tile, " and ", to_tile) \ No newline at end of file diff --git a/RaceTrack.tscn b/RaceTrack.tscn new file mode 100644 index 0000000..094add3 --- /dev/null +++ b/RaceTrack.tscn @@ -0,0 +1,72 @@ +[gd_scene load_steps=30 format=2] + +[ext_resource path="res://RaceTrack.gd" type="Script" id=1] +[ext_resource path="res://OpponentCar.gd" type="Script" id=2] +[ext_resource path="res://assets/2D_Car_Pack_DevilsWorkShop_V01/bus/busiso_0007.png" type="Texture" id=3] +[ext_resource path="res://assets/2D_Car_Pack_DevilsWorkShop_V01/bus/busiso_0004.png" type="Texture" id=4] +[ext_resource path="res://assets/2D_Car_Pack_DevilsWorkShop_V01/bus/busiso_0001.png" type="Texture" id=5] +[ext_resource path="res://assets/2D_Car_Pack_DevilsWorkShop_V01/bus/busiso_0002.png" type="Texture" id=6] +[ext_resource path="res://assets/2D_Car_Pack_DevilsWorkShop_V01/bus/busiso_0003.png" type="Texture" id=7] +[ext_resource path="res://assets/2D_Car_Pack_DevilsWorkShop_V01/bus/busiso_0000.png" type="Texture" id=8] +[ext_resource path="res://assets/2D_Car_Pack_DevilsWorkShop_V01/bus/busiso_0005.png" type="Texture" id=9] +[ext_resource path="res://assets/2D_Car_Pack_DevilsWorkShop_V01/bus/busiso_0006.png" type="Texture" id=10] +[ext_resource path="res://track1/policeCar/policeiso_0003.png" type="Texture" id=11] +[ext_resource path="res://track1/policeCar/policeiso_0004.png" type="Texture" id=12] +[ext_resource path="res://track1/policeCar/policeiso_0001.png" type="Texture" id=13] +[ext_resource path="res://track1/policeCar/policeiso_0002.png" type="Texture" id=14] +[ext_resource path="res://track1/policeCar/policeiso_0007.png" type="Texture" id=15] +[ext_resource path="res://track1/policeCar/policeiso_0000.png" type="Texture" id=16] +[ext_resource path="res://track1/policeCar/policeiso_0005.png" type="Texture" id=17] +[ext_resource path="res://track1/policeCar/policeiso_0006.png" type="Texture" id=18] +[ext_resource path="res://assets/2D_Car_Pack_DevilsWorkShop_V01/car02/car02iso_0003.png" type="Texture" id=19] +[ext_resource path="res://assets/2D_Car_Pack_DevilsWorkShop_V01/car02/car02iso_0004.png" type="Texture" id=20] +[ext_resource path="res://assets/2D_Car_Pack_DevilsWorkShop_V01/car02/car02iso_0001.png" type="Texture" id=21] +[ext_resource path="res://assets/2D_Car_Pack_DevilsWorkShop_V01/car02/car02iso_0002.png" type="Texture" id=22] +[ext_resource path="res://assets/2D_Car_Pack_DevilsWorkShop_V01/car02/car02iso_0007.png" type="Texture" id=23] +[ext_resource path="res://assets/2D_Car_Pack_DevilsWorkShop_V01/car02/car02iso_0000.png" type="Texture" id=24] +[ext_resource path="res://assets/2D_Car_Pack_DevilsWorkShop_V01/car02/car02iso_0005.png" type="Texture" id=25] +[ext_resource path="res://assets/2D_Car_Pack_DevilsWorkShop_V01/car02/car02iso_0006.png" type="Texture" id=26] +[ext_resource path="res://CarSprite.gd" type="Script" id=27] + +[sub_resource type="Curve2D" id=6] +_data = { +"points": PoolVector2Array( ) +} + +[sub_resource type="SpriteFrames" id=2] +animations = [ { +"frames": [ ExtResource( 3 ), ExtResource( 4 ), ExtResource( 5 ), ExtResource( 6 ), ExtResource( 7 ), ExtResource( 8 ), ExtResource( 9 ), ExtResource( 10 ) ], +"loop": true, +"name": "bus", +"speed": 5.0 +}, { +"frames": [ ExtResource( 11 ), ExtResource( 12 ), ExtResource( 13 ), ExtResource( 14 ), ExtResource( 15 ), ExtResource( 16 ), ExtResource( 17 ), ExtResource( 18 ) ], +"loop": true, +"name": "police", +"speed": 5.0 +}, { +"frames": [ ExtResource( 19 ), ExtResource( 20 ), ExtResource( 21 ), ExtResource( 22 ), ExtResource( 23 ), ExtResource( 24 ), ExtResource( 25 ), ExtResource( 26 ) ], +"loop": true, +"name": "car", +"speed": 5.0 +} ] + +[node name="RaceTrack" type="Path2D"] +curve = SubResource( 6 ) +script = ExtResource( 1 ) + +[node name="TrackOpponent" type="PathFollow2D" parent="."] +position = Vector2( 928.559, 184.278 ) +rotation = -0.013337 + +[node name="OpponentCar" type="Node2D" parent="TrackOpponent"] +position = Vector2( 0, 22 ) +scale = Vector2( 0.25, 0.25 ) +script = ExtResource( 2 ) + +[node name="CarSprite" type="AnimatedSprite" parent="TrackOpponent/OpponentCar"] +position = Vector2( -3.20422, 3.0376 ) +scale = Vector2( 0.8, 0.8 ) +frames = SubResource( 2 ) +animation = "car" +script = ExtResource( 27 ) diff --git a/TileMap2.gd b/TileMap2.gd index cac8a67..5039dfa 100644 --- a/TileMap2.gd +++ b/TileMap2.gd @@ -14,7 +14,7 @@ const TRACK_TILES = [ Vector2(7,1), Vector2(8,1), Vector2(9,1), Vector2(9,2), Vector2(9,3), Vector2(8,3), Vector2(7,3), Vector2(7,4), Vector2(7,5), Vector2(7,6), Vector2(6,6), Vector2(6,5), Vector2(6,4), Vector2(6,3), Vector2(5,3), - Vector2(4,3), Vector2(4,2), Vector2(4,1), Vector2(5,1), Vector2(6,1), + Vector2(4,3), Vector2(4,2), Vector2(4,1), Vector2(5,1), Vector2(6,1), ] var laps = 0 diff --git a/Track2.tscn b/Track2.tscn index 5b8d06e..3ed5887 100644 --- a/Track2.tscn +++ b/Track2.tscn @@ -1,62 +1,19 @@ -[gd_scene load_steps=39 format=2] +[gd_scene load_steps=13 format=2] [ext_resource path="res://TileMap2.gd" type="Script" id=1] -[ext_resource path="res://PlayerCar.tscn" type="PackedScene" id=2] -[ext_resource path="res://OpponentCar.gd" type="Script" id=3] -[ext_resource path="res://assets/2D_Car_Pack_DevilsWorkShop_V01/bus/busiso_0007.png" type="Texture" id=4] -[ext_resource path="res://assets/2D_Car_Pack_DevilsWorkShop_V01/bus/busiso_0004.png" type="Texture" id=5] -[ext_resource path="res://assets/2D_Car_Pack_DevilsWorkShop_V01/bus/busiso_0001.png" type="Texture" id=6] -[ext_resource path="res://assets/2D_Car_Pack_DevilsWorkShop_V01/bus/busiso_0002.png" type="Texture" id=7] -[ext_resource path="res://assets/2D_Car_Pack_DevilsWorkShop_V01/bus/busiso_0003.png" type="Texture" id=8] -[ext_resource path="res://assets/2D_Car_Pack_DevilsWorkShop_V01/bus/busiso_0000.png" type="Texture" id=9] -[ext_resource path="res://assets/2D_Car_Pack_DevilsWorkShop_V01/bus/busiso_0005.png" type="Texture" id=10] -[ext_resource path="res://assets/2D_Car_Pack_DevilsWorkShop_V01/bus/busiso_0006.png" type="Texture" id=11] -[ext_resource path="res://track1/policeCar/policeiso_0003.png" type="Texture" id=12] -[ext_resource path="res://track1/policeCar/policeiso_0004.png" type="Texture" id=13] -[ext_resource path="res://track1/policeCar/policeiso_0001.png" type="Texture" id=14] -[ext_resource path="res://track1/policeCar/policeiso_0002.png" type="Texture" id=15] -[ext_resource path="res://track1/policeCar/policeiso_0007.png" type="Texture" id=16] -[ext_resource path="res://track1/policeCar/policeiso_0000.png" type="Texture" id=17] -[ext_resource path="res://track1/policeCar/policeiso_0005.png" type="Texture" id=18] -[ext_resource path="res://track1/policeCar/policeiso_0006.png" type="Texture" id=19] -[ext_resource path="res://assets/2D_Car_Pack_DevilsWorkShop_V01/car02/car02iso_0003.png" type="Texture" id=20] -[ext_resource path="res://assets/2D_Car_Pack_DevilsWorkShop_V01/car02/car02iso_0004.png" type="Texture" id=21] -[ext_resource path="res://assets/2D_Car_Pack_DevilsWorkShop_V01/car02/car02iso_0001.png" type="Texture" id=22] -[ext_resource path="res://assets/2D_Car_Pack_DevilsWorkShop_V01/car02/car02iso_0002.png" type="Texture" id=23] -[ext_resource path="res://assets/2D_Car_Pack_DevilsWorkShop_V01/car02/car02iso_0007.png" type="Texture" id=24] -[ext_resource path="res://assets/2D_Car_Pack_DevilsWorkShop_V01/car02/car02iso_0000.png" type="Texture" id=25] -[ext_resource path="res://assets/2D_Car_Pack_DevilsWorkShop_V01/car02/car02iso_0005.png" type="Texture" id=26] -[ext_resource path="res://assets/2D_Car_Pack_DevilsWorkShop_V01/car02/car02iso_0006.png" type="Texture" id=27] -[ext_resource path="res://CarSprite.gd" type="Script" id=28] -[ext_resource path="res://track2/track.png" type="Texture" id=29] -[ext_resource path="res://Gui.tscn" type="PackedScene" id=30] -[ext_resource path="res://Gui.gd" type="Script" id=31] -[ext_resource path="res://Oswald-Bold.otf" type="DynamicFontData" id=32] -[ext_resource path="res://Wrong Way.gd" type="Script" id=33] +[ext_resource path="res://RaceTrack.tscn" type="PackedScene" id=2] +[ext_resource path="res://PlayerCar.tscn" type="PackedScene" id=3] +[ext_resource path="res://track2/track.png" type="Texture" id=4] +[ext_resource path="res://Gui.tscn" type="PackedScene" id=5] +[ext_resource path="res://Gui.gd" type="Script" id=6] +[ext_resource path="res://Oswald-Bold.otf" type="DynamicFontData" id=7] +[ext_resource path="res://Wrong Way.gd" type="Script" id=8] [sub_resource type="Curve2D" id=1] _data = { "points": PoolVector2Array( 0, 0, 0, 0, 480.755, 160.968, -44.1176, -1.2605, 44.1176, 1.2605, 684, 160.042, 0, 0, 0, 0, 759.607, 207.133, 0, -74.5098, 0, 74.5098, 769.296, 303.494, 0.926585, -49.109, -0.926585, 49.109, 768.41, 373.455, 31.9672, -0.926585, -31.9672, 0.926585, 708.312, 430.479, 0, 0, 0, 0, 572.437, 428.124, 6.46179, -42.6478, -6.46179, 42.6478, 517.286, 470.208, 0, 0, 0, 0, 513.409, 733.849, 76.2492, 0, -76.2492, 0, 447.99, 810.7, 0, 0, 0, 0, 382.881, 731.264, 0, 52.9867, 0, -52.9867, 384.173, 475.378, 0, 0, 0, 0, 329.971, 426.614, 13.9668, 1.39668, -13.9668, -1.39668, 184.018, 428.709, 1.39668, 43.297, -1.39668, -43.297, 121.023, 363.376, 0.814508, 29.1703, -0.814508, -29.1703, 118.928, 292.145, -63.5488, 1.39668, 63.5488, -1.39668, 182.477, 159.461, 0, 0, 0, 0, 304.658, 160.902, 0, 0, 0, 0, 480.755, 161.631 ) } -[sub_resource type="SpriteFrames" id=2] -animations = [ { -"frames": [ ExtResource( 4 ), ExtResource( 5 ), ExtResource( 6 ), ExtResource( 7 ), ExtResource( 8 ), ExtResource( 9 ), ExtResource( 10 ), ExtResource( 11 ) ], -"loop": true, -"name": "bus", -"speed": 5.0 -}, { -"frames": [ ExtResource( 12 ), ExtResource( 13 ), ExtResource( 14 ), ExtResource( 15 ), ExtResource( 16 ), ExtResource( 17 ), ExtResource( 18 ), ExtResource( 19 ) ], -"loop": true, -"name": "police", -"speed": 5.0 -}, { -"frames": [ ExtResource( 20 ), ExtResource( 21 ), ExtResource( 22 ), ExtResource( 23 ), ExtResource( 24 ), ExtResource( 25 ), ExtResource( 26 ), ExtResource( 27 ) ], -"loop": true, -"name": "car", -"speed": 5.0 -} ] - [sub_resource type="Gradient" id=3] colors = PoolColorArray( 0.242729, 0.410156, 0.128174, 1, 0.088892, 0.507812, 0.0753784, 1 ) @@ -69,7 +26,7 @@ outline_size = 2 outline_color = Color( 0.254902, 0.121569, 0.121569, 1 ) extra_spacing_char = 3 extra_spacing_space = 3 -font_data = ExtResource( 32 ) +font_data = ExtResource( 7 ) [node name="Track2" type="Node2D"] @@ -78,43 +35,29 @@ cell_size = Vector2( 128, 128 ) format = 1 script = ExtResource( 1 ) +[node name="RaceTrack" parent="." instance=ExtResource( 2 )] + +[node name="TrackPlayer" type="PathFollow2D" parent="RaceTrack"] +position = Vector2( 928.559, 184.278 ) +rotation = -0.013337 + +[node name="PlayerCar" parent="RaceTrack/TrackPlayer" instance=ExtResource( 3 )] +position = Vector2( 0, -22.892 ) +scale = Vector2( 0.25, 0.25 ) +skin = "car" + [node name="Track" type="Path2D" parent="."] position = Vector2( 447.804, 23.3101 ) z_index = 2 curve = SubResource( 1 ) -[node name="TrackPlayer" type="PathFollow2D" parent="Track"] -position = Vector2( 480.755, 160.968 ) -rotation = -0.013337 - -[node name="PlayerCar" parent="Track/TrackPlayer" instance=ExtResource( 2 )] -position = Vector2( 0, -22.892 ) -scale = Vector2( 0.25, 0.25 ) -skin = "police" - -[node name="TrackOpponent" type="PathFollow2D" parent="Track"] -position = Vector2( 480.755, 160.968 ) -rotation = -0.013337 - -[node name="OpponentCar" type="Node2D" parent="Track/TrackOpponent"] -position = Vector2( 0, 22 ) -scale = Vector2( 0.25, 0.25 ) -script = ExtResource( 3 ) - -[node name="CarSprite" type="AnimatedSprite" parent="Track/TrackOpponent/OpponentCar"] -position = Vector2( -3.20422, 3.0376 ) -scale = Vector2( 0.8, 0.8 ) -frames = SubResource( 2 ) -animation = "car" -script = ExtResource( 28 ) - [node name="CanvasLayer" type="CanvasLayer" parent="."] layer = -1 [node name="TextureRect" type="TextureRect" parent="CanvasLayer"] margin_right = 1923.0 margin_bottom = 1084.0 -texture = ExtResource( 29 ) +texture = ExtResource( 4 ) [node name="CanvasLayer" type="CanvasLayer" parent="CanvasLayer"] layer = -1 @@ -132,21 +75,21 @@ margin_right = 159.21 margin_bottom = 107.773 [node name="Label" type="Label" parent="HBoxContainer"] -margin_top = 17.0 +margin_top = 18.0 margin_right = 53.0 -margin_bottom = 31.0 +margin_bottom = 32.0 text = "Rotation" [node name="Rotation" type="Label" parent="HBoxContainer"] margin_left = 57.0 -margin_top = 17.0 +margin_top = 18.0 margin_right = 71.0 -margin_bottom = 31.0 +margin_bottom = 32.0 text = "0°" -[node name="GUI" parent="." instance=ExtResource( 30 )] +[node name="GUI" parent="." instance=ExtResource( 5 )] editor/display_folded = false -script = ExtResource( 31 ) +script = ExtResource( 6 ) [node name="Wrong Way" type="Label" parent="."] margin_left = 792.258 @@ -160,9 +103,9 @@ custom_colors/font_color = Color( 0.780392, 0.129412, 0.129412, 1 ) text = "WRONG WAY !!" align = 1 valign = 1 -script = ExtResource( 33 ) +script = ExtResource( 8 ) [connection signal="lap_completed" from="TileMap" to="GUI" method="_on_TileMap_lap_completed"] -[connection signal="player_moved" from="TileMap" to="Track/TrackPlayer/PlayerCar" method="_on_TileMap_player_moved"] +[connection signal="player_moved" from="TileMap" to="RaceTrack/TrackPlayer/PlayerCar" method="_on_TileMap_player_moved"] [connection signal="wrong_way" from="TileMap" to="Wrong Way" method="_on_TileMap_wrong_way"] [editable path="GUI"]