Working on physics interaction

WorldChunkRefactoring
Martin Felis 2022-08-20 00:18:16 +02:00
parent b3920b8975
commit c25da8f903
9 changed files with 246 additions and 9 deletions

BIN
Assets/white.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 546 B

35
Assets/white.png.import Normal file
View File

@ -0,0 +1,35 @@
[remap]
importer="texture"
type="StreamTexture"
path="res://.import/white.png-63bf30d1e697e9c6c58b33e6327f2179.stex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://Assets/white.png"
dest_files=[ "res://.import/white.png-63bf30d1e697e9c6c58b33e6327f2179.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

View File

@ -0,0 +1,16 @@
extends Node2D
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass

View File

@ -32,3 +32,6 @@ func _process(delta):
vel = vel * max_speed / speed
emit_signal("position_updated", pos)
func _draw():
draw_circle (target, 10, Color(0.9, 0.2, 0.2))

View File

@ -4,6 +4,7 @@ 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 is_active = false
@ -20,6 +21,9 @@ 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):
@ -33,17 +37,51 @@ func _process(delta):
pass
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
func _on_entity_clicked():
is_active = not is_active
func _on_position_updated(new_position):
transform.origin = new_position
update()
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
func _unhandled_input(event):
if event is InputEventMouseButton and event.pressed:
assert(movable_component)
movable_component.target = event.position
var world_pos = ScreenToWorld(event.position, get_viewport().get_camera())
movable_component.target = world_pos

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=7 format=2]
[gd_scene load_steps=10 format=2]
[ext_resource path="res://Entities/PlayerEntity.gd" type="Script" id=1]
[ext_resource path="res://Components/TintedSpriteComponent.gd" type="Script" id=2]
@ -6,6 +6,13 @@
[ext_resource path="res://Components/ClickableComponent.tscn" type="PackedScene" id=4]
[ext_resource path="res://Components/MovableComponent.gd" type="Script" id=5]
[ext_resource path="res://Assets/pirate.svg" type="Texture" id=6]
[ext_resource path="res://Assets/white.png" type="Texture" id=8]
[sub_resource type="RectangleShape2D" id=1]
extents = Vector2( 40, 103 )
[sub_resource type="RectangleShape2D" id=2]
extents = Vector2( 41, 107.5 )
[node name="PlayerEntity" type="Node2D"]
script = ExtResource( 1 )
@ -14,13 +21,30 @@ script = ExtResource( 1 )
[node name="Movable" type="Node2D" parent="Components"]
script = ExtResource( 5 )
max_speed = 1000
[node name="Clickable" parent="Components" instance=ExtResource( 4 )]
max_speed = 300
[node name="TintedSprite" type="Sprite" parent="Components"]
visible = false
texture = ExtResource( 6 )
script = ExtResource( 2 )
[node name="Clickable" parent="Components" instance=ExtResource( 4 )]
[node name="CollisionShape2D" parent="Components/Clickable/Area2D" index="0"]
position = Vector2( 7, -8 )
shape = SubResource( 1 )
[node name="Color" type="Node" parent="Components"]
script = ExtResource( 3 )
[node name="Collision" type="KinematicBody2D" parent="Components"]
[node name="Shape" type="CollisionShape2D" parent="Components/Collision"]
shape = SubResource( 2 )
[node name="TintedSprite" type="Sprite" parent="Components/Collision/Shape"]
scale = Vector2( 80.6324, 216.834 )
texture = ExtResource( 8 )
script = ExtResource( 2 )
[editable path="Components/Clickable"]

View File

@ -1,9 +1,16 @@
[gd_scene load_steps=4 format=2]
[gd_scene load_steps=8 format=2]
[ext_resource path="res://Entities/SimpleEntity.tscn" type="PackedScene" id=1]
[ext_resource path="res://Entities/PlayerEntity.tscn" type="PackedScene" id=2]
[ext_resource path="res://Assets/white.png" type="Texture" id=3]
[ext_resource path="res://Materials/PhysicsObjects.tres" type="Material" id=4]
[ext_resource path="res://Game.gd" type="Script" id=6]
[sub_resource type="RectangleShape2D" id=1]
extents = Vector2( 57.5347, 51.5794 )
[sub_resource type="RectangleShape2D" id=2]
[node name="Game" type="Node2D"]
script = ExtResource( 6 )
@ -45,7 +52,43 @@ position = Vector2( 157, 306 )
color = Color( 0, 0.0156863, 1, 1 )
[node name="PlayerEntity" parent="." instance=ExtResource( 2 )]
position = Vector2( 263, 225 )
[node name="World" type="Node2D" parent="."]
[node name="Wall" type="KinematicBody2D" parent="World"]
position = Vector2( 582, 817 )
scale = Vector2( 7.56, 1 )
__meta__ = {
"_edit_group_": true
}
[node name="Sprite" type="Sprite" parent="World/Wall"]
material = ExtResource( 4 )
position = Vector2( 0.965286, 0.579379 )
scale = Vector2( 115, 104 )
texture = ExtResource( 3 )
[node name="CollisionShape2D" type="CollisionShape2D" parent="World/Wall"]
position = Vector2( 0.465286, 0.57938 )
z_as_relative = false
shape = SubResource( 1 )
[node name="RigidBody2D" type="RigidBody2D" parent="World"]
position = Vector2( 649, 227 )
__meta__ = {
"_edit_group_": true
}
[node name="CollisionShape2D" type="CollisionShape2D" parent="World/RigidBody2D"]
rotation = -0.432491
scale = Vector2( 11.5039, 4.3795 )
shape = SubResource( 2 )
[node name="Sprite" type="Sprite" parent="World/RigidBody2D/CollisionShape2D"]
material = ExtResource( 4 )
use_parent_material = true
scale = Vector2( 20.002, 19.4893 )
texture = ExtResource( 3 )
[connection signal="pressed" from="Control/AddEntityButton" to="." method="_on_AddEntityButton_pressed"]
[connection signal="pressed" from="Control/AddWanderingEntity" to="." method="_on_AddWanderingEntity_pressed"]

View File

@ -0,0 +1,73 @@
[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 )
[resource]
shader = SubResource( 4 )
shader_param/TextureUniform = ExtResource( 1 )

View File

@ -42,6 +42,11 @@ config/name="GodotComponentTest"
run/main_scene="res://Game.tscn"
config/icon="res://icon.png"
[display]
window/size/width=1280
window/size/height=768
[physics]
common/enable_pause_aware_picking=true