Merge pull request #7 from eriwyn/3d

3d
pull/15/head
eriwyn 3 years ago committed by GitHub
commit f6292894d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. BIN
      entities/environment/birchtree/BirchTree_1.glb
  2. 1065
      entities/environment/birchtree/BirchTree_1.glb.import
  3. BIN
      entities/environment/birchtree/Black.material
  4. BIN
      entities/environment/birchtree/DarkGreen.material
  5. BIN
      entities/environment/birchtree/Green.material
  6. BIN
      entities/environment/birchtree/White.material
  7. 14
      entities/environment/birchtree/birchtree.tscn
  8. 21
      project.godot
  9. 8
      ui/map/cursor/Cursor.gd
  10. BIN
      ui/map/cursor/cursor.png
  11. 35
      ui/map/cursor/cursor.png.import
  12. 8
      ui/map/map.gd
  13. 8
      ui/map/map.tscn
  14. 1
      utils/Global.gd
  15. 98
      utils/camera/CamBase.gd
  16. 34
      utils/camera/CamBase.tscn
  17. 140
      utils/camera/CameraController.gd
  18. 158
      utils/camera/CameraInput.gd
  19. 17
      utils/camera/SelectionBox.gd
  20. 1
      utils/terrain/Terrain.gd
  21. 43
      world/World.gd
  22. 0
      world/default_env.tres
  23. 37
      world/game.gd
  24. 49
      world/game.tscn
  25. BIN
      world/world.material
  26. 4
      world/world.tres

File diff suppressed because it is too large Load Diff

@ -0,0 +1,14 @@
[gd_scene load_steps=3 format=2]
[ext_resource path="res://entities/environment/birchtree/BirchTree_1.glb" type="PackedScene" id=1]
[sub_resource type="BoxShape" id=1]
extents = Vector3( 0.643397, 1, 0.536359 )
[node name="Birchtree" type="StaticBody"]
[node name="CommonTree_4" parent="." instance=ExtResource( 1 )]
[node name="CollisionShape" type="CollisionShape" parent="."]
transform = Transform( 1, 0, 0, 0, 1.245, 0, 0, 0, 1, 0, 1.12837, 0 )
shape = SubResource( 1 )

@ -9,6 +9,11 @@
config_version=4 config_version=4
_global_script_classes=[ { _global_script_classes=[ {
"base": "Camera",
"class": "CameraController",
"language": "GDScript",
"path": "res://utils/camera/CameraController.gd"
}, {
"base": "Reference", "base": "Reference",
"class": "Delaunator", "class": "Delaunator",
"language": "GDScript", "language": "GDScript",
@ -25,6 +30,7 @@ _global_script_classes=[ {
"path": "res://utils/terrain/Terrain.gd" "path": "res://utils/terrain/Terrain.gd"
} ] } ]
_global_script_class_icons={ _global_script_class_icons={
"CameraController": "",
"Delaunator": "", "Delaunator": "",
"PoissonDiscSampling": "", "PoissonDiscSampling": "",
"Terrain": "" "Terrain": ""
@ -44,10 +50,23 @@ Global="*res://utils/Global.gd"
common/drop_mouse_on_gui_input_disabled=true common/drop_mouse_on_gui_input_disabled=true
[input]
main_command={
"deadzone": 0.5,
"events": [ Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"button_mask":0,"position":Vector2( 0, 0 ),"global_position":Vector2( 0, 0 ),"factor":1.0,"button_index":2,"pressed":false,"doubleclick":false,"script":null)
]
}
alt_command={
"deadzone": 0.5,
"events": [ Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"button_mask":0,"position":Vector2( 0, 0 ),"global_position":Vector2( 0, 0 ),"factor":1.0,"button_index":1,"pressed":false,"doubleclick":false,"script":null)
]
}
[physics] [physics]
common/enable_pause_aware_picking=true common/enable_pause_aware_picking=true
[rendering] [rendering]
environment/default_environment="res://default_env.tres" environment/default_environment="res://world/default_env.tres"

@ -0,0 +1,8 @@
extends Sprite
func _on_Camera_camera_moved(new_location):
var map_x = new_location.x
var map_y = new_location.z
position.x = map_x
position.y = map_y
pass # Replace with function body.

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.5 KiB

@ -0,0 +1,35 @@
[remap]
importer="texture"
type="StreamTexture"
path="res://.import/cursor.png-c6b2f949aa939fd4f4289acd7e6ebaee.stex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://ui/map/cursor/cursor.png"
dest_files=[ "res://.import/cursor.png-c6b2f949aa939fd4f4289acd7e6ebaee.stex" ]
[params]
compress/mode=0
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/bptc_ldr=0
compress/normal_map=0
flags/repeat=0
flags/filter=true
flags/mipmaps=false
flags/anisotropic=false
flags/srgb=2
process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
process/normal_map_invert_y=false
stream=false
size_limit=0
detect_3d=true
svg/scale=1.0

@ -1,5 +1,7 @@
extends Node2D extends Node2D
signal map_clicked
var terrain var terrain
func heightmap(): func heightmap():
@ -91,6 +93,12 @@ func _draw():
# draw_voronoi_cells_convex_hull() # draw_voronoi_cells_convex_hull()
# draw_voronoi_edges(Color("#ff0000")) # draw_voronoi_edges(Color("#ff0000"))
func _process(_delta):
if Input.is_action_pressed("alt_command"):
var new_position = get_viewport().get_mouse_position() / scale
if new_position.x <= 2000 and new_position.y <= 2000:
emit_signal("map_clicked", new_position)
func _on_Game_world_loaded(game_terrain): func _on_Game_world_loaded(game_terrain):
terrain = game_terrain terrain = game_terrain
update() update()

@ -1,6 +1,12 @@
[gd_scene load_steps=2 format=2] [gd_scene load_steps=4 format=2]
[ext_resource path="res://ui/map/map.gd" type="Script" id=1] [ext_resource path="res://ui/map/map.gd" type="Script" id=1]
[ext_resource path="res://ui/map/cursor/cursor.png" type="Texture" id=2]
[ext_resource path="res://ui/map/cursor/Cursor.gd" type="Script" id=3]
[node name="Map" type="Node2D"] [node name="Map" type="Node2D"]
script = ExtResource( 1 ) script = ExtResource( 1 )
[node name="Cursor" type="Sprite" parent="."]
texture = ExtResource( 2 )
script = ExtResource( 3 )

@ -1,6 +1,7 @@
extends Node extends Node
var debug = true var debug = true
var terrain
# Debuging messages # Debuging messages
func print_debug(message): func print_debug(message):

@ -0,0 +1,98 @@
extends Spatial
const MOVE_MARGIN = 20
const MOVE_SPEED = 30
const ray_length = 1000
onready var cam = $Camera
var team = 0
var selected_units = []
onready var selection_box = $SelectionBox
var start_sel_pos = Vector2()
func _process(delta):
var m_pos = get_viewport().get_mouse_position()
# calc_move(m_pos, delta)
if Input.is_action_just_pressed("main_command"):
move_selected_units(m_pos)
if Input.is_action_just_pressed("alt_command"):
selection_box.start_sel_pos = m_pos
start_sel_pos = m_pos
if Input.is_action_pressed("alt_command"):
selection_box.m_pos = m_pos
selection_box.is_visible = true
else:
selection_box.is_visible = false
if Input.is_action_just_released("alt_command"):
select_units(m_pos)
func calc_move(m_pos, delta):
var v_size = get_viewport().size
var move_vec = Vector3()
if m_pos.x < MOVE_MARGIN:
move_vec.x -= 1
if m_pos.y < MOVE_MARGIN:
move_vec.z -= 1
if m_pos.x > v_size.x - MOVE_MARGIN:
move_vec.x += 1
if m_pos.y > v_size.y - MOVE_MARGIN:
move_vec.z += 1
move_vec = move_vec.rotated(Vector3(0, 1, 0), rotation_degrees.y)
global_translate(move_vec * delta * MOVE_SPEED)
func move_selected_units(m_pos):
var result = raycast_from_mouse(m_pos, 1)
if result:
var unit_index = 0
for unit in selected_units:
var new_position = result.position
new_position.x = new_position.x + unit_index
unit.move_to(new_position)
unit_index += 1
func select_units(m_pos):
var new_selected_units = []
if m_pos.distance_squared_to(start_sel_pos) < 16:
var u = get_unit_under_mouse(m_pos)
if u != null:
new_selected_units.append(u)
else:
new_selected_units = get_units_in_box(start_sel_pos, m_pos)
for unit in selected_units:
unit.deselect()
for unit in new_selected_units:
unit.select()
selected_units = new_selected_units
func get_unit_under_mouse(m_pos):
var result = raycast_from_mouse(m_pos, 3)
if result and result.collider.is_in_group("units"):
return result.collider
func get_units_in_box(top_left, bot_right):
if top_left.x > bot_right.x:
var tmp = top_left.x
top_left.x = bot_right.x
bot_right.x = tmp
if top_left.y > bot_right.y:
var tmp = top_left.y
top_left.y = bot_right.y
bot_right.y = tmp
var box = Rect2(top_left, bot_right - top_left)
var box_selected_units = []
for unit in get_tree().get_nodes_in_group("units"):
if box.has_point(cam.unproject_position(unit.global_transform.origin)):
box_selected_units.append(unit)
return box_selected_units
func raycast_from_mouse(m_pos, collision_mask):
var ray_start = cam.project_ray_origin(m_pos)
var ray_end = ray_start + cam.project_ray_normal(m_pos) * ray_length
var space_state = get_world().direct_space_state
return space_state.intersect_ray(ray_start, ray_end, [], collision_mask)
func _on_Camera_camera_moved(new_location):
pass # Replace with function body.

@ -0,0 +1,34 @@
[gd_scene load_steps=5 format=2]
[ext_resource path="res://utils/camera/SelectionBox.gd" type="Script" id=1]
[ext_resource path="res://utils/camera/CamBase.gd" type="Script" id=2]
[ext_resource path="res://utils/camera/CameraController.gd" type="Script" id=3]
[ext_resource path="res://utils/camera/CameraInput.gd" type="Script" id=4]
[node name="CamBase" type="Spatial"]
transform = Transform( 1, 0, 0, 0, 0.0238738, 0.999715, 0, -0.999715, 0.0238738, 0, 0, 16.935 )
script = ExtResource( 2 )
[node name="Camera" type="Camera" parent="."]
transform = Transform( 1, 0, 0, 0, 0.910272, -0.41401, 0, 0.41401, 0.910272, 0, 0, 6.618 )
size = 20.0
near = 0.01
far = 8192.0
script = ExtResource( 3 )
movement_speed = 70.017
min_zoom = 51.0
zoom_sensibility = 2.818
rotation_sensibility = 1.0
[node name="Node" type="Node" parent="Camera"]
script = ExtResource( 4 )
[node name="SelectionBox" type="Control" parent="."]
margin_right = 40.0
margin_bottom = 40.0
script = ExtResource( 1 )
[connection signal="on_change_action" from="Camera/Node" to="Camera" method="change_action"]
[connection signal="on_change_velocity" from="Camera/Node" to="Camera" method="change_velocity"]
[connection signal="on_rotate_view" from="Camera/Node" to="Camera" method="rotate_view"]
[connection signal="on_zoom" from="Camera/Node" to="Camera" method="zoom"]

@ -0,0 +1,140 @@
extends Camera
class_name CameraController
signal camera_moved(new_location)
enum CAMERA_ACTIONS{
MOVING,
ROTATING_VIEW,
}
export(float,1,100) var movement_speed = 30
export(float,0.01,0.99) var movement_damping = 0.74
export(float,0.01, 3.1415) var max_rotation = 1.2
export(float,0.01, 3.1415) var min_rotation = 0.5
#Value in percentage of screen portion
#A value of 0.3 means that when you place the cursor 30% or less away from an edge it will start pushing the camera
export(float, 0.0,1.0) var edge_size = 0.0
#EDIT HERE--->**,***<--- ZOOM MIN AND MAX LIMITS
export(float, 10,100) var min_zoom = 25
export(float, 10,100) var max_zoom = 100
export(float, 1,3) var zoom_sensibility = 2.5
export(float, 1,3) var rotation_sensibility = 2.3
export(float, 1.0, 10.0) var height = 5.0
var pitch : float
var yaw : float
var current_action = CAMERA_ACTIONS.MOVING
var velocity : Vector2
func _ready():
# Input.set_mouse_mode(Input.MOUSE_MODE_CONFINED)
pitch = rotation.x
yaw = rotation.y
# var new_rotation = (max_zoom - fov) * (max_rotation - min_rotation) / max_zoom + min_rotation
transform.basis = Basis(Vector3(1, 0, 0), (min_rotation + max_rotation) / 2.0)
fov = (min_zoom + max_zoom) / 2.0
func change_action(action):
current_action = action
match(current_action):
# CAMERA_ACTIONS.MOVING:
# Input.set_mouse_mode(Input.MOUSE_MODE_CONFINED)
CAMERA_ACTIONS.ROTATING_VIEW:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _process(delta):
match(current_action):
CAMERA_ACTIONS.MOVING:
#CAMERA MOVEMENT
velocity.x = clamp(velocity.x * movement_damping,-1.0,1.0)
velocity.y = clamp(velocity.y * movement_damping,-1.0,1.0)
if velocity != Vector2.ZERO:
move(velocity)
func change_velocity(_velocity : Vector2):
velocity = _velocity
func move(_velocity : Vector2):
#Move along cameras X axis
global_transform.origin += global_transform.basis.x * velocity.x * movement_speed * get_process_delta_time()
#Calculate a forward camera direction that is perpendicular to the XZ plane
var forward = global_transform.basis.x.cross(Vector3.UP)
#Move the camera along that forward direction
global_transform.origin += forward * velocity.y * movement_speed * get_process_delta_time()
var y_offset = 0
var space_state = get_world().direct_space_state
var result = space_state.intersect_ray(Vector3(global_transform.origin.x, 100, global_transform.origin.z), Vector3(global_transform.origin.x, 0, global_transform.origin.z))
if result:
y_offset = result.position.y
else:
y_offset = 0
global_transform.origin.y = max(5 + y_offset * 1.3, 10)
emit_signal("camera_moved", global_transform.origin)
func zoom(direction : float):
#Zooming using fov
var new_fov = fov + (sign(direction) * pow(abs(direction),zoom_sensibility)/100 * get_process_delta_time())
fov = clamp(new_fov,min_zoom,max_zoom)
# Linear equation
var slope = (min_rotation - max_rotation) / (max_zoom - min_zoom)
var b = max_rotation - slope * min_zoom
var new_rotation = slope * fov + b
transform.basis = Basis(Vector3(1, 0, 0), new_rotation)
func rotate_view(axis : Vector2):
var pitch_rotation_amount = -axis.y/100 * get_process_delta_time() * rotation_sensibility
var yaw_rotation_amount = -axis.x/100 * get_process_delta_time() * rotation_sensibility
pitch += pitch_rotation_amount
pitch = clamp(pitch,-PI/2,0)
yaw += yaw_rotation_amount
rotation.x = pitch
rotation.y = yaw
func _on_Map_map_clicked(position):
global_transform.origin.x = position.x
global_transform.origin.z = position.y
var y_offset = 0
var space_state = get_world().direct_space_state
var result = space_state.intersect_ray(Vector3(global_transform.origin.x, 100, global_transform.origin.z), Vector3(global_transform.origin.x, 0, global_transform.origin.z))
if result:
y_offset = result.position.y
else:
y_offset = 0
global_transform.origin.y = max(height + y_offset * 1.3, 30)
pass # Replace with function body.
func _on_World_character_created(position):
global_transform.origin.x = position.x
global_transform.origin.z = position.y
var y_offset = 0
var space_state = get_world().direct_space_state
var result = space_state.intersect_ray(Vector3(global_transform.origin.x, 100, global_transform.origin.z), Vector3(global_transform.origin.x, 0, global_transform.origin.z))
if result:
y_offset = result.position.y
else:
y_offset = 0
global_transform.origin.y = max(height + y_offset * 1.3, 30)
pass # Replace with function body.

@ -0,0 +1,158 @@
extends Node
export(float,0.001,1.0) var screen_edge_size : float = 0.3
export(float) var mouse_wheel_damping = 0.9
#USED TO CALCULATE RAW MOVEMENT WHILE PUSHING AN EDGE
var horizontal : float = 0.0
var vertical : float = 0.0
#USED TO STORE MOUSE WHEEL INERTIA TO ENABLE SMOOTH STOPPING
var mouse_wheel : float = 0.0
signal on_change_velocity(velocity)
signal on_rotate_view(relative)
signal on_change_action(new_state)
signal on_zoom(value)
var current_action
#TOUCH VARIABLE FOR MOBILE
var touch_count : int = 0
var swipe_start : Vector2
func _ready():
connect("on_change_action",self,"change_action")
emit_signal("on_change_action",CameraController.CAMERA_ACTIONS.MOVING)
func change_action(action):
current_action = action
func toggle_action():
current_action = 1 - current_action
func start_swipe(position : Vector2):
swipe_start = position
func move_swipe(position : Vector2):
if blocked_movement:
return
var delta = (position - swipe_start)*-1
var direction_x = sign(delta.x)
var direction_y = sign(delta.y)
var view_size = get_viewport().get_visible_rect().size - Vector2.ONE
horizontal = range_lerp(abs(delta.x),0,view_size.x,0.0,1.0)
vertical = range_lerp(abs(delta.y),0,view_size.y,0.0,1.0)
#Applies direction
horizontal *= direction_x
vertical *= direction_y
var touches = [ Vector2.ZERO, Vector2.ZERO ]
var start_pinch_distance : float
var last_pinch_distance : float
var pinching : float
var blocked_movement : bool = false
func _input(event):
if OS.get_name() == "Android" or OS.get_name() == "iOS":
#MOBILE##############
if event is InputEventScreenTouch:
#SET TOUCH STARTING POSTIION
touches[event.index] = event.position
if event.pressed:
touch_count += 1
start_swipe(event.position)
else:
touch_count -= 1
#RENABLE SWIPE MOVEMENT BECAUSE EVERY TOUCH WAS LIFTED
if blocked_movement and touch_count <= 0:
blocked_movement = false
#RESET PINCHING VALUE WHEN A NEW TOUCH IS DETECTED OR HAS BEEN LIFTED
pinching = 0.0
if touch_count == 2:
#STARTED ZOOMING, BLOCK MOVEMENT UNTIL EVERY TOUCH IS LIFTED
blocked_movement = true
start_pinch_distance = (touches[1] - touches[0]).length()
if event is InputEventScreenDrag:
if touch_count == 2:
#UPDATE TOUCHES POSITIONS
touches[event.index] = event.position
#CALCULATE DISTANCE BETWEEN TOUCHES
var pinch_distance = (touches[1] - touches[0]).length()
var pinch_direction = 1 if pinch_distance > last_pinch_distance else -1
#CALCULATE PINCH DELTA
pinching = abs(start_pinch_distance - pinch_distance) * pinch_direction
#USE MOUSE WHEEL BUFFER TO ENABLE SMOOTHING
mouse_wheel += pinching * get_process_delta_time()
last_pinch_distance = pinch_distance
else:
if current_action == CameraController.CAMERA_ACTIONS.MOVING:
move_swipe(event.position)
elif current_action == CameraController.CAMERA_ACTIONS.ROTATING_VIEW:
emit_signal("on_rotate_view",event.relative)
###############MOBILE
else:
#PC##################
#Camera edge pushing
if event is InputEventMouseMotion:
#ROTATE VIEW
if current_action == CameraController.CAMERA_ACTIONS.ROTATING_VIEW:
emit_signal("on_rotate_view",event.relative)
#Gets screen size
var view_size = get_viewport().get_visible_rect().size - Vector2.ONE
#Get mouse position in percentage values relative to the screen
var delta = (event.position) / view_size
#Convert it to a range between [-1,1]
delta = (delta * 2) - Vector2.ONE
if current_action == CameraController.CAMERA_ACTIONS.MOVING:
#Store it an buffer to use it on _process
#Calculates delta based on percentage between the edge size and the actual edge
horizontal = max(abs(delta.x) - (1.0 - screen_edge_size),0)
vertical = max(abs(delta.y) - (1.0 - screen_edge_size),0)
#Converts it to an [0.0,1.0] range
horizontal = range_lerp(horizontal,0.0,screen_edge_size,0.0,1.0)
vertical = range_lerp(vertical,0.0,screen_edge_size,0.0,1.0)
#Applies direction
horizontal *= sign(delta.x)
vertical *= sign(delta.y)
elif current_action == CameraController.CAMERA_ACTIONS.ROTATING_VIEW:
horizontal = delta.x
vertical = delta.y
pass
if event is InputEventMouseButton:
#WHEEL SCROLL
if event.button_index == BUTTON_WHEEL_UP or event.button_index == BUTTON_WHEEL_DOWN:
if event.pressed and not event.is_echo():
var direction = (-1 if event.button_index == BUTTON_WHEEL_UP else 0) + (1 if event.button_index == BUTTON_WHEEL_DOWN else 0)
mouse_wheel += direction * get_process_delta_time() * 1000
###################PC
func _process(delta):
#PC######
match(current_action):
CameraController.CAMERA_ACTIONS.MOVING:
#RESIDUAL MOVEMENT
if horizontal != 0 or vertical != 0:
emit_signal("on_change_velocity",Vector2(horizontal, vertical))
#MOUSE WHEEL
if mouse_wheel != 0:
mouse_wheel = mouse_wheel * mouse_wheel_damping
emit_signal("on_zoom",mouse_wheel)
#######PC

@ -0,0 +1,17 @@
extends Control
var is_visible = false
var m_pos = Vector2()
var start_sel_pos = Vector2()
const sel_box_col = Color(0, 1, 0)
const sel_box_line_width = 3
func _draw():
if is_visible and start_sel_pos != m_pos:
draw_line(start_sel_pos, Vector2(m_pos.x, start_sel_pos.y), sel_box_col, sel_box_line_width)
draw_line(start_sel_pos, Vector2(start_sel_pos.x, m_pos.y), sel_box_col, sel_box_line_width)
draw_line(m_pos, Vector2(m_pos.x, start_sel_pos.y), sel_box_col, sel_box_line_width)
draw_line(m_pos, Vector2(start_sel_pos.x, m_pos.y), sel_box_col, sel_box_line_width)
func _process(delta):
update()

@ -63,6 +63,7 @@ class Triangle:
var list_points = [] var list_points = []
for edge in edges(): for edge in edges():
list_points.append(Point.new(_terrain._triangles[edge._idx], _terrain)) list_points.append(Point.new(_terrain._triangles[edge._idx], _terrain))
list_points.invert()
return list_points return list_points
func triangles_adjacent(): func triangles_adjacent():

@ -0,0 +1,43 @@
extends Spatial
var terrain
func _ready():
pass
func draw_world():
# for i in range(0, 1, 1):
# print(i)
var st = SurfaceTool.new()
st.begin(Mesh.PRIMITIVE_TRIANGLES)
st.add_smooth_group(true)
for triangle in terrain.get_triangles():
for point in triangle.points():
var factor = Vector3(1, 24*5, 1)
# if point.get_data("river") and i == 0:
# factor.y -= 0
# if i == 1:
# factor.y -= 2.0
st.add_vertex(point.point3d() * factor)
st.generate_normals()
# st.generate_tangents()
st.index()
# Commit to a mesh.
var mesh = st.commit()
var mi = MeshInstance.new()
mi.mesh = mesh
var material = load("res://world/world.material")
mi.set_surface_material(0, material)
mi.create_trimesh_collision()
mi.cast_shadow = GeometryInstance.SHADOW_CASTING_SETTING_ON
print(mi)
add_child(mi)
func _on_Game_world_loaded(game_terrain):
terrain = game_terrain
draw_world()

@ -1,17 +1,17 @@
extends Spatial extends Node
signal world_loaded signal world_loaded
export(int) var width = 2000 export(int) var width = 2000
export(int) var height = 2000 export(int) var height = 2000
export(int) var spacing = 20 export(int) var spacing = 5
export(int, 1, 9) var octaves = 5 export(int, 1, 9) var octaves = 5
export(int, 1, 30) var wavelength = 8 export(int, 1, 30) var wavelength = 8
export(int) var border_width = 200 export(int) var border_width = 200
export(int) var terraces = 24 export(int) var terraces = 24
export(int) var terrace_height = 5 export(int) var terrace_height = 5
export(float) var mountain_height = 6.0 / 24.0 export(float) var mountain_height = 6.0 / 24.0
export(int) var river_proba = 100 export(int) var river_proba = 200
var rng = RandomNumberGenerator.new() var rng = RandomNumberGenerator.new()
var noise = OpenSimplexNoise.new() var noise = OpenSimplexNoise.new()
@ -24,7 +24,7 @@ func _ready():
noise.octaves = octaves noise.octaves = octaves
# terrain = Terrain.new(width,height,spacing,false) # terrain = Terrain.new(width,height,spacing,false)
var terrain_name="bonjour" var terrain_name="bonjourazeazea"
terrain = Terrain.new() terrain = Terrain.new()
print(terrain.list()) print(terrain.list())
@ -36,6 +36,7 @@ func _ready():
if terrain.is_created() or terrain.is_loaded(): if terrain.is_created() or terrain.is_loaded():
init_data() init_data()
add_trees()
emit_signal("world_loaded", terrain) emit_signal("world_loaded", terrain)
else: else:
Global.print_debug("Pas de terrain, pas de construction ...") Global.print_debug("Pas de terrain, pas de construction ...")
@ -47,13 +48,14 @@ func init_data():
point.set_elevation(point_find_elevation(point.point2d())) point.set_elevation(point_find_elevation(point.point2d()))
point.set_data("water", point_is_water(point)) point.set_data("water", point_is_water(point))
point.set_data("mountain", point_is_mountain(point)) point.set_data("mountain", point_is_mountain(point))
point.set_data("river", point_is_river(point)) # point.set_data("river", point_is_river(point))
fill_oceans() fill_oceans()
for point in terrain.get_points(): for point in terrain.get_points():
if point.get_data("water") and not point.get_data("ocean"): if point.get_data("water") and not point.get_data("ocean"):
point.set_elevation(0.1) point.set_elevation(0.1)
point.set_data("water", false)
point.set_data("coast", point_is_coast(point)) point.set_data("coast", point_is_coast(point))
if point.get_data("river"): if point.get_data("river"):
set_river_path(point) set_river_path(point)
@ -87,7 +89,6 @@ func set_river_path(point):
var start_elevation = point.get_elevation() var start_elevation = point.get_elevation()
var waypoints = [] var waypoints = []
var stack = [] var stack = []
var end
stack.append(point.get_index()) stack.append(point.get_index())
var came_from = {} var came_from = {}
@ -97,7 +98,6 @@ func set_river_path(point):
waypoints.append(current_point_id) waypoints.append(current_point_id)
start_elevation = terrain.get_point(current_point_id).get_elevation() start_elevation = terrain.get_point(current_point_id).get_elevation()
stack = [] stack = []
end = current_point_id
if terrain.get_point(current_point_id).get_data("ocean"): if terrain.get_point(current_point_id).get_data("ocean"):
break break
for neighbour in terrain.get_point(current_point_id).points_around(): for neighbour in terrain.get_point(current_point_id).points_around():
@ -116,6 +116,7 @@ func set_river_path(point):
path.append(point.get_index()) path.append(point.get_index())
for index in path: for index in path:
terrain.get_point(index).set_data("river", true) terrain.get_point(index).set_data("river", true)
terrain.get_point(index).set_data("water", true)
# Point # Point
@ -140,7 +141,7 @@ func point_find_elevation(point):
elevation = min(elevation, 1) elevation = min(elevation, 1)
# elevation = elevation * terraces elevation = round(elevation * terraces) / terraces
return elevation return elevation
func point_is_water(point): func point_is_water(point):
@ -192,3 +193,23 @@ func edge_is_river(edge):
if edge.start().get_data("river") and edge.end().get_data("river"): if edge.start().get_data("river") and edge.end().get_data("river"):
return true return true
return false return false
func add_trees():
rng.randomize()
var treescene = load("res://entities/environment/birchtree/birchtree.tscn")
for point in terrain.get_points():
if not point.get_data("water"):
var num = rng.randi_range(0, 5)
if num == 1:
var tree = treescene.instance()
tree.translation = Vector3(point.point3d() * Vector3(1, 24*5, 1))
add_child(tree)
# for point in points.size():
# if points_data[i].elevation > 0:
# var num = rng.randi_range(0, 20)
# if num == 2:
# var tree = treescene.instance()
# tree.translation = Vector3(points[i].x, points_data[i].elevation * terrace_height, points[i].y)
# add_child(tree)

@ -1,16 +1,59 @@
[gd_scene load_steps=3 format=2] [gd_scene load_steps=8 format=2]
[ext_resource path="res://ui/ui.tscn" type="PackedScene" id=1] [ext_resource path="res://ui/ui.tscn" type="PackedScene" id=1]
[ext_resource path="res://world/game.gd" type="Script" id=2] [ext_resource path="res://world/game.gd" type="Script" id=2]
[ext_resource path="res://world/default_env.tres" type="Environment" id=3]
[ext_resource path="res://world/World.gd" type="Script" id=4]
[ext_resource path="res://utils/camera/CamBase.tscn" type="PackedScene" id=5]
[node name="Game" type="Spatial"] [sub_resource type="PlaneMesh" id=1]
size = Vector2( 2000, 2000 )
[sub_resource type="SpatialMaterial" id=2]
albedo_color = Color( 0.054902, 0.533333, 0.741176, 1 )
[node name="Game" type="Node"]
script = ExtResource( 2 ) script = ExtResource( 2 )
[node name="UI" parent="." instance=ExtResource( 1 )] [node name="UI" parent="." instance=ExtResource( 1 )]
[node name="Map" parent="UI" index="0"] [node name="Map" parent="UI" index="0"]
scale = Vector2( 0.5, 0.5 ) scale = Vector2( 0.25, 0.25 )
[node name="Cursor" parent="UI/Map" index="0"]
scale = Vector2( 4, 4 )
z_index = 3
[node name="World" type="Spatial" parent="."]
script = ExtResource( 4 )
[node name="Water" type="MeshInstance" parent="World"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 1000, -0.1, 1000 )
mesh = SubResource( 1 )
material/0 = SubResource( 2 )
[node name="WorldEnvironment" type="WorldEnvironment" parent="World"]
environment = ExtResource( 3 )
[node name="CamBase" parent="World" instance=ExtResource( 5 )]
[node name="Camera" parent="World/CamBase" index="0"]
transform = Transform( 1, 0, 0, 0, 0.910272, -0.41401, 0, 0.41401, 0.910272, 0, -1.90735e-06, 6.618 )
movement_speed = 48.076
min_zoom = 10.0
zoom_sensibility = 1.436
rotation_sensibility = 3.0
[node name="DirectionalLight" type="DirectionalLight" parent="World"]
transform = Transform( 1, 0, 0, 0, 0.512597, 0.858629, 0, -0.858629, 0.512597, 0, 1.41623, 0 )
light_energy = 0.1
shadow_enabled = true
[connection signal="world_loaded" from="." to="UI/Map" method="_on_Game_world_loaded"] [connection signal="world_loaded" from="." to="UI/Map" method="_on_Game_world_loaded"]
[connection signal="world_loaded" from="." to="World" method="_on_Game_world_loaded"]
[connection signal="map_clicked" from="UI/Map" to="World/CamBase/Camera" method="_on_Map_map_clicked"]
[connection signal="camera_moved" from="World/CamBase/Camera" to="UI/Map/Cursor" method="_on_Camera_camera_moved"]
[editable path="UI"] [editable path="UI"]
[editable path="UI/Map"]
[editable path="World/CamBase"]

Binary file not shown.

@ -0,0 +1,4 @@
[gd_resource type="SpatialMaterial" format=2]
[resource]
albedo_color = Color( 0.109804, 0.305882, 0.0745098, 1 )
Loading…
Cancel
Save