diff options
Diffstat (limited to 'Racing-Game/4WheelCar')
| -rw-r--r-- | Racing-Game/4WheelCar/4WheelCar.tscn | 1 | ||||
| -rw-r--r-- | Racing-Game/4WheelCar/BackWheel.gd | 44 | ||||
| -rw-r--r-- | Racing-Game/4WheelCar/CarBody.gd | 56 | ||||
| -rw-r--r-- | Racing-Game/4WheelCar/FrontWheel.gd | 38 | ||||
| -rw-r--r-- | Racing-Game/4WheelCar/Skid/BLLine.gd | 25 | ||||
| -rw-r--r-- | Racing-Game/4WheelCar/Skid/BRLine.gd | 30 | ||||
| -rw-r--r-- | Racing-Game/4WheelCar/Skid/FLLine.gd | 25 | ||||
| -rw-r--r-- | Racing-Game/4WheelCar/Skid/FRLine.gd | 25 | ||||
| -rw-r--r-- | Racing-Game/4WheelCar/Skid/SkidController.gd | 2 | ||||
| -rw-r--r-- | Racing-Game/4WheelCar/Skid/Skidmarks.gd | 8 |
10 files changed, 53 insertions, 201 deletions
diff --git a/Racing-Game/4WheelCar/4WheelCar.tscn b/Racing-Game/4WheelCar/4WheelCar.tscn index 958cc39..81fef02 100644 --- a/Racing-Game/4WheelCar/4WheelCar.tscn +++ b/Racing-Game/4WheelCar/4WheelCar.tscn @@ -6,7 +6,6 @@ [ext_resource path="res://4WheelCar/Skid/SkidController.gd" type="Script" id=4] [ext_resource path="res://4WheelCar/BackWheel.gd" type="Script" id=5] - [sub_resource type="RectangleShape2D" id=1] [sub_resource type="RectangleShape2D" id=2] diff --git a/Racing-Game/4WheelCar/BackWheel.gd b/Racing-Game/4WheelCar/BackWheel.gd index b5d8297..9c9adff 100644 --- a/Racing-Game/4WheelCar/BackWheel.gd +++ b/Racing-Game/4WheelCar/BackWheel.gd @@ -7,26 +7,18 @@ var isForward = true var velocity -var lastLoc = 0; - -var wheelSlip = Vector2(0,0); var isSkid = false; -signal slip -signal end var elapsed = 0 -# + var gripDelay = 0 var carAngle var carVector var velVector -# Called when the node enters the scene tree for the first time. func _ready(): - lastLoc = get_position_in_parent() - pass # Replace with function body. + pass -# Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta): carAngle = get_node("../../../CarBody").get_transform().get_rotation() carVector = Vector2(cos(carAngle + PI/2),sin(carAngle + PI/2)) @@ -34,18 +26,17 @@ func _process(delta): velocity = measure_velocity() isForward = is_forward() set_rotation(carAngle) - isSlip(delta) # gripDelay = has_grip(0.4,delta) isSkid = Input.is_action_pressed("grip") - if Input.is_action_pressed("grip") || Input.is_action_pressed("break"): + if Input.is_action_pressed("grip") || Input.is_action_pressed("brake"): null_slide(max(5,velocity/7),delta) else: null_slide(1,delta) #Braking - if Input.is_action_pressed("break"): + if Input.is_action_pressed("brake"): if velocity > 20: linear_damp = 2 else: @@ -53,20 +44,19 @@ func _process(delta): else: linear_damp = 0.01 -# createLine(lastLoc, get_position_in_parent()) -# lastLoc = get_position_in_parent() if Input.is_action_pressed("forward"): - if !Input.is_action_pressed("break"): + 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("break"): + if !Input.is_action_pressed("brake"): apply_central_impulse(Vector2(0,(1)).rotated(carAngle)*delta*5000) else: pass +#Core physics, stops sliding sideways and make car roll forwards func null_slide(var strength, var delta): #strength is how strong you would like the nullify to be #higher is less sliding/drifting @@ -74,20 +64,9 @@ func null_slide(var strength, var delta): 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 + var wheelSlip = (-(movementUnitVector - nullify))*strength apply_central_impulse(wheelSlip*delta*5000) - -func isSlip(time): - if (wheelSlip.length() > 0.2): - emit_signal("slip") - else: - emit_signal("end") -#func createLine(from, to): -# var tires = get_node("./MyLine") -# tires.add_point(from) -# tires.add_point(to) - #func has_grip(var tractionDelay, var delta): # var movementUnitVector = get_linear_velocity().normalized()#the direction of the velocity # var directionAngle = carAngle#the angle the car is facing(relative to the world) @@ -107,9 +86,10 @@ func isSlip(time): func measure_velocity(): return sqrt(get_linear_velocity().dot(get_linear_velocity()))/12 - -func is_forward():#determines if the car is driving forward, or backward - var carVector = Vector2(cos(carAngle + PI/2),sin(carAngle + PI/2)) + +#determines if the car is driving forward, or backward +func is_forward(): + 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: diff --git a/Racing-Game/4WheelCar/CarBody.gd b/Racing-Game/4WheelCar/CarBody.gd index 179381c..be06de1 100644 --- a/Racing-Game/4WheelCar/CarBody.gd +++ b/Racing-Game/4WheelCar/CarBody.gd @@ -1,61 +1,41 @@ extends RigidBody2D -# Declare member variables here. Examples: -# var a = 2 -# var b = "text" -# Called when the node enters the scene tree for the first time. -var angular = 0; -var directionAngle -var directionUnitVector +var angular = 0 +var isSkidding = 0 var cameraNode var speedometerNode -var isSkidding = 0 -var frWheel -var flWheel -var brWheel -var blWheel + +var directionAngle +var directionUnitVector func _ready(): - frWheel = get_node("./Engine/Wheels/FRWheel") - flWheel = get_node("./Engine/Wheels/FLWheel") - brWheel = get_node("./Engine/Wheels/BRWheel") - blWheel = get_node("./Engine/Wheels/BLWheel") -# Called every frame. 'delta' is the elapsed time since the previous frame. -func _process(delta): + pass + +func _process(_delta): directionAngle = get_transform().get_rotation() + (PI/2.0)#the angle the car is facing(relative to the world) directionUnitVector = Vector2(cos(directionAngle),sin(directionAngle))#the direction the car is facing - linear_damp = 0.01 - angular = 5 - - if(isSkidding > 0): - frWheel.isSkidOverride = true - flWheel.isSkidOverride = true - brWheel.isSkidOverride = true - blWheel.isSkidOverride = true - frWheel.isSkid = true - flWheel.isSkid = true - brWheel.isSkid = true - blWheel.isSkid = true - #emit_signal("speedometer", measure_forward_velocity()) + linear_damp = 0.01 #Sets forward momentum dampening + angular = 5 #Sets the base angular momentum dampening + + #HUD Stuff: cameraNode = get_node("/root/World/Camera2D") speedometerNode = get_node("/root/World/Camera2D/Panel/Speedometer") cameraNode.set_position(get_global_transform().get_origin()) speedometerNode.updateSpeed(floor(measure_velocity())) + #Steering strength at different speeds, should probably look at this and remove it(move to wheels) + #See issue #8 if measure_velocity() < 30: - angular_damp = angular*1 + angular_damp = angular*0.2 elif measure_velocity() < 85: angular_damp = angular*0.4 else: - angular_damp = angular*0.05 - + angular_damp = angular*1 - +#Total speed of the car func measure_velocity(): return sqrt(get_linear_velocity().dot(get_linear_velocity()))/12 +#Gets only the component that is going in the direction of the car func measure_forward_velocity(): return floor(measure_velocity() * cos(directionUnitVector.angle_to(get_linear_velocity()))) - -func forceSkidMarks(skid): - isSkidding += skid diff --git a/Racing-Game/4WheelCar/FrontWheel.gd b/Racing-Game/4WheelCar/FrontWheel.gd index d4e5af4..76eaad0 100644 --- a/Racing-Game/4WheelCar/FrontWheel.gd +++ b/Racing-Game/4WheelCar/FrontWheel.gd @@ -1,20 +1,19 @@ extends RigidBody2D -# Declare member variables here. Examples: -# var a = 2 -# var b = "text" -var velocity -var velVector -var velUnitVector -var velAngle -var carAngle -var delay = 0 +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) +#Skidmarks on floor onready var skidObj = preload("res://4WheelCar/Skid/Skidmark.tscn") +#Reduces steering strength when braking +var steerDampBase = 1 var steerDamp = 1 -#Steering +#Steering Curve Vars var steerSplitA = 20 var steerSplitB = 40 var steerHeight = 2.6 @@ -24,8 +23,8 @@ var steerMinimum = 1 var gripDelay = 0 var wheelSlip = Vector2(0,0) -var isSkid = false;#this one is used when user presses shift. Initially called in this function -var isSkidOverride = false;#this one is used when driving over sand, initially called in carbody +var isSkid = false #this one is used when user presses shift. Initially called in this function +var isSkidOverride = false #this one is used when driving over sand, initially called in carbody signal slip signal end @@ -40,13 +39,15 @@ func _ready(): # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta): #Variable Setup + #--- velocity = measure_velocity() velVector = get_node("../../../CarBody").get_linear_velocity() velUnitVector = velVector.normalized() velAngle = atan2(velVector.y,velVector.x) carAngle = get_node("../../../CarBody").get_transform().get_rotation() - set_rotation(carAngle) isForward = is_forward() + #--- + set_rotation(carAngle) # gripDelay = has_grip(0.4,delta) isSlip(delta) @@ -54,13 +55,13 @@ func _process(delta): isSkid = Input.is_action_pressed("grip") #Determines if drifting - if Input.is_action_pressed("grip") || Input.is_action_pressed("break"): + if Input.is_action_pressed("grip") || Input.is_action_pressed("brake"): null_slide(max(5,velocity/7),delta) else: null_slide(1,delta) #Braking - if Input.is_action_pressed("break"): + if Input.is_action_pressed("brake"): if velocity > 20: linear_damp = 3 steerDamp = 0.7 @@ -107,6 +108,7 @@ func null_slide(var strength, var delta): # gripDelay = 0 # return gripDelay +#Determines if skidmarks should be creted, or stopped func isSlip(time): if (wheelSlip.length() > 0.6): if(elapsed/4 > time): @@ -120,7 +122,8 @@ func isSlip(time): func measure_velocity(): return floor(sqrt(get_linear_velocity().dot(get_linear_velocity()))/12) -func is_forward():#determines if the car is driving forward, or backward +#determines if the car is driving forward, or backward +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 @@ -129,13 +132,14 @@ func is_forward():#determines if the car is driving forward, or backward else: return false +#returns the angle the car is facing, relative to the direction it is moving func steer_angle(): - #returns the angle the car is facing, relative to the direcction it is moving 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 diff --git a/Racing-Game/4WheelCar/Skid/BLLine.gd b/Racing-Game/4WheelCar/Skid/BLLine.gd deleted file mode 100644 index 042b034..0000000 --- a/Racing-Game/4WheelCar/Skid/BLLine.gd +++ /dev/null @@ -1,25 +0,0 @@ -extends Line2D - -# Declare member variables here. Examples: -# var a = 2 -# var b = "text" -var tempnode = 0 -var on = true - -# Called when the node enters the scene tree for the first time. -func _ready(): - tempnode = get_node("../Cars/4WheelCar/CarBody/Wheels/BLWheel") - pass # Replace with function body. - -# Called every frame. 'delta' is the elapsed time since the previous frame. -func _process(delta): - pass - -func _on_BLWheel_slip(): - if(on): - add_point(tempnode.get_global_position()) - - -func _on_BLWheel_end(): - duplicate(1) - on = false diff --git a/Racing-Game/4WheelCar/Skid/BRLine.gd b/Racing-Game/4WheelCar/Skid/BRLine.gd deleted file mode 100644 index 16578e3..0000000 --- a/Racing-Game/4WheelCar/Skid/BRLine.gd +++ /dev/null @@ -1,30 +0,0 @@ -extends Line2D - -# Declare member variables here. Examples: -# var a = 2 -# var b = "text" -var tempnode = 0 -var on = true - -# Called when the node enters the scene tree for the first time. -func _ready(): - print("ready") - tempnode = get_node("../Cars/4WheelCar/CarBody/Wheels/BRWheel") - clear_points() - pass # Replace with function body. - -# Called every frame. 'delta' is the elapsed time since the previous frame. -func _process(delta): - pass - -func _on_BRWheel_slip(): - while on: - print("slip2") - add_point(tempnode.get_global_position()) - - -func _on_BRWheel_end(): - on = false - print("duplicated") - var duplication = duplicate(13) - duplication.request_ready() diff --git a/Racing-Game/4WheelCar/Skid/FLLine.gd b/Racing-Game/4WheelCar/Skid/FLLine.gd deleted file mode 100644 index fffcf04..0000000 --- a/Racing-Game/4WheelCar/Skid/FLLine.gd +++ /dev/null @@ -1,25 +0,0 @@ -extends Line2D - -# Declare member variables here. Examples: -# var a = 2 -# var b = "text" -var tempnode = 0 -var on = true - -# Called when the node enters the scene tree for the first time. -func _ready(): - tempnode = get_node("../Cars/4WheelCar/CarBody/Wheels/FLWheel") - pass # Replace with function body. - -# Called every frame. 'delta' is the elapsed time since the previous frame. -func _process(delta): - pass - -func _on_FLWheel_slip(): - if(on): - add_point(tempnode.get_global_position()) - - -func _on_FLWheel_end(): - duplicate(1) - on = false diff --git a/Racing-Game/4WheelCar/Skid/FRLine.gd b/Racing-Game/4WheelCar/Skid/FRLine.gd deleted file mode 100644 index e1fbc0f..0000000 --- a/Racing-Game/4WheelCar/Skid/FRLine.gd +++ /dev/null @@ -1,25 +0,0 @@ -extends Line2D - -# Declare member variables here. Examples: -# var a = 2 -# var b = "text" -var tempnode = 0 -var on = true - -# Called when the node enters the scene tree for the first time. -func _ready(): - tempnode = get_node("../Cars/4WheelCar/CarBody/Wheels/FRWheel") - pass # Replace with function body. - -# Called every frame. 'delta' is the elapsed time since the previous frame. -func _process(delta): - pass - -func _on_FRWheel_slip(): - if(on): - add_point(tempnode.get_global_position()) - - -func _on_FRWheel_end(): - duplicate(1) - on = false diff --git a/Racing-Game/4WheelCar/Skid/SkidController.gd b/Racing-Game/4WheelCar/Skid/SkidController.gd index b1d8233..7c633c9 100644 --- a/Racing-Game/4WheelCar/Skid/SkidController.gd +++ b/Racing-Game/4WheelCar/Skid/SkidController.gd @@ -16,7 +16,7 @@ func _ready(): add_child(skidRecent) # Called every frame. 'delta' is the elapsed time since the previous frame. -func _process(delta): +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 diff --git a/Racing-Game/4WheelCar/Skid/Skidmarks.gd b/Racing-Game/4WheelCar/Skid/Skidmarks.gd index 4c662b4..07bf622 100644 --- a/Racing-Game/4WheelCar/Skid/Skidmarks.gd +++ b/Racing-Game/4WheelCar/Skid/Skidmarks.gd @@ -1,20 +1,14 @@ extends Line2D -# Declare member variables here. Examples: -# var a = 2 -# var b = "text" var skidDraw = true var wheel var carBody -# Called when the node enters the scene tree for the first time. func _ready(): wheel = get_parent().get_parent() carBody = get_parent().get_parent().get_parent().get_parent() - -# Called every frame. 'delta' is the elapsed time since the previous frame. -func _process(delta): +func _process(_delta): global_position = Vector2(0,0) global_rotation = 0 if(skidDraw): |
