GodotComponentTest/Entities/PlayerEntity.gd

88 lines
2.7 KiB
GDScript
Raw Normal View History

2022-08-19 18:48:54 +02:00
extends Node2D
2022-08-17 00:50:30 +02:00
onready var color_component = $Components/Color
onready var tinted_sprite_component = $Components/TintedSprite
onready var clickable_component = $Components/Clickable
2022-08-19 18:48:54 +02:00
onready var movable_component = $Components/Movable
2022-08-20 00:18:16 +02:00
onready var collision_component = $Components/Collision
2022-08-17 00:50:30 +02:00
onready var is_active = false
# Called when the node enters the scene tree for the first time.
func _ready():
if color_component and tinted_sprite_component:
print ("Connecting signals")
color_component.connect("color_changed", tinted_sprite_component, "set_color_tint")
if clickable_component:
clickable_component.connect("clicked", self, "_on_entity_clicked")
2022-08-19 18:48:54 +02:00
if movable_component:
movable_component.connect("position_updated", self, "_on_position_updated")
movable_component.target = self.transform.origin
movable_component.pos = self.transform.origin
2022-08-20 00:18:16 +02:00
if collision_component:
collision_component.transform.origin = self.transform.origin
2022-08-17 00:50:30 +02:00
2022-08-19 19:13:39 +02:00
2022-08-17 00:50:30 +02:00
func _process(delta):
if is_active and color_component:
var color = Color(
(sin(float(OS.get_system_time_msecs()) / 1000.0) + 1.0) / 2.0,
(sin(float(OS.get_system_time_msecs()) / 300.0) + 1.0) / 2.0,
(sin(float(OS.get_system_time_msecs()) / 100.0) + 1.0) / 2.0
)
color_component.set_color(color)
pass
2022-08-19 19:13:39 +02:00
2022-08-20 00:18:16 +02:00
func _physics_process(delta):
if not collision_component:
return
if movable_component and collision_component:
var target_position = movable_component.pos * 0.5
var translation = (target_position - transform.origin)
if translation.length_squared() < 0.001:
return
var target_velocity = translation / delta
var physics_velocity = collision_component.move_and_slide(target_velocity)
print ("targ pos: " + str(target_position))
print ("comp pos: " + str(collision_component.transform.origin))
print ("self pos: " + str(transform.origin))
transform.origin = collision_component.transform.origin
2022-08-17 00:50:30 +02:00
func _on_entity_clicked():
is_active = not is_active
2022-08-19 18:48:54 +02:00
2022-08-19 19:13:39 +02:00
2022-08-19 18:48:54 +02:00
func _on_position_updated(new_position):
2022-08-20 00:18:16 +02:00
if not collision_component:
transform.origin = new_position
update()
func ScreenToWorld(pos: Vector2, camera: Camera):
# pos = pos - OS.get_window_safe_area().size * 0.5
# pos = pos - get_viewport().size * 0.5
var project_size = Vector2(
ProjectSettings.get_setting("display/window/size/width"),
ProjectSettings.get_setting("display/window/size/height")
)
var window_size = OS.get_window_safe_area().size
var zoom_scale = window_size.x / window_size.x
pos = pos
return pos
2022-08-19 18:48:54 +02:00
2022-08-19 19:13:39 +02:00
func _unhandled_input(event):
if event is InputEventMouseButton and event.pressed:
assert(movable_component)
2022-08-20 00:18:16 +02:00
var world_pos = ScreenToWorld(event.position, get_viewport().get_camera())
movable_component.target = world_pos