From edc4b5630db7c88b8617dcf33462cb811d5e715f Mon Sep 17 00:00:00 2001 From: Artus Date: Fri, 30 Oct 2020 15:23:13 +0100 Subject: [PATCH] putting together in Main scene --- CarSprite.gd | 15 ++---- Countdown.gd | 32 +++++++++++++ Countdown.tscn | 23 +++++++++ Global.gd | 10 ++++ Main.gd | 18 +++++++ Main.tscn | 23 +++++++++ Menu.gd | 16 +++++++ Menu.tscn | 67 ++++++++++++++++++++++++++ OpponentCar.gd | 10 ++-- RaceTrack.gd | 120 +++++++++++++++-------------------------------- RaceTrack.tscn | 119 ++++++++++++++++++++++++++++------------------ Track2.tscn | 57 ++++++---------------- TrackOpponent.gd | 14 ++++++ TrackPath.gd | 83 ++++++++++++++++++++++++++++++++ Wrong Way.gd | 5 +- font.tres | 9 ++++ 16 files changed, 433 insertions(+), 188 deletions(-) create mode 100644 Countdown.gd create mode 100644 Countdown.tscn create mode 100644 Global.gd create mode 100644 Main.gd create mode 100644 Main.tscn create mode 100644 Menu.gd create mode 100644 Menu.tscn create mode 100644 TrackOpponent.gd create mode 100644 TrackPath.gd create mode 100644 font.tres diff --git a/CarSprite.gd b/CarSprite.gd index cd03113..f94a134 100644 --- a/CarSprite.gd +++ b/CarSprite.gd @@ -2,25 +2,25 @@ extends AnimatedSprite enum { RIGHT, DOWN_RIGHT, DOWN, DOWN_LEFT, LEFT, UP_LEFT, UP, UP_RIGHT } -var sprite := UP export (int) var threshold = 5 # Range to snap around axis direction, in degrees. +export (String, "bus", "police", "car") var skin = "car" +var sprite := UP -var time = 0.0 # Called when the node enters the scene tree for the first time. func _ready(): - pass # Replace with function body. + set_animation(skin) func _process(delta): """ The sprite has its own representation of rotation, so we need to tweak things. - The sprite inherits orientation from its parent (track) that must be canceled. + The sprite inherits orientation from its parent (TrackFollow) that must be canceled. """ var global = get_global_rotation_degrees() var local = get_rotation_degrees() var total = wrapf(global - local, -180.0, 180.0) var r = snap_rotation(total) if _update_sprite(r): - print_debug("frame changed") + #print_debug("frame changed") set_frame(sprite) # We can just cancel out the "part of" the rotation # embedded in the sprites, leaving out a small @@ -28,11 +28,6 @@ func _process(delta): #set_rotation_degrees(-r) # ...Or we just cancel out everything set_rotation_degrees(-total) - - get_node("/root/Track2/HBoxContainer/Rotation").set_text("%.1f°" % r) - time += delta - if time >= 0.1: - print_debug("%.1f" % get_rotation_degrees(), "° | global: ", "%.1f" % get_global_rotation_degrees(), "°") func snap_rotation(r): diff --git a/Countdown.gd b/Countdown.gd new file mode 100644 index 0000000..c7ca5b7 --- /dev/null +++ b/Countdown.gd @@ -0,0 +1,32 @@ +extends Node2D + +signal race_started + +export (int) var delay = 3 +var timer + + +func _ready(): + # Set up a timer + timer = Timer.new() + add_child(timer) + timer.wait_time = 1.0 + timer.connect("timeout", self, "_timeout") + timer.start() + + $Number.set_text("%d" % delay) + + +func _timeout(): + #print("Timeout !") + delay -= 1 + if delay < 0: + timer.stop() + set_visible(false) + elif delay == 0: + # Go ! + $Number.set_text("GO") + emit_signal("race_started") + else: + $Number.set_text("%d" % delay) + \ No newline at end of file diff --git a/Countdown.tscn b/Countdown.tscn new file mode 100644 index 0000000..498aeed --- /dev/null +++ b/Countdown.tscn @@ -0,0 +1,23 @@ +[gd_scene load_steps=4 format=2] + +[ext_resource path="res://Countdown.gd" type="Script" id=1] +[ext_resource path="res://Oswald-Bold.otf" type="DynamicFontData" id=2] + +[sub_resource type="DynamicFont" id=1] +size = 255 +outline_size = 20 +outline_color = Color( 0.145098, 0.145098, 0.145098, 1 ) +use_filter = true +font_data = ExtResource( 2 ) + +[node name="Countdown" type="Node2D"] +script = ExtResource( 1 ) +delay = 5 + +[node name="Number" type="Label" parent="."] +margin_right = 1920.0 +margin_bottom = 1080.0 +custom_fonts/font = SubResource( 1 ) +text = "3" +align = 1 +valign = 1 diff --git a/Global.gd b/Global.gd new file mode 100644 index 0000000..09cec67 --- /dev/null +++ b/Global.gd @@ -0,0 +1,10 @@ +extends Node + +const TRACKS = [ + [ 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 selected_track \ No newline at end of file diff --git a/Main.gd b/Main.gd new file mode 100644 index 0000000..0c0b6a9 --- /dev/null +++ b/Main.gd @@ -0,0 +1,18 @@ +extends Node + +# Declare member variables here. Examples: +# var a = 2 +# var b = "text" + +# Called when the node enters the scene tree for the first time. +func _ready(): + Global.selected_track = 0 + + +func start_game(): + pass + + +func change_track(): + """ Change the race track """ + pass \ No newline at end of file diff --git a/Main.tscn b/Main.tscn new file mode 100644 index 0000000..dc936d4 --- /dev/null +++ b/Main.tscn @@ -0,0 +1,23 @@ +[gd_scene load_steps=7 format=2] + +[ext_resource path="res://Main.gd" type="Script" id=1] +[ext_resource path="res://Menu.tscn" type="PackedScene" id=2] +[ext_resource path="res://RaceTrack.tscn" type="PackedScene" id=3] +[ext_resource path="res://PlayerCar.tscn" type="PackedScene" id=4] +[ext_resource path="res://Gui.tscn" type="PackedScene" id=5] +[ext_resource path="res://Countdown.tscn" type="PackedScene" id=6] + +[node name="Main" type="Node"] +script = ExtResource( 1 ) + +[node name="Menu" parent="." instance=ExtResource( 2 )] + +[node name="RaceTrack" parent="." instance=ExtResource( 3 )] + +[node name="PlayerCar" parent="RaceTrack" instance=ExtResource( 4 )] + +[node name="GUI" parent="." instance=ExtResource( 5 )] +visible = false + +[node name="Countdown" parent="." instance=ExtResource( 6 )] +visible = false diff --git a/Menu.gd b/Menu.gd new file mode 100644 index 0000000..9c21c0d --- /dev/null +++ b/Menu.gd @@ -0,0 +1,16 @@ +extends CanvasLayer + +signal track_changed(track_idx) +signal skin_changed(skin) + +# Declare member variables here. Examples: +# var a = 2 +# var b = "text" + +# Called when the node enters the scene tree for the first time. +func _ready(): + pass # Replace with function body. + +# Called every frame. 'delta' is the elapsed time since the previous frame. +#func _process(delta): +# pass diff --git a/Menu.tscn b/Menu.tscn new file mode 100644 index 0000000..4dbcecc --- /dev/null +++ b/Menu.tscn @@ -0,0 +1,67 @@ +[gd_scene load_steps=5 format=2] + +[ext_resource path="res://Menu.gd" type="Script" id=1] +[ext_resource path="res://Oswald-Bold.otf" type="DynamicFontData" id=2] +[ext_resource path="res://font.tres" type="DynamicFont" id=3] + +[sub_resource type="DynamicFont" id=1] +resource_name = "BaseFont" +size = 60 +use_filter = true +font_data = ExtResource( 2 ) + +[node name="Menu" type="CanvasLayer"] +script = ExtResource( 1 ) + +[node name="Title" type="Label" parent="."] +margin_left = 701.404 +margin_top = 169.251 +margin_right = 944.404 +margin_bottom = 252.251 +rect_scale = Vector2( 2, 2 ) +custom_fonts/font = SubResource( 1 ) +custom_colors/font_color = Color( 0.219608, 0.137255, 0.709804, 1 ) +custom_colors/font_color_shadow = Color( 0.423529, 0.2, 0.0352941, 1 ) +custom_constants/shadow_offset_x = 2 +custom_constants/shadow_offset_y = 3 +custom_constants/shadow_as_outline = 0 +text = "Car Racer" + +[node name="ButtonBox" type="VBoxContainer" parent="."] +margin_left = 800.375 +margin_top = 448.991 +margin_right = 949.375 +margin_bottom = 654.991 +rect_scale = Vector2( 2, 2 ) + +[node name="Jouer" type="Label" parent="ButtonBox"] +margin_right = 149.0 +margin_bottom = 66.0 +custom_fonts/font = ExtResource( 3 ) +custom_colors/font_color_shadow = Color( 0.454902, 0.219608, 0.0901961, 1 ) +custom_constants/shadow_offset_x = 2 +custom_constants/shadow_offset_y = 3 +text = "Jouer" +align = 1 + +[node name="Options" type="Label" parent="ButtonBox"] +margin_top = 70.0 +margin_right = 149.0 +margin_bottom = 136.0 +custom_fonts/font = ExtResource( 3 ) +custom_colors/font_color_shadow = Color( 0.454902, 0.219608, 0.0901961, 1 ) +custom_constants/shadow_offset_x = 2 +custom_constants/shadow_offset_y = 3 +text = "Options" +align = 1 + +[node name="Quitter" type="Label" parent="ButtonBox"] +margin_top = 140.0 +margin_right = 149.0 +margin_bottom = 206.0 +custom_fonts/font = ExtResource( 3 ) +custom_colors/font_color_shadow = Color( 0.454902, 0.219608, 0.0901961, 1 ) +custom_constants/shadow_offset_x = 2 +custom_constants/shadow_offset_y = 3 +text = "Quitter" +align = 1 diff --git a/OpponentCar.gd b/OpponentCar.gd index 7355c72..0d441ff 100644 --- a/OpponentCar.gd +++ b/OpponentCar.gd @@ -1,16 +1,14 @@ -extends Node2D +extends PathFollow2D -onready var track = get_node("/root/Track2/RaceTrack/TrackOpponent/") export (float) var speed = 0.125 - # Called when the node enters the scene tree for the first time. func _ready(): - $CarSprite.set_animation("police") + pass # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta): - var position = track.get_unit_offset() + var position = get_unit_offset() position += speed * delta - track.set_unit_offset(position) + set_unit_offset(position) diff --git a/RaceTrack.gd b/RaceTrack.gd index aa3738a..9349f1e 100644 --- a/RaceTrack.gd +++ b/RaceTrack.gd @@ -1,86 +1,44 @@ -extends Path2D +extends Node -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), -] +signal player_moved(track_offset) +signal lap_completed(laps) +signal wrong_way(coords) + +# TODO: +# - Find a mean to import tracks (arrays of cell coordinates). +# ( - Autopopulate the map according to track data ) +# - Build the track Curve2D from track and tiles data +# - Keyboard controls !! + +const TRACK_TILES = Global.TRACKS[0] + +onready var map = $TileMap +var laps = 0 +var current_cell = TRACK_TILES[0] + +func get_track_offset(coords): + var offset = float(TRACK_TILES.find(coords)) / float(len(TRACK_TILES)) + return offset -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) + pass # Replace with function body. -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 +func _input(event): + # Check if the mouse if following the tiles track + if event is InputEventMouseMotion: + var hover_cell = map.world_to_map(event.position) + if hover_cell != current_cell: # The mouse moved to a new cell + # Check the tile is on path + if TRACK_TILES.find(hover_cell) != -1: + # Check if a lap is finished + if hover_cell == TRACK_TILES[0] and current_cell == TRACK_TILES[-1]: + laps += 1 + emit_signal("lap_completed", laps) + # Check we are following the path + if TRACK_TILES[TRACK_TILES.find(hover_cell) - 1] == current_cell: + emit_signal("player_moved", laps + get_track_offset(hover_cell)) + emit_signal("wrong_way", Vector2(-1, -1)) + current_cell = hover_cell + else: + emit_signal("wrong_way", map.map_to_world(hover_cell)) diff --git a/RaceTrack.tscn b/RaceTrack.tscn index 094add3..e1dd3df 100644 --- a/RaceTrack.tscn +++ b/RaceTrack.tscn @@ -1,72 +1,99 @@ -[gd_scene load_steps=30 format=2] +[gd_scene load_steps=34 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] +[ext_resource path="res://TrackPath.gd" type="Script" 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://assets/2D_Car_Pack_DevilsWorkShop_V01/car02/car02iso_0003.png" type="Texture" id=12] +[ext_resource path="res://assets/2D_Car_Pack_DevilsWorkShop_V01/car02/car02iso_0004.png" type="Texture" id=13] +[ext_resource path="res://assets/2D_Car_Pack_DevilsWorkShop_V01/car02/car02iso_0001.png" type="Texture" id=14] +[ext_resource path="res://assets/2D_Car_Pack_DevilsWorkShop_V01/car02/car02iso_0002.png" type="Texture" id=15] +[ext_resource path="res://assets/2D_Car_Pack_DevilsWorkShop_V01/car02/car02iso_0007.png" type="Texture" id=16] +[ext_resource path="res://assets/2D_Car_Pack_DevilsWorkShop_V01/car02/car02iso_0000.png" type="Texture" id=17] +[ext_resource path="res://assets/2D_Car_Pack_DevilsWorkShop_V01/car02/car02iso_0005.png" type="Texture" id=18] +[ext_resource path="res://assets/2D_Car_Pack_DevilsWorkShop_V01/car02/car02iso_0006.png" type="Texture" id=19] +[ext_resource path="res://track1/policeCar/policeiso_0003.png" type="Texture" id=20] +[ext_resource path="res://track1/policeCar/policeiso_0004.png" type="Texture" id=21] +[ext_resource path="res://track1/policeCar/policeiso_0001.png" type="Texture" id=22] +[ext_resource path="res://track1/policeCar/policeiso_0002.png" type="Texture" id=23] +[ext_resource path="res://track1/policeCar/policeiso_0007.png" type="Texture" id=24] +[ext_resource path="res://track1/policeCar/policeiso_0000.png" type="Texture" id=25] +[ext_resource path="res://track1/policeCar/policeiso_0005.png" type="Texture" id=26] +[ext_resource path="res://track1/policeCar/policeiso_0006.png" type="Texture" id=27] +[ext_resource path="res://CarSprite.gd" type="Script" id=28] +[ext_resource path="res://wrong_way.tscn" type="PackedScene" id=29] -[sub_resource type="Curve2D" id=6] +[sub_resource type="Gradient" id=3] +colors = PoolColorArray( 0.284025, 0.4375, 0.0803223, 1, 0.30305, 0.480469, 0.0675659, 1 ) + +[sub_resource type="GradientTexture" id=4] +gradient = SubResource( 3 ) +width = 1920 + +[sub_resource type="Curve2D" id=1] _data = { -"points": PoolVector2Array( ) +"points": PoolVector2Array( 0, 0, 0, 0, -17.3556, 327.792, 0, 0, 0, 0, 1930.1, 452.427 ) } [sub_resource type="SpriteFrames" id=2] animations = [ { -"frames": [ ExtResource( 3 ), ExtResource( 4 ), ExtResource( 5 ), ExtResource( 6 ), ExtResource( 7 ), ExtResource( 8 ), ExtResource( 9 ), ExtResource( 10 ) ], +"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( 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 ) ], +"frames": [ ExtResource( 12 ), ExtResource( 13 ), ExtResource( 14 ), ExtResource( 15 ), ExtResource( 16 ), ExtResource( 17 ), ExtResource( 18 ), ExtResource( 19 ) ], "loop": true, "name": "car", "speed": 5.0 +}, { +"frames": [ ExtResource( 20 ), ExtResource( 21 ), ExtResource( 22 ), ExtResource( 23 ), ExtResource( 24 ), ExtResource( 25 ), ExtResource( 26 ), ExtResource( 27 ) ], +"loop": true, +"name": "police", +"speed": 5.0 } ] -[node name="RaceTrack" type="Path2D"] -curve = SubResource( 6 ) +[node name="RaceTrack" type="Node"] script = ExtResource( 1 ) -[node name="TrackOpponent" type="PathFollow2D" parent="."] -position = Vector2( 928.559, 184.278 ) -rotation = -0.013337 +[node name="TextureRect" type="TextureRect" parent="."] +margin_right = 1920.0 +margin_bottom = 1080.0 +texture = SubResource( 4 ) +expand = true -[node name="OpponentCar" type="Node2D" parent="TrackOpponent"] -position = Vector2( 0, 22 ) -scale = Vector2( 0.25, 0.25 ) +[node name="TileMap" type="TileMap" parent="."] +cell_size = Vector2( 128, 128 ) +format = 1 + +[node name="TrackPath" type="Path2D" parent="."] +curve = SubResource( 1 ) script = ExtResource( 2 ) -[node name="CarSprite" type="AnimatedSprite" parent="TrackOpponent/OpponentCar"] +[node name="Opponent" type="PathFollow2D" parent="TrackPath"] +position = Vector2( -17.3556, 327.792 ) +rotation = 0.0639077 +script = ExtResource( 3 ) + +[node name="OpponentCar" type="Node2D" parent="TrackPath/Opponent"] +position = Vector2( 0, 22 ) +scale = Vector2( 0.25, 0.25 ) + +[node name="CarSprite" type="AnimatedSprite" parent="TrackPath/Opponent/OpponentCar"] position = Vector2( -3.20422, 3.0376 ) scale = Vector2( 0.8, 0.8 ) frames = SubResource( 2 ) animation = "car" -script = ExtResource( 27 ) +script = ExtResource( 28 ) +skin = "bus" + +[node name="Wrong Way" parent="." instance=ExtResource( 29 )] +visible = false +[connection signal="wrong_way" from="." to="Wrong Way" method="_on_RaceTrack_wrong_way"] diff --git a/Track2.tscn b/Track2.tscn index 3ed5887..186f0fb 100644 --- a/Track2.tscn +++ b/Track2.tscn @@ -1,6 +1,6 @@ -[gd_scene load_steps=13 format=2] +[gd_scene load_steps=12 format=2] -[ext_resource path="res://TileMap2.gd" type="Script" id=1] +[ext_resource path="res://TileMap.gd" type="Script" id=1] [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] @@ -9,18 +9,13 @@ [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="Gradient" id=3] +[sub_resource type="Gradient" id=1] colors = PoolColorArray( 0.242729, 0.410156, 0.128174, 1, 0.088892, 0.507812, 0.0753784, 1 ) -[sub_resource type="GradientTexture" id=4] -gradient = SubResource( 3 ) +[sub_resource type="GradientTexture" id=2] +gradient = SubResource( 1 ) -[sub_resource type="DynamicFont" id=5] +[sub_resource type="DynamicFont" id=3] size = 13 outline_size = 2 outline_color = Color( 0.254902, 0.121569, 0.121569, 1 ) @@ -38,55 +33,31 @@ 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 +position = Vector2( -17.3556, 327.792 ) +rotation = 0.0639077 [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="CanvasLayer" type="CanvasLayer" parent="."] +[node name="TrackImage" type="CanvasLayer" parent="."] layer = -1 -[node name="TextureRect" type="TextureRect" parent="CanvasLayer"] +[node name="TextureRect" type="TextureRect" parent="TrackImage"] margin_right = 1923.0 margin_bottom = 1084.0 texture = ExtResource( 4 ) -[node name="CanvasLayer" type="CanvasLayer" parent="CanvasLayer"] +[node name="CanvasLayer" type="CanvasLayer" parent="TrackImage"] layer = -1 -[node name="TextureRect2" type="TextureRect" parent="CanvasLayer/CanvasLayer"] +[node name="TextureRect2" type="TextureRect" parent="TrackImage/CanvasLayer"] margin_right = 1923.0 margin_bottom = 1080.0 -texture = SubResource( 4 ) +texture = SubResource( 2 ) expand = true -[node name="HBoxContainer" type="HBoxContainer" parent="."] -margin_left = 11.2102 -margin_top = 57.7727 -margin_right = 159.21 -margin_bottom = 107.773 - -[node name="Label" type="Label" parent="HBoxContainer"] -margin_top = 18.0 -margin_right = 53.0 -margin_bottom = 32.0 -text = "Rotation" - -[node name="Rotation" type="Label" parent="HBoxContainer"] -margin_left = 57.0 -margin_top = 18.0 -margin_right = 71.0 -margin_bottom = 32.0 -text = "0°" - [node name="GUI" parent="." instance=ExtResource( 5 )] editor/display_folded = false script = ExtResource( 6 ) @@ -98,7 +69,7 @@ margin_right = 920.258 margin_bottom = 431.587 rect_rotation = -5.0 rect_clip_content = true -custom_fonts/font = SubResource( 5 ) +custom_fonts/font = SubResource( 3 ) custom_colors/font_color = Color( 0.780392, 0.129412, 0.129412, 1 ) text = "WRONG WAY !!" align = 1 diff --git a/TrackOpponent.gd b/TrackOpponent.gd new file mode 100644 index 0000000..2b595e1 --- /dev/null +++ b/TrackOpponent.gd @@ -0,0 +1,14 @@ +extends PathFollow2D + +export (float) var speed = 0.125 + +# Called when the node enters the scene tree for the first time. +func _ready(): + pass + +# Called every frame. 'delta' is the elapsed time since the previous frame. +func _process(delta): + var position = get_unit_offset() + position += speed * delta + set_unit_offset(position) + \ No newline at end of file diff --git a/TrackPath.gd b/TrackPath.gd new file mode 100644 index 0000000..a702bb3 --- /dev/null +++ b/TrackPath.gd @@ -0,0 +1,83 @@ +extends Path2D + +enum { UP, RIGHT, DOWN, LEFT } +const TRACK_TILES = Global.TRACKS[0] + +var tile_size : int = 128 +var half_size : int = tile_size / 2 +onready var map = get_node("../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.chartreuse, 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/Wrong Way.gd b/Wrong Way.gd index b901bce..f86db69 100644 --- a/Wrong Way.gd +++ b/Wrong Way.gd @@ -4,9 +4,10 @@ extends Label func _ready(): set_visible(false) -func _on_TileMap_wrong_way(coords): + +func _on_RaceTrack_wrong_way(coords): if coords == Vector2(-1,-1): set_visible(false) else: set_visible(true) - set_position(coords) + set_position(coords) \ No newline at end of file diff --git a/font.tres b/font.tres new file mode 100644 index 0000000..db0eb3d --- /dev/null +++ b/font.tres @@ -0,0 +1,9 @@ +[gd_resource type="DynamicFont" load_steps=2 format=2] + +[ext_resource path="res://Oswald-Bold.otf" type="DynamicFontData" id=1] + +[resource] +resource_name = "BaseFont" +size = 48 +use_filter = true +font_data = ExtResource( 1 )