Skip to content

Commit ac20e9a

Browse files
committed
Apply a shader when player selects a country.
1 parent 395d1fe commit ac20e9a

File tree

5 files changed

+45
-1
lines changed

5 files changed

+45
-1
lines changed

entities/countries/country.gd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ class_name Country
44
enum Keys { USA }
55

66
@export var country_key: Keys
7+
@export var sprite: Sprite2D
8+
var selected_material: Material = preload("res://entities/countries/selected_country_material.tres")
79
var country_name: String
810

911
func _ready() -> void:
@@ -15,5 +17,9 @@ func _ready() -> void:
1517

1618
func select() -> void:
1719
var countries_vm = ViewModelRegistry.retrieve(ViewModelRegistry.Keys.COUNTRIES) as CountriesViewModel
20+
sprite.material = selected_material
1821
if countries_vm:
1922
countries_vm.select_country(country_key)
23+
24+
func unselect() -> void:
25+
sprite.material = null
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
shader_type canvas_item;
2+
3+
//void vertex() {
4+
//// Called for every vertex the material is visible on.
5+
//}
6+
7+
const float frequency = 750.00;
8+
9+
void fragment() {
10+
vec4 tex_color = texture(TEXTURE, UV);
11+
vec4 overlay_color = vec4(.3, .3, .9, .8);
12+
13+
// Create a diagonal striped pattern
14+
float stripe = mod((UV.x + UV.y) * frequency, 2) > 1.00 ? 1.0 : 0.0;
15+
16+
// Apply overlay only to non-transparent areas and within stripe pattern
17+
if (tex_color.a > 0.0 && stripe == 1.0) {
18+
COLOR = mix(tex_color, overlay_color, overlay_color.a);
19+
} else {
20+
COLOR = tex_color;
21+
}
22+
}
23+
24+
//void light() {
25+
// Called for every pixel for every light affecting the CanvasItem.
26+
// Uncomment to replace the default light processing function with this one.
27+
//}

entities/countries/country.tscn

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
[ext_resource type="Script" path="res://entities/countries/country.gd" id="1_j7m2o"]
44
[ext_resource type="Texture2D" uid="uid://5tcwuxorjhmw" path="res://art/usa.png" id="2_1k81q"]
55

6-
[node name="Country" type="Area2D"]
6+
[node name="Country" type="Area2D" node_paths=PackedStringArray("sprite")]
77
script = ExtResource("1_j7m2o")
8+
sprite = NodePath("Sprite2D")
89

910
[node name="Sprite2D" type="Sprite2D" parent="."]
1011
texture = ExtResource("2_1k81q")
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[gd_resource type="ShaderMaterial" load_steps=2 format=3 uid="uid://du3ukvaptkks8"]
2+
3+
[ext_resource type="Shader" path="res://entities/countries/country.gdshader" id="1_bbqy0"]
4+
5+
[resource]
6+
shader = ExtResource("1_bbqy0")

scenes/common/player_controller.gd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ extends Node2D
1010
@export var menu_scene: PackedScene
1111
var is_dragging: bool = false
1212
var menu_instance: Control
13+
var selected_country: Country
1314

1415
func _ready():
1516
Input.mouse_mode = Input.MOUSE_MODE_CONFINED
@@ -26,6 +27,9 @@ func _unhandled_input(event: InputEvent) -> void:
2627
for collision in collisions:
2728
var country := collision.collider as Country
2829
if country:
30+
if selected_country:
31+
selected_country.unselect()
32+
selected_country = country
2933
country.select()
3034
return
3135

0 commit comments

Comments
 (0)