101 lines
2.8 KiB
GDScript
101 lines
2.8 KiB
GDScript
extends Path3D
|
|
##TODO Make it so error is raised if more than one func_type is selected
|
|
##Make editor shape dissapear when game runs and make editor shape mimic expected shape
|
|
## Add more functions
|
|
## Make it so steps generate an invisible collision area over them
|
|
## to allow for ease of walking up steps
|
|
|
|
@export_flags("Curve", "Steps") var use_case
|
|
@export_category("Steps")
|
|
@export var steps: int = 10
|
|
@export var step_height: float = 1
|
|
@export var invisible_slope: bool = false
|
|
@export_category("Curve")
|
|
@export var slope: float = 1
|
|
@export var offset: float = 0
|
|
@export var log_base: int = 10
|
|
@export var start_point: int = 0
|
|
@export var end_point: int = 2
|
|
@export_flags("Linear", "Quadratic", "Logarithmic", "Absolute") var func_type
|
|
var y: float = 0
|
|
var x: float = 0
|
|
var first_run: bool = true
|
|
var prev_slope: float
|
|
var prev_offset: float
|
|
var prev_log_base: int
|
|
|
|
func _ready() -> void:
|
|
|
|
prev_slope = slope
|
|
prev_offset = offset
|
|
prev_log_base = log_base
|
|
|
|
match use_case:
|
|
0:
|
|
assert(func_type != 0, "ERROR: Please select a use case in the inspector")
|
|
1:
|
|
update_function()
|
|
2:
|
|
make_steps()
|
|
|
|
first_run = false
|
|
|
|
func _process(_delta: float) -> void:
|
|
|
|
match use_case:
|
|
1:
|
|
if prev_slope != slope or prev_offset != offset or prev_log_base != log_base:
|
|
update_function()
|
|
print("points: " + str(self.curve.point_count))
|
|
prev_slope = slope
|
|
prev_offset = offset
|
|
prev_log_base = log_base
|
|
|
|
func make_steps():
|
|
|
|
##Set the first point at zeros and go from there
|
|
self.curve.add_point(Vector3(0,0,0))
|
|
##The y value
|
|
var yval = 0
|
|
|
|
##Add all of the steps
|
|
for xval in range(1, steps + 2):
|
|
##For each point after the first add a point at the same y and another that is up more
|
|
self.curve.add_point(Vector3(xval,yval,0))
|
|
yval += step_height
|
|
self.curve.add_point(Vector3(xval,yval,0))
|
|
|
|
##Remove the last point so the steps are flat at the top
|
|
self.curve.remove_point(self.curve.point_count - 1)
|
|
|
|
func update_function():
|
|
print(log_base)
|
|
##The curve index for where the point should be placed
|
|
var index = 0
|
|
##For every x-value to be populated onto the curve
|
|
for xval in range(start_point, end_point + 1):
|
|
##Add the new point with an x value equal to xval
|
|
if first_run:
|
|
self.curve.add_point(Vector3(xval,0,0))
|
|
##Get x value of curve at point
|
|
x = self.curve.get_point_position(index).x
|
|
##Get y value relative to x for the curve
|
|
match func_type:
|
|
0:
|
|
assert(func_type != 0, "ERROR: Curve must be set to a function type")
|
|
1:
|
|
y = slope * x + offset
|
|
2:
|
|
y = slope * x**2 + offset
|
|
4:
|
|
if self.curve.get_point_position(index).x > 0 and log_base > 1:
|
|
y = log(x) / log(log_base)
|
|
else:
|
|
y = x
|
|
8:
|
|
y = abs(x)
|
|
##Set curve to x and y values
|
|
self.curve.set_point_position(index, Vector3(x,y,0))
|
|
index += 1
|
|
#print(y)
|