diff options
Diffstat (limited to 'World.gd')
-rw-r--r-- | World.gd | 103 |
1 files changed, 47 insertions, 56 deletions
@@ -1,68 +1,59 @@ -extends RigidBody +extends Spatial +export(NodePath) var traveler = null +onready var _traveler = get_node(traveler) export var acceleration = 10.0 -export var generation_distance = 300.0 -export var generation_subdivision = 5.0 -var gen_coords = Vector2.ZERO -const init_distance = 10 - -onready var Rock = preload("res://Rock.tscn") -onready var noise = OpenSimplexNoise.new() - -func _on_WorldGenCull_body_exited(body:Node): - body.queue_free() - -func make_rock(xform:Transform): - var rock = Rock.instance() - rock.transform = xform - self.add_child(rock) - -func generate_location(coords:Vector2): - if coords.x < -init_distance || coords.x > init_distance || coords.y < -init_distance || coords.y > init_distance: - if noise.get_noise_2d(coords.x,coords.y) > 0.6: - coords *= generation_subdivision - make_rock(Transform(Basis(), Vector3(coords.x,0.0,coords.y))) - -func generate_world(): - var new_coords = -(self.transform.origin / generation_subdivision).floor() - new_coords = Vector2(new_coords.x,new_coords.z) - var direction = new_coords - gen_coords - var gen_max = generation_distance / generation_subdivision - var gen_min = -gen_max - if gen_coords.x != new_coords.x: - for i in range(new_coords.y+gen_min,new_coords.y+gen_max): - generate_location(Vector2(new_coords.x+(gen_min if direction.x < 0 else gen_max),i)) - if gen_coords.y != new_coords.y: - for i in range(new_coords.x+gen_min,new_coords.x+gen_max): - generate_location(Vector2(i,new_coords.y+(gen_min if direction.y < 0 else gen_max))) - gen_coords = new_coords +onready var Chunk = preload("res://Chunk.tscn") +onready var chunk_size = $"WorldGen/Chunk/Size".shape.radius*2.0 +var chunks = {} + +func v2_coords(xform:Transform): + var coords = xform.origin + coords = Vector2(coords.x,coords.z) + return coords + +func chunk_coords(coords:Vector2): + coords = (coords / chunk_size).floor() + return coords + +func world_coords(coords:Vector2): + coords *= chunk_size + return Vector3(coords.x,0.0,coords.y) + +func gen_chunk(xform:Transform): + var chunk = Chunk.instance() + chunk.transform = xform + $"WorldGen".add_child(chunk) + chunks[chunk_coords(v2_coords(xform))] = chunk + +func gen_chunks(): + var gen_center = -v2_coords($"WorldGen".transform) + var gen_radius = $"WorldGenCull/Radius".shape.radius-chunk_size + for x in range(-gen_radius, gen_radius, chunk_size): + for y in range(-gen_radius, gen_radius, chunk_size): + var gen_coords = Vector2(x,y) + var coords = chunk_coords(gen_coords + gen_center) + if gen_coords.length() <= gen_radius && !chunks.has(coords): + gen_chunk(Transform(Basis(), world_coords(coords))) + +func _on_WorldGenCull_area_exited(area:Area): + gen_chunks() + area.queue_free() + chunks.erase(chunk_coords(v2_coords(area.transform))) func _ready(): - randomize() - noise.seed = randi() - noise.octaves = 4 - noise.period = 16 - noise.persistence = 0.001 - noise.lacunarity = 2.0 - - var gen_max = generation_distance / generation_subdivision - var gen_min = -gen_max - for i in range(gen_min,gen_max): - for j in range(gen_min,gen_max): - generate_location(Vector2(i,j)) + gen_chunks() func travel(direction:Vector3): - var boat = $"/root/Main/ShipRigidBody" - var boat_direction = boat.global_transform.basis - boat_direction = Basis(Vector3.UP*boat_direction.get_euler().y) + var heading = _traveler.global_transform.basis + heading = Basis(Vector3.UP*heading.get_euler().y) direction = -direction - direction = boat_direction * direction + direction = heading * direction direction = direction.normalized() - self.add_central_force(direction*acceleration) + $"WorldGen".add_central_force(direction*acceleration) -func _integrate_forces(_state): - generate_world() +func _physics_process(_state): var stick = Input.get_axis("ship_down","ship_up") if stick != 0.0: - travel(Vector3.FORWARD*stick) + self.travel(Vector3.FORWARD*stick) |