mirror of
https://github.com/godotengine/godot-demo-projects.git
synced 2025-12-16 13:30:07 +01:00
Format files using updated file_format.sh
This commit is contained in:
@@ -24,44 +24,44 @@ func _process(delta):
|
||||
var ball_pos = ball.get_position()
|
||||
var left_rect = Rect2(left_paddle.get_position() - pad_size * 0.5, pad_size)
|
||||
var right_rect = Rect2(right_paddle.get_position() - pad_size * 0.5, pad_size)
|
||||
|
||||
|
||||
# Integrate new ball postion.
|
||||
ball_pos += direction * ball_speed * delta
|
||||
|
||||
|
||||
# Flip when touching roof or floor.
|
||||
if (ball_pos.y < 0 and direction.y < 0) or (ball_pos.y > screen_size.y and direction.y > 0):
|
||||
direction.y = -direction.y
|
||||
|
||||
|
||||
# Flip, change direction and increase speed when touching pads.
|
||||
if (left_rect.has_point(ball_pos) and direction.x < 0) or (right_rect.has_point(ball_pos) and direction.x > 0):
|
||||
direction.x = -direction.x
|
||||
ball_speed *= 1.1
|
||||
direction.y = randf() * 2.0 - 1
|
||||
direction = direction.normalized()
|
||||
|
||||
|
||||
# Check gameover.
|
||||
if ball_pos.x < 0 or ball_pos.x > screen_size.x:
|
||||
ball_pos = screen_size * 0.5
|
||||
ball_speed = INITIAL_BALL_SPEED
|
||||
direction = Vector2(-1, 0)
|
||||
|
||||
|
||||
ball.set_position(ball_pos)
|
||||
|
||||
|
||||
# Move left pad.
|
||||
var left_pos = left_paddle.get_position()
|
||||
|
||||
|
||||
if left_pos.y > 0 and Input.is_action_pressed("left_move_up"):
|
||||
left_pos.y += -PAD_SPEED * delta
|
||||
if left_pos.y < screen_size.y and Input.is_action_pressed("left_move_down"):
|
||||
left_pos.y += PAD_SPEED * delta
|
||||
|
||||
|
||||
left_paddle.set_position(left_pos)
|
||||
|
||||
|
||||
# Move right pad.
|
||||
var right_pos = right_paddle.get_position()
|
||||
if right_pos.y > 0 and Input.is_action_pressed("right_move_up"):
|
||||
right_pos.y += -PAD_SPEED * delta
|
||||
if right_pos.y < screen_size.y and Input.is_action_pressed("right_move_down"):
|
||||
right_pos.y += PAD_SPEED * delta
|
||||
|
||||
|
||||
right_paddle.set_position(right_pos)
|
||||
|
||||
@@ -36,9 +36,9 @@ onready var camera2 = viewport2.get_node(@"Camera2")
|
||||
func _ready():
|
||||
_on_size_changed()
|
||||
_update_splitscreen()
|
||||
|
||||
|
||||
get_viewport().connect("size_changed", self, "_on_size_changed")
|
||||
|
||||
|
||||
view.material.set_shader_param("viewport1", viewport1.get_texture())
|
||||
view.material.set_shader_param("viewport2", viewport2.get_texture())
|
||||
|
||||
@@ -50,7 +50,7 @@ func _process(_delta):
|
||||
|
||||
func _move_cameras():
|
||||
var position_difference = _compute_position_difference_in_world()
|
||||
|
||||
|
||||
var distance = clamp(_compute_horizontal_length(position_difference), 0, max_separation)
|
||||
|
||||
position_difference = position_difference.normalized() * distance
|
||||
@@ -66,7 +66,7 @@ func _update_splitscreen():
|
||||
var screen_size = get_viewport().get_visible_rect().size
|
||||
var player1_position = camera1.unproject_position(player1.translation) / screen_size
|
||||
var player2_position = camera2.unproject_position(player2.translation) / screen_size
|
||||
|
||||
|
||||
var thickness
|
||||
if adaptive_split_line_thickness:
|
||||
var position_difference = _compute_position_difference_in_world()
|
||||
@@ -75,7 +75,7 @@ func _update_splitscreen():
|
||||
thickness = clamp(thickness, 0, split_line_thickness)
|
||||
else:
|
||||
thickness = split_line_thickness
|
||||
|
||||
|
||||
view.material.set_shader_param("split_active", _get_split_state())
|
||||
view.material.set_shader_param("player1_position", player1_position)
|
||||
view.material.set_shader_param("player2_position", player2_position)
|
||||
@@ -93,10 +93,10 @@ func _get_split_state():
|
||||
|
||||
func _on_size_changed():
|
||||
var screen_size = get_viewport().get_visible_rect().size
|
||||
|
||||
|
||||
$Viewport1.size = screen_size
|
||||
$Viewport2.size = screen_size
|
||||
|
||||
|
||||
view.material.set_shader_param("viewport_size", screen_size)
|
||||
|
||||
|
||||
|
||||
@@ -12,5 +12,5 @@ func _physics_process(_delta):
|
||||
velocity.z += Input.get_action_strength("move_down_player" + str(player_id))
|
||||
velocity.x = -Input.get_action_strength("move_left_player" + str(player_id))
|
||||
velocity.x += Input.get_action_strength("move_right_player" + str(player_id))
|
||||
|
||||
|
||||
move_and_slide(velocity.normalized() * walk_speed)
|
||||
|
||||
@@ -24,22 +24,22 @@ void fragment() {
|
||||
|
||||
float width = viewport_size.x;
|
||||
float height = viewport_size.y;
|
||||
|
||||
|
||||
if (split_active) {
|
||||
vec2 dx = player2_position - player1_position;
|
||||
float split_slope;
|
||||
|
||||
|
||||
if (dx.y != 0.0) {
|
||||
split_slope = dx.x / dx.y;
|
||||
} else {
|
||||
split_slope = 100000.0; // High value (vertical split) if dx.y = 0
|
||||
}
|
||||
|
||||
|
||||
vec2 split_origin = vec2(0.5, 0.5);
|
||||
vec2 split_line_start = vec2(0.0, height * ((split_origin.x - 0.0) * split_slope + split_origin.y));
|
||||
vec2 split_line_end = vec2(width, height * ((split_origin.x - 1.0) * split_slope + split_origin.y));
|
||||
float distance_to_split_line = distanceToLine(split_line_start, split_line_end, vec2(UV.x * width, UV.y * height));
|
||||
|
||||
|
||||
// Draw split border if close enough
|
||||
if (distance_to_split_line < split_line_thickness) {
|
||||
COLOR = split_line_color;
|
||||
|
||||
@@ -10,5 +10,5 @@ func _ready():
|
||||
for wall in walls:
|
||||
var material = SpatialMaterial.new()
|
||||
material.albedo_color = Color(randf(), randf(), randf())
|
||||
|
||||
|
||||
wall.material_override = material
|
||||
|
||||
@@ -17,7 +17,7 @@ onready var node_area = $Quad/Area
|
||||
|
||||
func _ready():
|
||||
node_area.connect("mouse_entered", self, "_mouse_entered_area")
|
||||
|
||||
|
||||
# If the material is NOT set to use billboard settings, then avoid running billboard specific code
|
||||
if node_quad.get_surface_material(0).params_billboard_mode == 0:
|
||||
set_process(false)
|
||||
@@ -39,7 +39,7 @@ func _unhandled_input(event):
|
||||
if event is mouse_event:
|
||||
is_mouse_event = true
|
||||
break
|
||||
|
||||
|
||||
# If the event is a mouse/touch event and/or the mouse is either held or inside the area, then
|
||||
# we need to do some additional processing in the handle_mouse function before passing the event to the viewport.
|
||||
# If the event is not a mouse/touch event, then we can just pass the event directly to the viewport.
|
||||
@@ -53,14 +53,14 @@ func _unhandled_input(event):
|
||||
func handle_mouse(event):
|
||||
# Get mesh size to detect edges and make conversions. This code only support PlaneMesh and QuadMesh.
|
||||
quad_mesh_size = node_quad.mesh.size
|
||||
|
||||
|
||||
# Detect mouse being held to mantain event while outside of bounds. Avoid orphan clicks
|
||||
if event is InputEventMouseButton or event is InputEventScreenTouch:
|
||||
is_mouse_held = event.pressed
|
||||
|
||||
|
||||
# Find mouse position in Area
|
||||
var mouse_pos3D = find_mouse(event.global_position)
|
||||
|
||||
|
||||
# Check if the mouse is outside of bounds, use last position to avoid errors
|
||||
# NOTE: mouse_exited signal was unrealiable in this situation
|
||||
is_mouse_inside = mouse_pos3D != null
|
||||
@@ -73,12 +73,12 @@ func handle_mouse(event):
|
||||
mouse_pos3D = last_mouse_pos3D
|
||||
if mouse_pos3D == null:
|
||||
mouse_pos3D = Vector3.ZERO
|
||||
|
||||
|
||||
# TODO: adapt to bilboard mode or avoid completely
|
||||
|
||||
|
||||
# convert the relative event position from 3D to 2D
|
||||
var mouse_pos2D = Vector2(mouse_pos3D.x, -mouse_pos3D.y)
|
||||
|
||||
|
||||
# Right now the event position's range is the following: (-quad_size/2) -> (quad_size/2)
|
||||
# We need to convert it into the following range: 0 -> quad_size
|
||||
mouse_pos2D.x += quad_mesh_size.x / 2
|
||||
@@ -86,16 +86,16 @@ func handle_mouse(event):
|
||||
# Then we need to convert it into the following range: 0 -> 1
|
||||
mouse_pos2D.x = mouse_pos2D.x / quad_mesh_size.x
|
||||
mouse_pos2D.y = mouse_pos2D.y / quad_mesh_size.y
|
||||
|
||||
|
||||
# Finally, we convert the position to the following range: 0 -> viewport.size
|
||||
mouse_pos2D.x = mouse_pos2D.x * node_viewport.size.x
|
||||
mouse_pos2D.y = mouse_pos2D.y * node_viewport.size.y
|
||||
# We need to do these conversions so the event's position is in the viewport's coordinate system.
|
||||
|
||||
|
||||
# Set the event's position and global position.
|
||||
event.position = mouse_pos2D
|
||||
event.global_position = mouse_pos2D
|
||||
|
||||
|
||||
# If the event is a mouse motion event...
|
||||
if event is InputEventMouseMotion:
|
||||
# If there is not a stored previous position, then we'll assume there is no relative motion.
|
||||
@@ -107,23 +107,23 @@ func handle_mouse(event):
|
||||
event.relative = mouse_pos2D - last_mouse_pos2D
|
||||
# Update last_mouse_pos2D with the position we just calculated.
|
||||
last_mouse_pos2D = mouse_pos2D
|
||||
|
||||
|
||||
# Finally, send the processed input event to the viewport.
|
||||
node_viewport.input(event)
|
||||
|
||||
|
||||
func find_mouse(global_position):
|
||||
var camera = get_viewport().get_camera()
|
||||
|
||||
|
||||
# From camera center to the mouse position in the Area
|
||||
var from = camera.project_ray_origin(global_position)
|
||||
var dist = find_further_distance_to(camera.transform.origin)
|
||||
var to = from + camera.project_ray_normal(global_position) * dist
|
||||
|
||||
|
||||
|
||||
|
||||
# Manually raycasts the are to find the mouse position
|
||||
var result = get_world().direct_space_state.intersect_ray(from, to, [], node_area.collision_layer,false,true) #for 3.1 changes
|
||||
|
||||
|
||||
if result.size() > 0:
|
||||
return result.position
|
||||
else:
|
||||
@@ -137,7 +137,7 @@ func find_further_distance_to(origin):
|
||||
edges.append(node_area.to_global(Vector3(quad_mesh_size.x / 2, -quad_mesh_size.y / 2, 0)))
|
||||
edges.append(node_area.to_global(Vector3(-quad_mesh_size.x / 2, quad_mesh_size.y / 2, 0)))
|
||||
edges.append(node_area.to_global(Vector3(-quad_mesh_size.x / 2, -quad_mesh_size.y / 2, 0)))
|
||||
|
||||
|
||||
# Get the furthest distance between the camera and collision to avoid raycasting too far or too short
|
||||
var far_dist = 0
|
||||
var temp_dist
|
||||
@@ -145,13 +145,13 @@ func find_further_distance_to(origin):
|
||||
temp_dist = origin.distance_to(edge)
|
||||
if temp_dist > far_dist:
|
||||
far_dist = temp_dist
|
||||
|
||||
|
||||
return far_dist
|
||||
|
||||
|
||||
func rotate_area_to_billboard():
|
||||
var billboard_mode = node_quad.get_surface_material(0).params_billboard_mode
|
||||
|
||||
|
||||
# Try to match the area with the material's billboard setting, if enabled
|
||||
if billboard_mode > 0:
|
||||
# Get the camera
|
||||
@@ -159,12 +159,12 @@ func rotate_area_to_billboard():
|
||||
# Look in the same direction as the camera
|
||||
var look = camera.to_global(Vector3(0, 0, -100)) - camera.global_transform.origin
|
||||
look = node_area.translation + look
|
||||
|
||||
|
||||
# Y-Billboard: Lock Y rotation, but gives bad results if the camera is tilted.
|
||||
if billboard_mode == 2:
|
||||
look = Vector3(look.x, 0, look.z)
|
||||
|
||||
|
||||
node_area.look_at(look, Vector3.UP)
|
||||
|
||||
|
||||
# Rotate in the Z axis to compensate camera tilt
|
||||
node_area.rotate_object_local(Vector3.BACK, camera.rotation.z)
|
||||
|
||||
Reference in New Issue
Block a user