Fixed player movement

WorldChunkRefactoring
Martin Felis 2022-08-20 14:36:17 +02:00
parent c25da8f903
commit 0b48746635
10 changed files with 78 additions and 145 deletions

View File

@ -33,5 +33,6 @@ func _process(delta):
emit_signal("position_updated", pos)
func _draw():
draw_circle (target, 10, Color(0.9, 0.2, 0.2))

View File

@ -1,14 +1,14 @@
extends Node2D
extends KinematicBody2D
onready var color_component = $Components/Color
onready var tinted_sprite_component = $Components/TintedSprite
onready var clickable_component = $Components/Clickable
onready var movable_component = $Components/Movable
onready var collision_component = $Components/Collision
onready var color_component = $Color
onready var tinted_sprite_component = $TintedSprite
onready var clickable_component = $Clickable
onready var movable_component = $Movable
onready var collision_component = $Collision
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")
@ -21,9 +21,6 @@ func _ready():
movable_component.connect("position_updated", self, "_on_position_updated")
movable_component.target = self.transform.origin
movable_component.pos = self.transform.origin
if collision_component:
collision_component.transform.origin = self.transform.origin
func _process(delta):
@ -42,18 +39,12 @@ func _physics_process(delta):
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:
if movable_component.vel.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
var physics_velocity = move_and_slide(movable_component.vel)
movable_component.pos = self.position
movable_component.vel = physics_velocity
func _on_entity_clicked():
@ -65,23 +56,9 @@ func _on_position_updated(new_position):
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
func _unhandled_input(event):
if event is InputEventMouseButton and event.pressed:
assert(movable_component)
var world_pos = ScreenToWorld(event.position, get_viewport().get_camera())
movable_component.target = world_pos
movable_component.target = event.position

View File

@ -11,40 +11,40 @@
[sub_resource type="RectangleShape2D" id=1]
extents = Vector2( 40, 103 )
[sub_resource type="RectangleShape2D" id=2]
extents = Vector2( 41, 107.5 )
[sub_resource type="CapsuleShape2D" id=2]
radius = 46.0
height = 122.0
[node name="PlayerEntity" type="Node2D"]
[node name="PlayerEntity" type="KinematicBody2D"]
script = ExtResource( 1 )
[node name="Components" type="Node2D" parent="."]
[node name="Movable" type="Node2D" parent="Components"]
[node name="Movable" type="Node2D" parent="."]
script = ExtResource( 5 )
max_speed = 300
[node name="TintedSprite" type="Sprite" parent="Components"]
visible = false
[node name="TintedSprite" type="Sprite" parent="."]
position = Vector2( -9, 3 )
texture = ExtResource( 6 )
script = ExtResource( 2 )
[node name="Clickable" parent="Components" instance=ExtResource( 4 )]
[node name="Clickable" parent="." instance=ExtResource( 4 )]
[node name="CollisionShape2D" parent="Components/Clickable/Area2D" index="0"]
[node name="CollisionShape2D" parent="Clickable/Area2D" index="0"]
position = Vector2( 7, -8 )
shape = SubResource( 1 )
[node name="Color" type="Node" parent="Components"]
[node name="Color" type="Node" parent="."]
script = ExtResource( 3 )
[node name="Collision" type="KinematicBody2D" parent="Components"]
[node name="Shape" type="CollisionShape2D" parent="Components/Collision"]
[node name="Collision" type="CollisionShape2D" parent="."]
shape = SubResource( 2 )
[node name="TintedSprite" type="Sprite" parent="Components/Collision/Shape"]
[node name="TintedSprite" type="Sprite" parent="Collision"]
visible = false
scale = Vector2( 80.6324, 216.834 )
texture = ExtResource( 8 )
script = ExtResource( 2 )
[editable path="Components/Clickable"]
[editable path="Clickable"]

View File

@ -1,9 +1,9 @@
extends Node2D
onready var color_component = $Components/Color
onready var tinted_sprite_component = $Components/TintedSprite
onready var clickable_component = $Components/Clickable
onready var movable_component = $Components/Movable
onready var color_component = $Color
onready var tinted_sprite_component = $TintedSprite
onready var clickable_component = $Clickable
onready var movable_component = $Movable
onready var is_active = false

View File

@ -9,14 +9,12 @@
[node name="SimpleEntity" type="Node2D"]
script = ExtResource( 2 )
[node name="Components" type="Node2D" parent="."]
[node name="Clickable" parent="." instance=ExtResource( 4 )]
[node name="Clickable" parent="Components" instance=ExtResource( 4 )]
[node name="TintedSprite" type="Sprite" parent="Components"]
[node name="TintedSprite" type="Sprite" parent="."]
texture = ExtResource( 5 )
script = ExtResource( 1 )
[node name="Color" type="Node" parent="Components"]
[node name="Color" type="Node" parent="."]
script = ExtResource( 3 )
color = Color( 1, 1, 1, 1 )

View File

@ -1,9 +1,9 @@
extends Node2D
onready var color_component = $Components/Color
onready var tinted_sprite_component = $Components/TintedSprite
onready var clickable_component = $Components/Clickable
onready var movable_component = $Components/Movable
onready var color_component = $Color
onready var tinted_sprite_component = $TintedSprite
onready var clickable_component = $Clickable
onready var movable_component = $Movable
onready var is_active = false

View File

@ -10,16 +10,14 @@
[node name="WanderingEntity" type="Node2D"]
script = ExtResource( 2 )
[node name="Components" type="Node2D" parent="."]
[node name="Movable" type="Node2D" parent="Components"]
[node name="Movable" type="Node2D" parent="."]
script = ExtResource( 6 )
[node name="Clickable" parent="Components" instance=ExtResource( 4 )]
[node name="Clickable" parent="." instance=ExtResource( 4 )]
[node name="TintedSprite" type="Sprite" parent="Components"]
[node name="TintedSprite" type="Sprite" parent="."]
texture = ExtResource( 5 )
script = ExtResource( 1 )
[node name="Color" type="Node" parent="Components"]
[node name="Color" type="Node" parent="."]
script = ExtResource( 3 )

View File

@ -3,6 +3,8 @@ extends Node2D
onready var SimpleEntity = preload("res://Entities/SimpleEntity.tscn")
onready var WanderingEntity = preload("res://Entities/WanderingEntity.tscn")
onready var player_movable_target_label = $UI/PlayerMovableTarget
onready var player = $PlayerEntity
onready var entities = $Entities
signal world_location_clicked
@ -10,6 +12,11 @@ signal world_location_clicked
# Called when the node enters the scene tree for the first time.
func _ready():
pass
func _process(delta):
player_movable_target_label.text = str(player.movable_component.target)
func _on_AddEntityButton_pressed():
var entity_instance = SimpleEntity.instance()

View File

@ -42,15 +42,9 @@ __meta__ = {
[node name="SimpleEntity" parent="Entities" instance=ExtResource( 1 )]
position = Vector2( 360, 118 )
[node name="Color" parent="Entities/SimpleEntity/Components" index="2"]
color = Color( 0.298039, 0.745098, 0.368627, 1 )
[node name="SimpleEntity2" parent="Entities" instance=ExtResource( 1 )]
position = Vector2( 157, 306 )
[node name="Color" parent="Entities/SimpleEntity2/Components" index="2"]
color = Color( 0, 0.0156863, 1, 1 )
[node name="PlayerEntity" parent="." instance=ExtResource( 2 )]
[node name="World" type="Node2D" parent="."]
@ -90,6 +84,34 @@ use_parent_material = true
scale = Vector2( 20.002, 19.4893 )
texture = ExtResource( 3 )
[node name="Wall2" type="KinematicBody2D" parent="World"]
position = Vector2( 896, 386 )
scale = Vector2( 7.56, 1 )
__meta__ = {
"_edit_group_": true
}
[node name="Sprite" type="Sprite" parent="World/Wall2"]
material = ExtResource( 4 )
position = Vector2( 0.965286, 0.579379 )
scale = Vector2( 115, 104 )
texture = ExtResource( 3 )
[node name="CollisionShape2D" type="CollisionShape2D" parent="World/Wall2"]
position = Vector2( 0.465286, 0.57938 )
z_as_relative = false
shape = SubResource( 1 )
[node name="UI" type="CanvasLayer" parent="."]
[node name="PlayerMovableTarget" type="Label" parent="UI"]
margin_right = 40.0
margin_bottom = 14.0
text = "Blaaa"
__meta__ = {
"_edit_use_anchors_": false
}
[connection signal="pressed" from="Control/AddEntityButton" to="." method="_on_AddEntityButton_pressed"]
[connection signal="pressed" from="Control/AddWanderingEntity" to="." method="_on_AddWanderingEntity_pressed"]

View File

@ -1,73 +1,3 @@
[gd_resource type="ShaderMaterial" load_steps=7 format=2]
[ext_resource path="res://Assets/white.png" type="Texture" id=1]
[sub_resource type="VisualShaderNodeColorConstant" id=2]
constant = Color( 0.953125, 0.0670166, 0.0670166, 1 )
[sub_resource type="VisualShaderNodeTextureUniform" id=3]
uniform_name = "TextureUniform"
[sub_resource type="VisualShaderNodeInput" id=6]
input_name = "fragcoord"
[sub_resource type="VisualShaderNodeVectorOp" id=7]
operator = 2
[sub_resource type="VisualShader" id=4]
code = "shader_type canvas_item;
uniform sampler2D TextureUniform;
void vertex() {
// Output:0
}
void fragment() {
// Color:2
vec3 n_out2p0 = vec3(0.953125, 0.067017, 0.067017);
float n_out2p1 = 1.000000;
// Input:4
vec3 n_out4p0 = FRAGCOORD.xyz;
// TextureUniform:3
vec3 n_out3p0;
float n_out3p1;
{
vec4 n_tex_read = texture(TextureUniform, n_out4p0.xy);
n_out3p0 = n_tex_read.rgb;
n_out3p1 = n_tex_read.a;
}
// VectorOp:5
vec3 n_out5p0 = n_out2p0 * n_out3p0;
// Output:0
COLOR.rgb = n_out5p0;
}
void light() {
// Output:0
}
"
graph_offset = Vector2( -314, -142.5 )
mode = 1
flags/light_only = false
nodes/fragment/2/node = SubResource( 2 )
nodes/fragment/2/position = Vector2( -20, 180 )
nodes/fragment/3/node = SubResource( 3 )
nodes/fragment/3/position = Vector2( -40, 360 )
nodes/fragment/4/node = SubResource( 6 )
nodes/fragment/4/position = Vector2( -260, 360 )
nodes/fragment/5/node = SubResource( 7 )
nodes/fragment/5/position = Vector2( 200, 240 )
nodes/fragment/connections = PoolIntArray( 2, 0, 5, 0, 3, 0, 5, 1, 5, 0, 0, 0, 4, 0, 3, 0 )
[gd_resource type="ShaderMaterial" format=2]
[resource]
shader = SubResource( 4 )
shader_param/TextureUniform = ExtResource( 1 )