Files
2024-08-06 04:49:41 +02:00

173 lines
4.8 KiB
GDScript

extends MeshInstance3D
var isActive = false
var inMenu = false
var randomMaterial : int
var randomAxis : int
var last : Node3D
var vertices : Array
var newVerts : Array
var speed = 2.0
var score = 1
const maxMoveDistance = 3.0
var velocity : Vector3
var cam : Camera3D
var labelScore : Label
var rng = RandomNumberGenerator.new()
func calculate_randoms():
randomMaterial = rng.randi_range(0, 2)
randomAxis = rng.randi_range(0, 1)
if last != null:
while randomMaterial == last.randomMaterial:
randomMaterial = rng.randi_range(0, 2)
while randomAxis == last.randomAxis:
randomAxis = rng.randi_range(0, 1)
if randomAxis == 0:
velocity.z = 0
velocity.x = speed
else:
velocity.z = speed
velocity.x = 0
func rebuild_mesh(vertTemplate):
var tempVertices = []
var st = SurfaceTool.new()
st.begin(Mesh.PRIMITIVE_TRIANGLES)
match randomMaterial:
0:
st.set_material(PlatformData.material1())
1:
st.set_material(PlatformData.material2())
2:
st.set_material(PlatformData.material3())
var index = 0
for i in range(0, 6):
st.set_uv(Vector2(0, 0))
st.set_normal(PlatformData.normals[i])
st.add_vertex(vertTemplate[PlatformData.triangles[i][0]])
st.set_uv(Vector2(0, 1))
st.set_normal(PlatformData.normals[i])
st.add_vertex(vertTemplate[PlatformData.triangles[i][1]])
st.set_uv(Vector2(1, 1))
st.set_normal(PlatformData.normals[i])
st.add_vertex(vertTemplate[PlatformData.triangles[i][2]])
st.set_uv(Vector2(1, 0))
st.set_normal(PlatformData.normals[i])
st.add_vertex(vertTemplate[PlatformData.triangles[i][3]])
tempVertices.append(vertTemplate[PlatformData.triangles[i][0]])
tempVertices.append(vertTemplate[PlatformData.triangles[i][1]])
tempVertices.append(vertTemplate[PlatformData.triangles[i][2]])
tempVertices.append(vertTemplate[PlatformData.triangles[i][3]])
st.add_index(index + 2)
st.add_index(index + 3)
st.add_index(index + 1)
st.add_index(index + 2)
st.add_index(index + 1)
st.add_index(index + 0)
index += 4
mesh = st.commit()
vertices = tempVertices
func _ready():
if name == "Platform1":
isActive = true
if isActive:
last = %Base
if not inMenu and name == "Platform1":
cam = %Camera3D
labelScore = %Score
calculate_randoms()
rebuild_mesh(PlatformData.vertices)
func _physics_process(delta):
if not isActive:
return
if abs(position.x) >= maxMoveDistance || abs(position.z) >= maxMoveDistance:
velocity.x *= -1
velocity.z *= -1
translate(velocity * delta)
if Input.is_action_just_pressed("Click") and not inMenu:
isActive = false
cam.translate(Vector3(0, 0.1, 0))
var otherVerts = last.vertices
var _newVerts = []
if len(last.newVerts) == 0:
_newVerts = PlatformData.vertices.duplicate()
else:
_newVerts = last.newVerts.duplicate()
if randomAxis == 0:
var currentX = position.x
var lastX = last.position.x
if vertices[0].x + currentX < otherVerts[0].x + lastX:
_newVerts[0].x = otherVerts[0].x - currentX + lastX
_newVerts[3].x = otherVerts[0].x - currentX + lastX
_newVerts[7].x = otherVerts[0].x - currentX + lastX
_newVerts[4].x = otherVerts[0].x - currentX + lastX
if vertices[3].x + currentX > otherVerts[3].x + lastX:
_newVerts[1].x = otherVerts[3].x - currentX + lastX
_newVerts[2].x = otherVerts[3].x - currentX + lastX
_newVerts[5].x = otherVerts[3].x - currentX + lastX
_newVerts[6].x = otherVerts[3].x - currentX + lastX
if vertices[3].x + currentX < otherVerts[0].x + lastX or vertices[0].x + currentX > otherVerts[3].x + lastX:
get_tree().reload_current_scene()
else:
var currentZ = position.z
var lastZ = last.position.z
if vertices[0].z + currentZ < otherVerts[0].z + lastZ:
_newVerts[0].z = otherVerts[0].z - currentZ + lastZ
_newVerts[1].z = otherVerts[0].z - currentZ + lastZ
_newVerts[2].z = otherVerts[0].z - currentZ + lastZ
_newVerts[3].z = otherVerts[0].z - currentZ + lastZ
if vertices[3].z + currentZ > otherVerts[3].z + lastZ:
_newVerts[4].z = otherVerts[3].z - currentZ + lastZ
_newVerts[5].z = otherVerts[3].z - currentZ + lastZ
_newVerts[6].z = otherVerts[3].z - currentZ + lastZ
_newVerts[7].z = otherVerts[3].z - currentZ + lastZ
if vertices[3].z + currentZ < otherVerts[0].z + lastZ or vertices[0].z + currentZ > otherVerts[3].z + lastZ:
get_tree().reload_current_scene()
newVerts = _newVerts
rebuild_mesh(_newVerts)
var inst = load("res://platform.tscn").instantiate()
inst.cam = cam
inst.labelScore = labelScore
get_parent().add_child(inst)
inst.isActive = false
inst.translate(Vector3(0, 0.2, 0) + position)
inst.last = self
inst.speed = speed + 0.2
inst.calculate_randoms()
inst.rebuild_mesh(newVerts)
labelScore.text = str(score)
inst.score = score + 1
inst.isActive = true