untitled-plant-game/World/Plant.gd

144 lines
3.5 KiB
GDScript3
Raw Permalink Normal View History

2021-12-03 23:12:22 +00:00
extends Node2D
2021-12-04 15:14:06 +00:00
export var size = Vector2()
2021-12-03 23:12:22 +00:00
# 2 dimensional array y and x
2021-12-04 00:20:48 +00:00
var pixels_x = []
2021-12-04 15:14:06 +00:00
var active = []
var rng
2021-12-04 20:02:52 +00:00
var branches = 4
2021-12-03 23:12:22 +00:00
2021-12-05 19:26:10 +00:00
var water = 20
2021-12-05 16:31:10 +00:00
var health = 100
var social = 50
var light = 50
2021-12-06 02:12:54 +00:00
var last_need = ["x", -100, -100]
2021-12-05 16:31:10 +00:00
2021-12-06 02:12:54 +00:00
var was_silent = false
signal good_water()
2021-12-05 16:31:10 +00:00
signal has_need(need_and_value)
func get_mood() -> int:
return (
abs(50 - water) * 2 +
abs(50 - light) * 2 +
(100 - social) +
(100 - health))
2021-12-04 17:37:24 +00:00
func _process(_delta):
2021-12-04 22:16:30 +00:00
#step()
2021-12-04 16:45:59 +00:00
pass
2021-12-04 16:19:43 +00:00
2021-12-05 16:31:10 +00:00
2021-12-04 16:19:43 +00:00
func step():
2021-12-06 02:12:54 +00:00
$Timer.wait_time = abs(float(get_mood()) / 100) + 0.25
2021-12-04 16:19:43 +00:00
call_deferred("grow")
2021-12-06 03:13:10 +00:00
if (randi() % 150 == 0) && (get_mood() < 50):
2021-12-04 20:02:52 +00:00
blossom()
2021-12-05 16:31:10 +00:00
communicate_needs()
func communicate_needs():
2021-12-06 02:12:54 +00:00
var needs = [abs((50 - water) * 2), 100 - health, 100 - social, abs((50 - light) * 2)]
var needs_for_real = [water, health, social, light]
var most_important = ["cool name sample text", needs[0], needs_for_real[0]]
2021-12-05 16:31:10 +00:00
var i = 0
for need in needs:
if most_important[1] <= need:
most_important[1] = need
2021-12-06 02:12:54 +00:00
most_important[2] = needs_for_real[i]
2021-12-05 16:31:10 +00:00
most_important[0] = i
i += 1
var names = ["water", "health", "social", "light"]
most_important[0] = names[most_important[0]]
2021-12-06 02:12:54 +00:00
if (last_need[0] != most_important[0]) || (abs(last_need[1] - most_important[1]) >= 10) || was_silent:
2021-12-05 16:31:10 +00:00
emit_signal("has_need", most_important)
last_need = most_important
2021-12-06 02:12:54 +00:00
was_silent = false
$TalkTimer.start(15)
2021-12-05 16:31:10 +00:00
2021-12-04 16:19:43 +00:00
2021-12-03 23:12:22 +00:00
func _ready():
2021-12-04 15:14:06 +00:00
randomize()
rng = RandomNumberGenerator.new()
2021-12-04 20:02:52 +00:00
rng.randomize()
2021-12-04 15:14:06 +00:00
size.x = ProjectSettings.get_setting("display/window/size/width") / 2
size.y = ProjectSettings.get_setting("display/window/size/height")
2021-12-03 23:12:22 +00:00
call_deferred("spawn_pixels")
2021-12-04 00:20:48 +00:00
call_deferred("plant_seed")
2021-12-04 20:02:52 +00:00
branches = rng.randi_range(2, 24)
print(branches)
2021-12-03 23:12:22 +00:00
2021-12-05 16:31:10 +00:00
2021-12-03 23:12:22 +00:00
func spawn_pixels():
for n in size.x:
2021-12-04 00:20:48 +00:00
var pixels_y = []
2021-12-03 23:12:22 +00:00
for i in size.y:
var Main = get_tree().current_scene
var Pixel = preload("res://World/Pixel.tscn")
var pixel = Pixel.instance()
# set position
pixel.rect_global_position = Vector2(n, i)
2021-12-04 00:20:48 +00:00
pixel.position = Vector2(n, i)
2021-12-03 23:12:22 +00:00
# show in scene
Main.add_child(pixel)
# initilize array
pixels_y.append(pixel)
pixels_x.append(pixels_y)
2021-12-05 16:31:10 +00:00
2021-12-04 00:20:48 +00:00
func plant_seed():
2021-12-06 19:32:17 +00:00
var plant_seed = pixels_x[size.x / 2][size.y - 12]
2021-12-04 15:14:06 +00:00
plant_seed.active = true
active.append(plant_seed)
2021-12-05 16:31:10 +00:00
2021-12-04 00:20:48 +00:00
func grow():
2021-12-04 20:02:52 +00:00
if (randi() % branches) >= 1:
2021-12-04 15:14:06 +00:00
new_pixel(active[active.size() - 1])
else:
new_pixel(active[randi() % active.size()])
2021-12-05 16:31:10 +00:00
2021-12-04 15:14:06 +00:00
func new_pixel(original_pixel):
var var_x = rng.randi_range(-1, 1)
2021-12-04 16:19:43 +00:00
var var_y
2021-12-04 16:45:59 +00:00
#grow down
if (randi() % 2) == 0:
2021-12-04 16:19:43 +00:00
var_y = rng.randi_range(-1, 1)
# grow regularly
else: var_y = rng.randi_range(-1, 0)
2021-12-04 15:14:06 +00:00
var new_position = Vector2(original_pixel.position)
new_position.x = clamp(new_position.x + var_x, 0, pixels_x.size() - 1)
new_position.y = clamp(new_position.y + var_y, 0, pixels_x[new_position.x].size() - 1)
var new_pixel = pixels_x[new_position.x][new_position.y]
if !new_pixel.active:
active.append(new_pixel)
new_pixel.age_up()
2021-12-05 16:31:10 +00:00
2021-12-04 20:02:52 +00:00
func blossom():
var Flower = preload("res://World/Flower.tscn")
var flower = Flower.instance()
var main = get_tree().current_scene
flower.global_position = active[randi() % active.size()].position
main.add_child(flower)
2021-12-05 16:31:10 +00:00
func _on_WaterTimer_timeout() -> void:
water = clamp(water - 1, 0, 100)
2021-12-06 02:12:54 +00:00
social = clamp(social - 2, 0, 100)
2021-12-05 19:26:10 +00:00
func add_water():
water = clamp(water + 1, 0, 100)
2021-12-06 02:12:54 +00:00
if water == 55:
$WaterSFX.play(0)
emit_signal("good_water")
func _on_TalkTimer_timeout() -> void:
was_silent = true
func _on_Terminal_was_social() -> void:
social += clamp(social + randi() % 11, 0, 10)