#14 ajout de la notion de terrain, de planète et de zone contaminée
This commit is contained in:
5
common/game_data/scripts/game_data.gd
Normal file
5
common/game_data/scripts/game_data.gd
Normal file
@@ -0,0 +1,5 @@
|
||||
extends Resource
|
||||
class_name GameData
|
||||
|
||||
@export var currentTerrainData : TerrainData
|
||||
|
||||
1
common/game_data/scripts/game_data.gd.uid
Normal file
1
common/game_data/scripts/game_data.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://73gftrlwnuhu
|
||||
70
common/game_data/scripts/terrain_data.gd
Normal file
70
common/game_data/scripts/terrain_data.gd
Normal file
@@ -0,0 +1,70 @@
|
||||
extends Resource
|
||||
class_name TerrainData
|
||||
|
||||
const TERRAIN_IMAGE_GAME_FACTOR = 50
|
||||
const DEFAULT_CONTAMINATION_CENTRAL_ZONE_MAX_SIZE = 1000
|
||||
const DEFAULT_CONTAMINATION_CENTRAL_ZONE_MIN_SIZE = 200
|
||||
|
||||
signal terrain_updated
|
||||
|
||||
@export var terrainSize : Vector2 = Vector2(2000,2000)
|
||||
|
||||
@export var contamination : Image = null
|
||||
|
||||
func generate_default_contamination(
|
||||
central_zone_max_size : int = DEFAULT_CONTAMINATION_CENTRAL_ZONE_MAX_SIZE,
|
||||
central_zone_min_size : int = DEFAULT_CONTAMINATION_CENTRAL_ZONE_MIN_SIZE,
|
||||
):
|
||||
var noise: Noise = FastNoiseLite.new()
|
||||
noise.seed = randi()
|
||||
noise.noise_type = FastNoiseLite.TYPE_CELLULAR
|
||||
noise.frequency = 0.005 * TERRAIN_IMAGE_GAME_FACTOR
|
||||
|
||||
var imageSize = terrainSize / TERRAIN_IMAGE_GAME_FACTOR;
|
||||
|
||||
var noise_image = noise.get_image(
|
||||
imageSize.x,
|
||||
imageSize.y,
|
||||
1.0
|
||||
)
|
||||
|
||||
ImageTools.draw_gradient(
|
||||
noise_image,
|
||||
imageSize/2,
|
||||
central_zone_min_size / TERRAIN_IMAGE_GAME_FACTOR
|
||||
)
|
||||
|
||||
ImageTools.draw_gradient(
|
||||
noise_image,
|
||||
imageSize/2,
|
||||
central_zone_max_size / TERRAIN_IMAGE_GAME_FACTOR,
|
||||
Color.BLACK,
|
||||
true
|
||||
)
|
||||
|
||||
|
||||
ImageTools.flatten(noise_image, 0.5)
|
||||
|
||||
contamination = Image.create(
|
||||
imageSize.x,
|
||||
imageSize.y,
|
||||
false,
|
||||
Image.Format.FORMAT_L8
|
||||
)
|
||||
|
||||
contamination.copy_from(noise_image)
|
||||
|
||||
func impact_contamination(position : Vector2, impact_radius : int, to_value : float = 1.):
|
||||
ImageTools.draw_circle(
|
||||
contamination,
|
||||
position / TERRAIN_IMAGE_GAME_FACTOR,
|
||||
impact_radius / TERRAIN_IMAGE_GAME_FACTOR,
|
||||
Color(1., 1., 1., to_value)
|
||||
)
|
||||
|
||||
func get_contamination(point : Vector2) -> float:
|
||||
var pixel_point : Vector2 = Vector2(point) / float(TERRAIN_IMAGE_GAME_FACTOR)
|
||||
return contamination.get_pixel(
|
||||
int(round(pixel_point.x)),
|
||||
int(round(pixel_point.y))
|
||||
).r
|
||||
1
common/game_data/scripts/terrain_data.gd.uid
Normal file
1
common/game_data/scripts/terrain_data.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://cx30nvq8b34lj
|
||||
52
common/tools/scripts/image_tools.gd
Normal file
52
common/tools/scripts/image_tools.gd
Normal file
@@ -0,0 +1,52 @@
|
||||
class_name ImageTools
|
||||
|
||||
static func draw_circle(image: Image, center: Vector2i, length: int, color: Color = Color.WHITE):
|
||||
for x in range(image.get_width()):
|
||||
for y in range(image.get_height()):
|
||||
var center_distance = Vector2i(x, y).distance_to(center)
|
||||
|
||||
if (center_distance <= length):
|
||||
image.set_pixel(x, y, color)
|
||||
|
||||
|
||||
static func draw_gradient(image: Image, center: Vector2i, length: int, color: Color = Color.WHITE, inverse := false):
|
||||
for x in range(image.get_width()):
|
||||
for y in range(image.get_height()):
|
||||
var original_pixel_color = image.get_pixel(x, y)
|
||||
var center_distance = Vector2i(x, y).distance_to(center)
|
||||
|
||||
if (center_distance == 0):
|
||||
if not inverse:
|
||||
image.set_pixel(x, y, original_pixel_color.blend(color))
|
||||
else:
|
||||
var color_to_add = Color(color, 1 / (center_distance / length)) if not inverse else Color(color, center_distance / length)
|
||||
image.set_pixel(
|
||||
x,
|
||||
y,
|
||||
original_pixel_color.blend(color_to_add)
|
||||
)
|
||||
|
||||
static func flatten(image: Image, threshold := 0.5):
|
||||
for x in range(image.get_width()):
|
||||
for y in range(image.get_height()):
|
||||
var original_pixel_color = image.get_pixel(x, y)
|
||||
|
||||
if original_pixel_color.r > threshold:
|
||||
image.set_pixel(
|
||||
x,
|
||||
y,
|
||||
Color.WHITE
|
||||
)
|
||||
else:
|
||||
image.set_pixel(
|
||||
x,
|
||||
y,
|
||||
Color.BLACK
|
||||
)
|
||||
|
||||
static func copy(from: Image, to : Image):
|
||||
for x in range(from.get_width()):
|
||||
for y in range(from.get_height()):
|
||||
to.set_pixel(x, y, from.get_pixel(x, y))
|
||||
|
||||
|
||||
1
common/tools/scripts/image_tools.gd.uid
Normal file
1
common/tools/scripts/image_tools.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://b05d3may5qqtv
|
||||
Reference in New Issue
Block a user