DashBoomColorClash/scenes/World.gd

59 lines
2.3 KiB
GDScript
Raw Normal View History

2024-01-26 21:07:19 +01:00
extends Node3D
@onready var player1: CharacterBody3D = $Player1
@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
@onready var world_coloring_player1_texture_rect: TextureRect = $WorldColoringViewport/Player1TextureRect
@onready var world_coloring_player1_sprite: Sprite2D = $WorldColoringViewport/Player1Sprite
@onready var world_coloring_player1_bomb_sprite: Sprite2D = $WorldColoringViewport/Player1BombSprite
2024-01-26 21:07:19 +01:00
var camera_offset: Vector3;
2024-01-27 00:05:04 +01:00
var world_coloring_material: ShaderMaterial
2024-01-26 21:07:19 +01:00
# Called when the node enters the scene tree for the first time.
func _ready():
camera_offset = camera.global_position - player1.global_position
2024-01-27 00:05:04 +01:00
world_coloring_material = load("res://materials/WorldColoringMaterialPass.tres")
world_coloring_material.set_shader_parameter("world_color_texture", world_coloring_viewport.get_texture())
2024-01-27 00:05:04 +01:00
world_coloring_player1_sprite.modulate = player1.color
world_coloring_player1_bomb_sprite.modulate = player1.color
player1.connect("bomb_triggered", player_bomb_triggered)
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-26 21:07:19 +01:00
camera.global_position = player1.global_position + camera_offset
pass
2024-01-27 00:05:04 +01:00
func _physics_process(_delta):
world_coloring_player1_sprite.global_position = player_position_to_world_coloring_position(player1.global_position)
world_coloring_material.set_shader_parameter("world_color_texture", world_coloring_viewport.get_texture())
2024-01-27 00:05:04 +01:00
func player_position_to_world_coloring_position(player_position: Vector3):
var result = Vector2 (player_position.x * 10 + 500, player_position.z * 10 + 500)
2024-01-27 00:05:04 +01:00
return result;
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
material.next_pass = world_coloring_material
2024-01-27 00:05:04 +01:00
func player_bomb_triggered(player):
world_coloring_player1_bomb_sprite.global_position = player_position_to_world_coloring_position(player.global_position)
world_coloring_player1_bomb_sprite.visible = true