From c7e8f17ba13d4c9e54764286367a26f25b049dc4 Mon Sep 17 00:00:00 2001 From: realtradam Date: Thu, 19 Mar 2020 03:32:54 -0400 Subject: fixed up file structure --- Racing-Game/4WheelCar/4WheelCar.tscn | 9 +-- Racing-Game/4WheelCar/BackWheel.gd | 50 ------------- Racing-Game/4WheelCar/BackWheel.tscn | 28 -------- Racing-Game/4WheelCar/FrontWheel.gd | 84 ---------------------- Racing-Game/4WheelCar/FrontWheel.tscn | 28 -------- Racing-Game/4WheelCar/Skid/SkidController.gd | 36 ---------- Racing-Game/4WheelCar/Skid/Skidmark.tscn | 11 --- Racing-Game/4WheelCar/Skid/Skidmarks.gd | 15 ---- Racing-Game/4WheelCar/Weight.gd | 26 ------- Racing-Game/4WheelCar/Weight.tscn | 23 ------ Racing-Game/4WheelCar/Weight/Weight.gd | 26 +++++++ Racing-Game/4WheelCar/Weight/Weight.tscn | 24 +++++++ Racing-Game/4WheelCar/Weight/WeightCreation.gd | 23 ++++++ Racing-Game/4WheelCar/WeightCreation.gd | 23 ------ Racing-Game/4WheelCar/Wheel.gd | 63 ---------------- Racing-Game/4WheelCar/WheelCreation.gd | 35 --------- Racing-Game/4WheelCar/Wheels/BackWheel.gd | 50 +++++++++++++ Racing-Game/4WheelCar/Wheels/BackWheel.tscn | 29 ++++++++ Racing-Game/4WheelCar/Wheels/FrontWheel.gd | 84 ++++++++++++++++++++++ Racing-Game/4WheelCar/Wheels/FrontWheel.tscn | 29 ++++++++ .../4WheelCar/Wheels/Skid/SkidController.gd | 36 ++++++++++ Racing-Game/4WheelCar/Wheels/Skid/Skidmark.tscn | 12 ++++ Racing-Game/4WheelCar/Wheels/Skid/Skidmarks.gd | 15 ++++ Racing-Game/4WheelCar/Wheels/Wheel.gd | 63 ++++++++++++++++ Racing-Game/4WheelCar/Wheels/WheelCreation.gd | 35 +++++++++ 25 files changed, 431 insertions(+), 426 deletions(-) delete mode 100644 Racing-Game/4WheelCar/BackWheel.gd delete mode 100644 Racing-Game/4WheelCar/BackWheel.tscn delete mode 100644 Racing-Game/4WheelCar/FrontWheel.gd delete mode 100644 Racing-Game/4WheelCar/FrontWheel.tscn delete mode 100644 Racing-Game/4WheelCar/Skid/SkidController.gd delete mode 100644 Racing-Game/4WheelCar/Skid/Skidmark.tscn delete mode 100644 Racing-Game/4WheelCar/Skid/Skidmarks.gd delete mode 100644 Racing-Game/4WheelCar/Weight.gd delete mode 100644 Racing-Game/4WheelCar/Weight.tscn create mode 100644 Racing-Game/4WheelCar/Weight/Weight.gd create mode 100644 Racing-Game/4WheelCar/Weight/Weight.tscn create mode 100644 Racing-Game/4WheelCar/Weight/WeightCreation.gd delete mode 100644 Racing-Game/4WheelCar/WeightCreation.gd delete mode 100644 Racing-Game/4WheelCar/Wheel.gd delete mode 100644 Racing-Game/4WheelCar/WheelCreation.gd create mode 100644 Racing-Game/4WheelCar/Wheels/BackWheel.gd create mode 100644 Racing-Game/4WheelCar/Wheels/BackWheel.tscn create mode 100644 Racing-Game/4WheelCar/Wheels/FrontWheel.gd create mode 100644 Racing-Game/4WheelCar/Wheels/FrontWheel.tscn create mode 100644 Racing-Game/4WheelCar/Wheels/Skid/SkidController.gd create mode 100644 Racing-Game/4WheelCar/Wheels/Skid/Skidmark.tscn create mode 100644 Racing-Game/4WheelCar/Wheels/Skid/Skidmarks.gd create mode 100644 Racing-Game/4WheelCar/Wheels/Wheel.gd create mode 100644 Racing-Game/4WheelCar/Wheels/WheelCreation.gd diff --git a/Racing-Game/4WheelCar/4WheelCar.tscn b/Racing-Game/4WheelCar/4WheelCar.tscn index ff5d89f..a6e289a 100644 --- a/Racing-Game/4WheelCar/4WheelCar.tscn +++ b/Racing-Game/4WheelCar/4WheelCar.tscn @@ -2,11 +2,12 @@ [ext_resource path="res://4WheelCar/CarBody.gd" type="Script" id=1] [ext_resource path="res://Art(non-orig)/PNG/Cars/big/blue/car_blue_2.png" type="Texture" id=2] -[ext_resource path="res://4WheelCar/WeightCreation.gd" type="Script" id=4] +[ext_resource path="res://4WheelCar/Weight/WeightCreation.gd" type="Script" id=4] [ext_resource path="res://4WheelCar/CarProperties.gd" type="Script" id=6] -[ext_resource path="res://4WheelCar/WheelCreation.gd" type="Script" id=9] +[ext_resource path="res://4WheelCar/Wheels/WheelCreation.gd" type="Script" id=9] -[sub_resource type="RectangleShape2D" id=2] + +[sub_resource type="RectangleShape2D" id=1] extents = Vector2( 35.7301, 58.1399 ) [node name="4WheelCar" type="Node"] @@ -24,7 +25,7 @@ texture = ExtResource( 2 ) [node name="CollisionShape2D" type="CollisionShape2D" parent="CarBody"] visible = false modulate = Color( 0, 0, 0, 1 ) -shape = SubResource( 2 ) +shape = SubResource( 1 ) [node name="Wheels" type="Node" parent="CarBody"] script = ExtResource( 9 ) diff --git a/Racing-Game/4WheelCar/BackWheel.gd b/Racing-Game/4WheelCar/BackWheel.gd deleted file mode 100644 index 1d6ee18..0000000 --- a/Racing-Game/4WheelCar/BackWheel.gd +++ /dev/null @@ -1,50 +0,0 @@ -extends "res://4WheelCar/Wheel.gd" - -#Accelerating -var hp = 4.0 -var acceleration = 5 - -var gripDelay = 0 - -func _ready(): - pass - -func _process(delta): - #Variable Setup - #---null_slide vars - nullStrength = 0 - carAngle = get_node("../../../CarBody").get_transform().get_rotation() - velVector = get_node("../../../CarBody").get_linear_velocity() - velocity = measure_velocity() - #--- - set_rotation(carAngle) - - isSkid = Input.is_action_pressed("grip") - - if Input.is_action_pressed("grip") || Input.is_action_pressed("brake"): - nullStrength += max(5,velocity/7) - else: - nullStrength += 1 - - #Braking - setBrake(0) - null_slide(nullStrength, delta) - - - if Input.is_action_pressed("forward"): - if !Input.is_action_pressed("brake"): - apply_central_impulse(Vector2(0,-gear(velocity, hp, acceleration)).rotated(carAngle)*delta*5000) - else: - pass - elif Input.is_action_pressed("backward"): - if !Input.is_action_pressed("brake"): - apply_central_impulse(Vector2(0,(1)).rotated(carAngle)*delta*5000) - else: - pass - -func measure_velocity(): - return sqrt(get_linear_velocity().dot(get_linear_velocity()))/12 - - -func gear(var rpm, var maxPower, var topSpeed): - return max(-pow((rpm/((1000/topSpeed)/sqrt(maxPower)))-sqrt(maxPower),2)+(maxPower+1),0) diff --git a/Racing-Game/4WheelCar/BackWheel.tscn b/Racing-Game/4WheelCar/BackWheel.tscn deleted file mode 100644 index e493d24..0000000 --- a/Racing-Game/4WheelCar/BackWheel.tscn +++ /dev/null @@ -1,28 +0,0 @@ -[gd_scene load_steps=5 format=2] - -[ext_resource path="res://4WheelCar/Skid/SkidController.gd" type="Script" id=1] -[ext_resource path="res://4WheelCar/BackWheel.gd" type="Script" id=2] -[ext_resource path="res://Art(non-orig)/crosshair(isorig).png" type="Texture" id=3] - -[sub_resource type="RectangleShape2D" id=1] -extents = Vector2( 7.07375, 14.3787 ) - -[node name="BackWheel" type="RigidBody2D"] -z_index = 2 -mass = 15.0 -linear_damp = 0.4 -angular_damp = 7.04 -script = ExtResource( 2 ) -__meta__ = { -"_edit_group_": true -} - -[node name="CollisionShape2D" type="CollisionShape2D" parent="."] -shape = SubResource( 1 ) -disabled = true - -[node name="SkidController2" type="Node" parent="."] -script = ExtResource( 1 ) - -[node name="Sprite" type="Sprite" parent="."] -texture = ExtResource( 3 ) diff --git a/Racing-Game/4WheelCar/FrontWheel.gd b/Racing-Game/4WheelCar/FrontWheel.gd deleted file mode 100644 index 7731bca..0000000 --- a/Racing-Game/4WheelCar/FrontWheel.gd +++ /dev/null @@ -1,84 +0,0 @@ -extends "res://4WheelCar/Wheel.gd" - -#Reduces steering strength when braking -var steerDamp = 1 - -#Steering Curve Vars -var steerSplitA = 20 -var steerSplitB = 40 -var steerHeight = 2.6 -var steerLimit = 73 -var steerMinimum = 1 - -# 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): - #Variable Update - #---null_slide vars - nullStrength = 0 - velocity = measure_velocity() - velVector = get_node("../../../CarBody").get_linear_velocity() - carAngle = get_node("../../../CarBody").get_transform().get_rotation() - #---Steering Vars - velAngle = atan2(velVector.y,velVector.x) - isForward = is_forward() - #--- - set_rotation(carAngle) - - isSkid = Input.is_action_pressed("grip") - - #Determines if drifting - if Input.is_action_pressed("grip") || Input.is_action_pressed("brake"): - nullStrength += max(5,velocity/7) - else: - nullStrength += 1 - - #Braking - setBrake(0) - null_slide(nullStrength,delta) - - #Steering - if Input.is_action_pressed("steer_left"): - apply_central_impulse(steerDamp*Vector2(0,steer_curve(steerSplitA, steerSplitB, steerHeight, steerLimit,steerMinimum)).rotated(steer_angle())*delta*5000) - if Input.is_action_pressed("steer_right"): - apply_central_impulse(steerDamp*Vector2(0,-steer_curve(steerSplitA, steerSplitB, steerHeight, steerLimit,steerMinimum)).rotated(steer_angle())*delta*5000) - -#returns the angle the car is facing, relative to the direction it is moving -func steer_angle(): - if isForward: - return carAngle + (PI/2.0) - else: - return carAngle - (PI/2.0) - -#Determines strength of steering as a function of the speed -func steer_curve(var splitA, splitB, var height, var limit, var minimum): - #Rules: - # splitA < splitB < limit - # height > 0, limit >= 0 - # --- - #Desmos: SteerCurve - #Link: https://www.desmos.com/calculator/jkrd8zzoj9 - # splitA = a - # splitB = b - # height = h - # limit = f - #note: minimum is not in the graph, it is simply the minimum y value you want when x > splitB - # --- - if(velocity >= splitB): - return max((-pow((velocity-splitB)/((limit-splitB)/sqrt(height)),2)+height)*abs(cos(abs(velAngle-carAngle)+PI/2)),minimum) - elif velocity >= splitA: - return height - else: - return max((-pow((velocity/(splitA/sqrt(height)))-sqrt(height),2)+height)*abs(cos(abs(velAngle-carAngle)+PI/2)),0) - -func is_forward(): - var carVector = Vector2(cos(carAngle + PI/2),sin(carAngle + PI/2)) - if velVector == Vector2(0,0) || carVector == Vector2(0,0): - return true - if velVector.dot(carVector) <= 0: - return true - else: - return false diff --git a/Racing-Game/4WheelCar/FrontWheel.tscn b/Racing-Game/4WheelCar/FrontWheel.tscn deleted file mode 100644 index 330e147..0000000 --- a/Racing-Game/4WheelCar/FrontWheel.tscn +++ /dev/null @@ -1,28 +0,0 @@ -[gd_scene load_steps=5 format=2] - -[ext_resource path="res://4WheelCar/Skid/SkidController.gd" type="Script" id=1] -[ext_resource path="res://4WheelCar/FrontWheel.gd" type="Script" id=2] -[ext_resource path="res://Art(non-orig)/crosshair(isorig).png" type="Texture" id=3] - -[sub_resource type="RectangleShape2D" id=1] -extents = Vector2( 6.93714, 14.1584 ) - -[node name="FrontWheel" type="RigidBody2D"] -z_index = 2 -mass = 15.0 -linear_damp = 0.4 -angular_damp = 7.04 -script = ExtResource( 2 ) -__meta__ = { -"_edit_group_": true -} - -[node name="CollisionShape2D" type="CollisionShape2D" parent="."] -shape = SubResource( 1 ) -disabled = true - -[node name="SkidController4" type="Node" parent="."] -script = ExtResource( 1 ) - -[node name="Sprite" type="Sprite" parent="."] -texture = ExtResource( 3 ) diff --git a/Racing-Game/4WheelCar/Skid/SkidController.gd b/Racing-Game/4WheelCar/Skid/SkidController.gd deleted file mode 100644 index e3b28c3..0000000 --- a/Racing-Game/4WheelCar/Skid/SkidController.gd +++ /dev/null @@ -1,36 +0,0 @@ -extends Node - -# Declare member variables here. Examples: -# var a = 2 -# var b = "text" -var skidSwitch = false # Is the wheel currantly skidding? -var skidFile # Stores the skid file -var skidRecent # Holds the most recent skidmark - -var deleteme = 0; - -# Called when the node enters the scene tree for the first time. -func _ready(): - skidFile = load("res://4WheelCar/Skid/Skidmark.tscn") - skidRecent = skidFile.instance() - add_child(skidRecent) - -# Called every frame. 'delta' is the elapsed time since the previous frame. -func _process(_delta): - if(skidSwitch && get_parent().isSkid):#if controller believes car is skidding and the wheel is skidding - #ignore the status and let the skid keep drawing - pass - elif(!skidSwitch && get_parent().isSkid):#controller doesnt skid, but wheel is supposed to be skidding - # create new skidding child, and set skid to true - skidSwitch = true - skidFile = load("res://4WheelCar/Skid/Skidmark.tscn") - skidRecent = skidFile.instance() - add_child(skidRecent) - #set to run - pass - elif(!get_parent().isSkid): - #stop child from skidding and store - skidRecent.skidDraw = false - skidSwitch = false - pass - diff --git a/Racing-Game/4WheelCar/Skid/Skidmark.tscn b/Racing-Game/4WheelCar/Skid/Skidmark.tscn deleted file mode 100644 index 17ecee0..0000000 --- a/Racing-Game/4WheelCar/Skid/Skidmark.tscn +++ /dev/null @@ -1,11 +0,0 @@ -[gd_scene load_steps=3 format=2] - -[ext_resource path="res://4WheelCar/Skid/Skidmarks.gd" type="Script" id=1] -[ext_resource path="res://Art(non-orig)/PNG/Objects/skidmark_short_1.png" type="Texture" id=2] - -[node name="Skidmark" type="Line2D"] -position = Vector2( -21.2326, -38.4176 ) -default_color = Color( 0.4, 0.501961, 1, 1 ) -texture = ExtResource( 2 ) -texture_mode = 192263440 -script = ExtResource( 1 ) diff --git a/Racing-Game/4WheelCar/Skid/Skidmarks.gd b/Racing-Game/4WheelCar/Skid/Skidmarks.gd deleted file mode 100644 index 07bf622..0000000 --- a/Racing-Game/4WheelCar/Skid/Skidmarks.gd +++ /dev/null @@ -1,15 +0,0 @@ -extends Line2D - -var skidDraw = true -var wheel -var carBody - -func _ready(): - wheel = get_parent().get_parent() - carBody = get_parent().get_parent().get_parent().get_parent() - -func _process(_delta): - global_position = Vector2(0,0) - global_rotation = 0 - if(skidDraw): - add_point(wheel.get_global_position()) diff --git a/Racing-Game/4WheelCar/Weight.gd b/Racing-Game/4WheelCar/Weight.gd deleted file mode 100644 index ea9e615..0000000 --- a/Racing-Game/4WheelCar/Weight.gd +++ /dev/null @@ -1,26 +0,0 @@ -extends RigidBody2D - - -# 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 - -func initVars(dataArray): -# #---Creating and attaching pinjoint to wheel - mass = dataArray[2] - var tempA = PinJoint2D.new() - tempA.set_position(Vector2(dataArray[0],dataArray[1])) - print(get_parent().get_parent().get_path()) - tempA.set_node_a(get_path()) - tempA.set_node_b(get_parent().get_parent().get_path()) - tempA.softness = 0 - tempA.bias = 0 - tempA.disable_collision = true - get_parent().add_child(tempA) -# #--- - pass diff --git a/Racing-Game/4WheelCar/Weight.tscn b/Racing-Game/4WheelCar/Weight.tscn deleted file mode 100644 index 4735229..0000000 --- a/Racing-Game/4WheelCar/Weight.tscn +++ /dev/null @@ -1,23 +0,0 @@ -[gd_scene load_steps=4 format=2] - -[ext_resource path="res://Art(non-orig)/crosshair(isorig).png" type="Texture" id=1] -[ext_resource path="res://4WheelCar/Weight.gd" type="Script" id=2] - -[sub_resource type="RectangleShape2D" id=1] - -[node name="Weight" type="RigidBody2D"] -position = Vector2( -0.446396, 25.4176 ) -z_index = 2 -mass = 10.0 -script = ExtResource( 2 ) - -[node name="CollisionShape2D" type="CollisionShape2D" parent="."] -z_index = 2 -shape = SubResource( 1 ) -disabled = true -__meta__ = { -"_edit_group_": true -} - -[node name="Sprite" type="Sprite" parent="."] -texture = ExtResource( 1 ) diff --git a/Racing-Game/4WheelCar/Weight/Weight.gd b/Racing-Game/4WheelCar/Weight/Weight.gd new file mode 100644 index 0000000..ea9e615 --- /dev/null +++ b/Racing-Game/4WheelCar/Weight/Weight.gd @@ -0,0 +1,26 @@ +extends RigidBody2D + + +# 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 + +func initVars(dataArray): +# #---Creating and attaching pinjoint to wheel + mass = dataArray[2] + var tempA = PinJoint2D.new() + tempA.set_position(Vector2(dataArray[0],dataArray[1])) + print(get_parent().get_parent().get_path()) + tempA.set_node_a(get_path()) + tempA.set_node_b(get_parent().get_parent().get_path()) + tempA.softness = 0 + tempA.bias = 0 + tempA.disable_collision = true + get_parent().add_child(tempA) +# #--- + pass diff --git a/Racing-Game/4WheelCar/Weight/Weight.tscn b/Racing-Game/4WheelCar/Weight/Weight.tscn new file mode 100644 index 0000000..094f257 --- /dev/null +++ b/Racing-Game/4WheelCar/Weight/Weight.tscn @@ -0,0 +1,24 @@ +[gd_scene load_steps=4 format=2] + +[ext_resource path="res://4WheelCar/Weight/Weight.gd" type="Script" id=1] +[ext_resource path="res://Art(non-orig)/crosshair(isorig).png" type="Texture" id=2] + + +[sub_resource type="RectangleShape2D" id=1] + +[node name="Weight" type="RigidBody2D"] +position = Vector2( -0.446396, 25.4176 ) +z_index = 2 +mass = 10.0 +script = ExtResource( 1 ) + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +z_index = 2 +shape = SubResource( 1 ) +disabled = true +__meta__ = { +"_edit_group_": true +} + +[node name="Sprite" type="Sprite" parent="."] +texture = ExtResource( 2 ) diff --git a/Racing-Game/4WheelCar/Weight/WeightCreation.gd b/Racing-Game/4WheelCar/Weight/WeightCreation.gd new file mode 100644 index 0000000..290325c --- /dev/null +++ b/Racing-Game/4WheelCar/Weight/WeightCreation.gd @@ -0,0 +1,23 @@ +extends Node + + +# This script creates and attaches all "masses". This makes it possible +# to simulate unbalanced weight distribution in a vehicle + + +# Called when the node enters the scene tree for the first time. +func _ready(): + createWeight([0,30,30]) + pass # Replace with function body. + + +# Called every frame. 'delta' is the elapsed time since the previous frame. +#func _process(delta): +# pass + +func createWeight(dataArray): + var weighttemp_resource = load("res://4WheelCar/Weight/Weight.tscn") + var weighttemp = weighttemp_resource.instance() + weighttemp.set_position(Vector2(dataArray[0],dataArray[1])) + add_child(weighttemp) + weighttemp.initVars(dataArray) diff --git a/Racing-Game/4WheelCar/WeightCreation.gd b/Racing-Game/4WheelCar/WeightCreation.gd deleted file mode 100644 index 6d6f607..0000000 --- a/Racing-Game/4WheelCar/WeightCreation.gd +++ /dev/null @@ -1,23 +0,0 @@ -extends Node - - -# This script creates and attaches all "masses". This makes it possible -# to simulate unbalanced weight distribution in a vehicle - - -# Called when the node enters the scene tree for the first time. -func _ready(): - createWeight([0,30,30]) - pass # Replace with function body. - - -# Called every frame. 'delta' is the elapsed time since the previous frame. -#func _process(delta): -# pass - -func createWeight(dataArray): - var weighttemp_resource = load("res://4WheelCar/Weight.tscn") - var weighttemp = weighttemp_resource.instance() - weighttemp.set_position(Vector2(dataArray[0],dataArray[1])) - add_child(weighttemp) - weighttemp.initVars(dataArray) diff --git a/Racing-Game/4WheelCar/Wheel.gd b/Racing-Game/4WheelCar/Wheel.gd deleted file mode 100644 index 49024fb..0000000 --- a/Racing-Game/4WheelCar/Wheel.gd +++ /dev/null @@ -1,63 +0,0 @@ -extends RigidBody2D - -var velocity #How fast car is moving -var velVector #What direction the car is moving -var velUnitVector #Direction vector, but in a single unit(no magnitude) -var velAngle #The angle of the velocity(relative to world) -var carAngle #The angle to car is facing(relative to world) -var nullStrength #sums up all sources of null to here -var isSkid = false #this one is used when user presses shift. Initially called in this function - -#Skidmarks on floor - -#var gripDelay = 0 - -var wheelSlip = Vector2(0,0) - -var isForward = true - -# 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): - pass - -func initVars(dataArray): -# #---Creating and attaching pinjoint to wheel - var tempA = PinJoint2D.new() - tempA.set_position(Vector2(dataArray[0],dataArray[1])) - tempA.set_node_a(get_path()) - tempA.set_node_b(get_parent().get_parent().get_path()) - tempA.softness = 0 - tempA.bias = 0 - tempA.disable_collision = true - get_parent().add_child(tempA) -# #--- - pass - -func null_slide(var strength, var delta): - #strength is how strong you would like the nullify to be - #higher is less sliding/drifting - var movementUnitVector = get_linear_velocity().normalized()#the direction of the velocity - var directionAngle = carAngle + (PI/2.0)#the angle the car is facing(relative to the world) - var directionUnitVector = Vector2(cos(directionAngle),sin(directionAngle)).normalized()#the direction the car is facing - var nullify = directionUnitVector * movementUnitVector.dot(directionUnitVector) - wheelSlip = (-(movementUnitVector - nullify))*strength - apply_central_impulse(wheelSlip*delta*5000) - -#checks if the car is braking, and applies brake physics -func setBrake(var strength): - #Braking - if Input.is_action_pressed("brake"): - if velocity > 20: - linear_damp = 3 - else: - linear_damp = 6 - else: - linear_damp = 0.01 - - -func measure_velocity(): - return floor(sqrt(get_linear_velocity().dot(get_linear_velocity()))/12) diff --git a/Racing-Game/4WheelCar/WheelCreation.gd b/Racing-Game/4WheelCar/WheelCreation.gd deleted file mode 100644 index 6be1a26..0000000 --- a/Racing-Game/4WheelCar/WheelCreation.gd +++ /dev/null @@ -1,35 +0,0 @@ -extends Node - - -# This script creates and attaches all the wheels. Depending on the variables -# given to this script, it can generate different type of vehicles - - -# Called when the node enters the scene tree for the first time. -func _ready(): - createWheel("front", [17,-27]) - createWheel("front", [-17,-27]) - createWheel("back", [20,36]) - createWheel("back", [-20,36]) - pass # Replace with function body. - - -# Called every frame. 'delta' is the elapsed time since the previous frame. -#func _process(delta): -# pass - -func createWheel(type,dataArray): - if(type == "front"): - var wheeltemp_resource = load("res://4WheelCar/FrontWheel.tscn") - var wheeltemp = wheeltemp_resource.instance() - wheeltemp.set_position(Vector2(dataArray[0],dataArray[1])) - add_child(wheeltemp) - wheeltemp.initVars(dataArray) - -# add_child(tempA) - elif(type == "back"): - var wheeltemp_resource = load("res://4WheelCar/BackWheel.tscn") - var wheeltemp = wheeltemp_resource.instance() - wheeltemp.set_position(Vector2(dataArray[0],dataArray[1])) - add_child(wheeltemp) - wheeltemp.initVars(dataArray) diff --git a/Racing-Game/4WheelCar/Wheels/BackWheel.gd b/Racing-Game/4WheelCar/Wheels/BackWheel.gd new file mode 100644 index 0000000..88df113 --- /dev/null +++ b/Racing-Game/4WheelCar/Wheels/BackWheel.gd @@ -0,0 +1,50 @@ +extends "res://4WheelCar/Wheels/Wheel.gd" + +#Accelerating +var hp = 4.0 +var acceleration = 5 + +var gripDelay = 0 + +func _ready(): + pass + +func _process(delta): + #Variable Setup + #---null_slide vars + nullStrength = 0 + carAngle = get_node("../../../CarBody").get_transform().get_rotation() + velVector = get_node("../../../CarBody").get_linear_velocity() + velocity = measure_velocity() + #--- + set_rotation(carAngle) + + isSkid = Input.is_action_pressed("grip") + + if Input.is_action_pressed("grip") || Input.is_action_pressed("brake"): + nullStrength += max(5,velocity/7) + else: + nullStrength += 1 + + #Braking + setBrake(0) + null_slide(nullStrength, delta) + + + if Input.is_action_pressed("forward"): + if !Input.is_action_pressed("brake"): + apply_central_impulse(Vector2(0,-gear(velocity, hp, acceleration)).rotated(carAngle)*delta*5000) + else: + pass + elif Input.is_action_pressed("backward"): + if !Input.is_action_pressed("brake"): + apply_central_impulse(Vector2(0,(1)).rotated(carAngle)*delta*5000) + else: + pass + +func measure_velocity(): + return sqrt(get_linear_velocity().dot(get_linear_velocity()))/12 + + +func gear(var rpm, var maxPower, var topSpeed): + return max(-pow((rpm/((1000/topSpeed)/sqrt(maxPower)))-sqrt(maxPower),2)+(maxPower+1),0) diff --git a/Racing-Game/4WheelCar/Wheels/BackWheel.tscn b/Racing-Game/4WheelCar/Wheels/BackWheel.tscn new file mode 100644 index 0000000..c09eaca --- /dev/null +++ b/Racing-Game/4WheelCar/Wheels/BackWheel.tscn @@ -0,0 +1,29 @@ +[gd_scene load_steps=5 format=2] + +[ext_resource path="res://4WheelCar/Wheels/Skid/SkidController.gd" type="Script" id=1] +[ext_resource path="res://4WheelCar/Wheels/BackWheel.gd" type="Script" id=2] +[ext_resource path="res://Art(non-orig)/crosshair(isorig).png" type="Texture" id=3] + + +[sub_resource type="RectangleShape2D" id=1] +extents = Vector2( 7.07375, 14.3787 ) + +[node name="BackWheel" type="RigidBody2D"] +z_index = 2 +mass = 15.0 +linear_damp = 0.4 +angular_damp = 7.04 +script = ExtResource( 2 ) +__meta__ = { +"_edit_group_": true +} + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource( 1 ) +disabled = true + +[node name="SkidController2" type="Node" parent="."] +script = ExtResource( 1 ) + +[node name="Sprite" type="Sprite" parent="."] +texture = ExtResource( 3 ) diff --git a/Racing-Game/4WheelCar/Wheels/FrontWheel.gd b/Racing-Game/4WheelCar/Wheels/FrontWheel.gd new file mode 100644 index 0000000..7a05522 --- /dev/null +++ b/Racing-Game/4WheelCar/Wheels/FrontWheel.gd @@ -0,0 +1,84 @@ +extends "res://4WheelCar/Wheels/Wheel.gd" + +#Reduces steering strength when braking +var steerDamp = 1 + +#Steering Curve Vars +var steerSplitA = 20 +var steerSplitB = 40 +var steerHeight = 2.6 +var steerLimit = 73 +var steerMinimum = 1 + +# 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): + #Variable Update + #---null_slide vars + nullStrength = 0 + velocity = measure_velocity() + velVector = get_node("../../../CarBody").get_linear_velocity() + carAngle = get_node("../../../CarBody").get_transform().get_rotation() + #---Steering Vars + velAngle = atan2(velVector.y,velVector.x) + isForward = is_forward() + #--- + set_rotation(carAngle) + + isSkid = Input.is_action_pressed("grip") + + #Determines if drifting + if Input.is_action_pressed("grip") || Input.is_action_pressed("brake"): + nullStrength += max(5,velocity/7) + else: + nullStrength += 1 + + #Braking + setBrake(0) + null_slide(nullStrength,delta) + + #Steering + if Input.is_action_pressed("steer_left"): + apply_central_impulse(steerDamp*Vector2(0,steer_curve(steerSplitA, steerSplitB, steerHeight, steerLimit,steerMinimum)).rotated(steer_angle())*delta*5000) + if Input.is_action_pressed("steer_right"): + apply_central_impulse(steerDamp*Vector2(0,-steer_curve(steerSplitA, steerSplitB, steerHeight, steerLimit,steerMinimum)).rotated(steer_angle())*delta*5000) + +#returns the angle the car is facing, relative to the direction it is moving +func steer_angle(): + if isForward: + return carAngle + (PI/2.0) + else: + return carAngle - (PI/2.0) + +#Determines strength of steering as a function of the speed +func steer_curve(var splitA, splitB, var height, var limit, var minimum): + #Rules: + # splitA < splitB < limit + # height > 0, limit >= 0 + # --- + #Desmos: SteerCurve + #Link: https://www.desmos.com/calculator/jkrd8zzoj9 + # splitA = a + # splitB = b + # height = h + # limit = f + #note: minimum is not in the graph, it is simply the minimum y value you want when x > splitB + # --- + if(velocity >= splitB): + return max((-pow((velocity-splitB)/((limit-splitB)/sqrt(height)),2)+height)*abs(cos(abs(velAngle-carAngle)+PI/2)),minimum) + elif velocity >= splitA: + return height + else: + return max((-pow((velocity/(splitA/sqrt(height)))-sqrt(height),2)+height)*abs(cos(abs(velAngle-carAngle)+PI/2)),0) + +func is_forward(): + var carVector = Vector2(cos(carAngle + PI/2),sin(carAngle + PI/2)) + if velVector == Vector2(0,0) || carVector == Vector2(0,0): + return true + if velVector.dot(carVector) <= 0: + return true + else: + return false diff --git a/Racing-Game/4WheelCar/Wheels/FrontWheel.tscn b/Racing-Game/4WheelCar/Wheels/FrontWheel.tscn new file mode 100644 index 0000000..25644a6 --- /dev/null +++ b/Racing-Game/4WheelCar/Wheels/FrontWheel.tscn @@ -0,0 +1,29 @@ +[gd_scene load_steps=5 format=2] + +[ext_resource path="res://4WheelCar/Wheels/Skid/SkidController.gd" type="Script" id=1] +[ext_resource path="res://4WheelCar/Wheels/FrontWheel.gd" type="Script" id=2] +[ext_resource path="res://Art(non-orig)/crosshair(isorig).png" type="Texture" id=3] + + +[sub_resource type="RectangleShape2D" id=1] +extents = Vector2( 6.93714, 14.1584 ) + +[node name="FrontWheel" type="RigidBody2D"] +z_index = 2 +mass = 15.0 +linear_damp = 0.4 +angular_damp = 7.04 +script = ExtResource( 2 ) +__meta__ = { +"_edit_group_": true +} + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource( 1 ) +disabled = true + +[node name="SkidController4" type="Node" parent="."] +script = ExtResource( 1 ) + +[node name="Sprite" type="Sprite" parent="."] +texture = ExtResource( 3 ) diff --git a/Racing-Game/4WheelCar/Wheels/Skid/SkidController.gd b/Racing-Game/4WheelCar/Wheels/Skid/SkidController.gd new file mode 100644 index 0000000..df1e054 --- /dev/null +++ b/Racing-Game/4WheelCar/Wheels/Skid/SkidController.gd @@ -0,0 +1,36 @@ +extends Node + +# Declare member variables here. Examples: +# var a = 2 +# var b = "text" +var skidSwitch = false # Is the wheel currantly skidding? +var skidFile # Stores the skid file +var skidRecent # Holds the most recent skidmark + +var deleteme = 0; + +# Called when the node enters the scene tree for the first time. +func _ready(): + skidFile = load("res://4WheelCar/Wheels/Skid/Skidmark.tscn") + skidRecent = skidFile.instance() + add_child(skidRecent) + +# Called every frame. 'delta' is the elapsed time since the previous frame. +func _process(_delta): + if(skidSwitch && get_parent().isSkid):#if controller believes car is skidding and the wheel is skidding + #ignore the status and let the skid keep drawing + pass + elif(!skidSwitch && get_parent().isSkid):#controller doesnt skid, but wheel is supposed to be skidding + # create new skidding child, and set skid to true + skidSwitch = true + skidFile = load("res://4WheelCar/Wheels/Skid/Skidmark.tscn") + skidRecent = skidFile.instance() + add_child(skidRecent) + #set to run + pass + elif(!get_parent().isSkid): + #stop child from skidding and store + skidRecent.skidDraw = false + skidSwitch = false + pass + diff --git a/Racing-Game/4WheelCar/Wheels/Skid/Skidmark.tscn b/Racing-Game/4WheelCar/Wheels/Skid/Skidmark.tscn new file mode 100644 index 0000000..865add0 --- /dev/null +++ b/Racing-Game/4WheelCar/Wheels/Skid/Skidmark.tscn @@ -0,0 +1,12 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://4WheelCar/Wheels/Skid/Skidmarks.gd" type="Script" id=1] +[ext_resource path="res://Art(non-orig)/PNG/Objects/skidmark_short_1.png" type="Texture" id=2] + + +[node name="Skidmark" type="Line2D"] +position = Vector2( -21.2326, -38.4176 ) +default_color = Color( 0.4, 0.501961, 1, 1 ) +texture = ExtResource( 2 ) +texture_mode = 192263440 +script = ExtResource( 1 ) diff --git a/Racing-Game/4WheelCar/Wheels/Skid/Skidmarks.gd b/Racing-Game/4WheelCar/Wheels/Skid/Skidmarks.gd new file mode 100644 index 0000000..07bf622 --- /dev/null +++ b/Racing-Game/4WheelCar/Wheels/Skid/Skidmarks.gd @@ -0,0 +1,15 @@ +extends Line2D + +var skidDraw = true +var wheel +var carBody + +func _ready(): + wheel = get_parent().get_parent() + carBody = get_parent().get_parent().get_parent().get_parent() + +func _process(_delta): + global_position = Vector2(0,0) + global_rotation = 0 + if(skidDraw): + add_point(wheel.get_global_position()) diff --git a/Racing-Game/4WheelCar/Wheels/Wheel.gd b/Racing-Game/4WheelCar/Wheels/Wheel.gd new file mode 100644 index 0000000..49024fb --- /dev/null +++ b/Racing-Game/4WheelCar/Wheels/Wheel.gd @@ -0,0 +1,63 @@ +extends RigidBody2D + +var velocity #How fast car is moving +var velVector #What direction the car is moving +var velUnitVector #Direction vector, but in a single unit(no magnitude) +var velAngle #The angle of the velocity(relative to world) +var carAngle #The angle to car is facing(relative to world) +var nullStrength #sums up all sources of null to here +var isSkid = false #this one is used when user presses shift. Initially called in this function + +#Skidmarks on floor + +#var gripDelay = 0 + +var wheelSlip = Vector2(0,0) + +var isForward = true + +# 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): + pass + +func initVars(dataArray): +# #---Creating and attaching pinjoint to wheel + var tempA = PinJoint2D.new() + tempA.set_position(Vector2(dataArray[0],dataArray[1])) + tempA.set_node_a(get_path()) + tempA.set_node_b(get_parent().get_parent().get_path()) + tempA.softness = 0 + tempA.bias = 0 + tempA.disable_collision = true + get_parent().add_child(tempA) +# #--- + pass + +func null_slide(var strength, var delta): + #strength is how strong you would like the nullify to be + #higher is less sliding/drifting + var movementUnitVector = get_linear_velocity().normalized()#the direction of the velocity + var directionAngle = carAngle + (PI/2.0)#the angle the car is facing(relative to the world) + var directionUnitVector = Vector2(cos(directionAngle),sin(directionAngle)).normalized()#the direction the car is facing + var nullify = directionUnitVector * movementUnitVector.dot(directionUnitVector) + wheelSlip = (-(movementUnitVector - nullify))*strength + apply_central_impulse(wheelSlip*delta*5000) + +#checks if the car is braking, and applies brake physics +func setBrake(var strength): + #Braking + if Input.is_action_pressed("brake"): + if velocity > 20: + linear_damp = 3 + else: + linear_damp = 6 + else: + linear_damp = 0.01 + + +func measure_velocity(): + return floor(sqrt(get_linear_velocity().dot(get_linear_velocity()))/12) diff --git a/Racing-Game/4WheelCar/Wheels/WheelCreation.gd b/Racing-Game/4WheelCar/Wheels/WheelCreation.gd new file mode 100644 index 0000000..db09f4b --- /dev/null +++ b/Racing-Game/4WheelCar/Wheels/WheelCreation.gd @@ -0,0 +1,35 @@ +extends Node + + +# This script creates and attaches all the wheels. Depending on the variables +# given to this script, it can generate different type of vehicles + + +# Called when the node enters the scene tree for the first time. +func _ready(): + createWheel("front", [17,-27]) + createWheel("front", [-17,-27]) + createWheel("back", [20,36]) + createWheel("back", [-20,36]) + pass # Replace with function body. + + +# Called every frame. 'delta' is the elapsed time since the previous frame. +#func _process(delta): +# pass + +func createWheel(type,dataArray): + if(type == "front"): + var wheeltemp_resource = load("res://4WheelCar/Wheels/FrontWheel.tscn") + var wheeltemp = wheeltemp_resource.instance() + wheeltemp.set_position(Vector2(dataArray[0],dataArray[1])) + add_child(wheeltemp) + wheeltemp.initVars(dataArray) + +# add_child(tempA) + elif(type == "back"): + var wheeltemp_resource = load("res://4WheelCar/Wheels/BackWheel.tscn") + var wheeltemp = wheeltemp_resource.instance() + wheeltemp.set_position(Vector2(dataArray[0],dataArray[1])) + add_child(wheeltemp) + wheeltemp.initVars(dataArray) -- cgit v1.2.3