2024-01-27 15:58:16 +01:00
|
|
|
class_name World extends Node3D
|
|
|
|
|
|
|
|
@export var players: Array
|
|
|
|
|
|
|
|
@onready var score_update_timer: Timer = $ScoreUpdateTimer
|
|
|
|
@onready var pixel_count_compute_shader: PixelCountComputeShader = $PixelCountComputeShader
|
2024-01-26 21:07:19 +01:00
|
|
|
|
|
|
|
@onready var camera: Camera3D = $Camera3D
|
2024-01-27 00:05:04 +01:00
|
|
|
@onready var level: Node3D = $Level
|
|
|
|
@onready var world_coloring_viewport: SubViewport = $WorldColoringViewport
|
2024-01-27 01:41:29 +01:00
|
|
|
|
2024-01-27 15:58:16 +01:00
|
|
|
var camera_position: Vector3;
|
2024-01-27 00:05:04 +01:00
|
|
|
var world_coloring_material: ShaderMaterial
|
2024-01-27 15:58:16 +01:00
|
|
|
var world_plane: Plane = Plane(Vector3.UP, Vector3.ZERO)
|
2024-01-26 21:07:19 +01:00
|
|
|
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
|
|
func _ready():
|
2024-01-27 00:05:04 +01:00
|
|
|
world_coloring_material = load("res://materials/WorldColoringMaterialPass.tres")
|
2024-01-27 01:15:01 +01:00
|
|
|
world_coloring_material.set_shader_parameter("world_color_texture", world_coloring_viewport.get_texture())
|
2024-01-27 00:05:04 +01:00
|
|
|
|
2024-01-27 15:58:16 +01:00
|
|
|
var player1: CharacterBody3D = $Player1
|
|
|
|
var player2: CharacterBody3D = $Player2
|
|
|
|
|
|
|
|
players.append(player1)
|
|
|
|
players.append(player2)
|
|
|
|
|
|
|
|
camera_position = camera.global_position
|
|
|
|
|
|
|
|
score_update_timer.connect("timeout", update_score)
|
|
|
|
|
2024-01-27 00:05:04 +01:00
|
|
|
apply_world_coloring_recursive(level)
|
2024-01-26 21:07:19 +01:00
|
|
|
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
2024-01-27 00:05:04 +01:00
|
|
|
func _process(_delta):
|
2024-01-27 15:58:16 +01:00
|
|
|
var player1 : Player = players[0]
|
|
|
|
var player_center = Vector3.ZERO
|
|
|
|
var player_coords_rect : Rect2 = Rect2(
|
|
|
|
player1.global_position.x,
|
|
|
|
player1.global_position.z,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
)
|
|
|
|
|
|
|
|
for player: CharacterBody3D in players:
|
|
|
|
player_center += player.global_position
|
|
|
|
player_coords_rect = player_coords_rect.expand(Vector2(player.global_position.x, player.global_position.z))
|
|
|
|
|
|
|
|
player_center = player_center / players.size()
|
|
|
|
player_center.y = 0
|
|
|
|
|
|
|
|
var view_width = player_coords_rect.size.x + 10
|
|
|
|
var view_height = player_coords_rect.size.y + 15
|
|
|
|
var camera_dist = max (view_width, view_height) * 0.5 / sin(deg_to_rad(camera.fov) * 0.5)
|
|
|
|
|
|
|
|
var camera_direction : Vector3 = -camera.basis.z
|
|
|
|
camera_position = player_center - camera_direction * camera_dist
|
|
|
|
|
|
|
|
camera.global_position = camera_position
|
2024-01-26 21:07:19 +01:00
|
|
|
pass
|
2024-01-27 00:05:04 +01:00
|
|
|
|
|
|
|
func _physics_process(_delta):
|
2024-01-27 01:15:01 +01:00
|
|
|
world_coloring_material.set_shader_parameter("world_color_texture", world_coloring_viewport.get_texture())
|
2024-01-27 00:05:04 +01:00
|
|
|
|
|
|
|
func apply_world_coloring_recursive (node):
|
|
|
|
for child in node.get_children():
|
|
|
|
var mesh_instance := child as MeshInstance3D
|
|
|
|
if mesh_instance:
|
|
|
|
assign_world_coloring_pass(mesh_instance)
|
|
|
|
|
|
|
|
apply_world_coloring_recursive(child)
|
|
|
|
|
|
|
|
func assign_world_coloring_pass(mesh_instance: MeshInstance3D) -> void:
|
|
|
|
var material = mesh_instance.get_active_material(0)
|
|
|
|
if not material:
|
|
|
|
return
|
2024-01-27 01:15:01 +01:00
|
|
|
|
|
|
|
material.next_pass = world_coloring_material
|
2024-01-27 15:58:16 +01:00
|
|
|
|
|
|
|
func update_score():
|
|
|
|
pixel_count_compute_shader.recalculate_score()
|
|
|
|
|
|
|
|
players[0].score = pixel_count_compute_shader.red_score
|
|
|
|
|
|
|
|
if players.size() >= 1:
|
|
|
|
players[1].score = pixel_count_compute_shader.blue_score
|