Skip to content

Commit

Permalink
player speed upgrade and vignette effect
Browse files Browse the repository at this point in the history
`VelocityComponent` is used in Player scene for speed acceleration and upgrade updates
Vignette is implemented with Shader from: `https://godotshaders.com/shader/color-vignetting`
  • Loading branch information
KartikWatts committed Jan 27, 2024
1 parent d61d918 commit a4416f4
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 13 deletions.
10 changes: 10 additions & 0 deletions 2d-survivors-course/resources/upgrades/player_speed.tres
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[gd_resource type="Resource" script_class="AbilityUpgrade" load_steps=2 format=3 uid="uid://1yg3d35wh0md"]

[ext_resource type="Script" path="res://resources/upgrades/ability_upgrade.gd" id="1_qlos1"]

[resource]
script = ExtResource("1_qlos1")
id = "player_speed"
max_quantity = 2
name = "Move Speed"
description = "Increases player movement speed by 10%."
6 changes: 5 additions & 1 deletion 2d-survivors-course/scenes/autoload/game_events.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ extends Node

signal experience_vial_collected(number: float)
signal ability_upgrade_added(upgrade: AbilityUpgrade, current_upgrades: Dictionary)

signal player_damaged()

func emit_experience_vial_collected(number: float):
experience_vial_collected.emit(number)


func emit_ability_upgrade_added(upgrade: AbilityUpgrade, current_upgrades: Dictionary):
ability_upgrade_added.emit(upgrade, current_upgrades)


func emit_player_damaged():
player_damaged.emit()
23 changes: 13 additions & 10 deletions 2d-survivors-course/scenes/game_object/player/player.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ extends CharacterBody2D
@onready var _abilities = $Abilities
@onready var _animation_player = $AnimationPlayer
@onready var _visuals = $Visuals
@onready var velocity_component = $VelocityComponent

const MAX_SPEED = 125
const ACCELERATION_SMOOTHING = 25

var number_colliding_bodies = 0
var base_speed = 0


func _ready():
base_speed = velocity_component.max_speed

_collision_area.body_entered.connect(on_body_entered)
_collision_area.body_exited.connect(on_body_exited)
_damage_interval_timer.timeout.connect(on_damage_interval_timer_timeout)
Expand All @@ -25,10 +28,8 @@ func _ready():
func _process(delta):
var movement_vector = get_movement_vector()
var direction = movement_vector.normalized()
var target_velocity = direction * MAX_SPEED

velocity = velocity.lerp(target_velocity, 1 - exp(-delta * ACCELERATION_SMOOTHING))
move_and_slide()
velocity_component.accelerate_in_direction(direction)
velocity_component.move(self)

if movement_vector.x !=0 or movement_vector.y!=0:
_animation_player.play("walk")
Expand Down Expand Up @@ -72,11 +73,13 @@ func on_damage_interval_timer_timeout():


func on_health_changed():
GameEvents.emit_player_damaged()
update_health_display()


func on_ability_upgrade_added(ability_upgrade: AbilityUpgrade, current_upgrades: Dictionary):
if not ability_upgrade is Ability:
return
var ability = ability_upgrade as Ability
_abilities.add_child(ability.ability_controller_scene.instantiate())
if ability_upgrade is Ability:
var ability = ability_upgrade as Ability
_abilities.add_child(ability.ability_controller_scene.instantiate())
elif ability_upgrade.id == "player_speed":
velocity_component.max_speed = base_speed + (base_speed * (current_upgrades["player_speed"]["quantity"] * 0.1))
7 changes: 6 additions & 1 deletion 2d-survivors-course/scenes/game_object/player/player.tscn
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
[gd_scene load_steps=12 format=3 uid="uid://c2o7ggmbuyq3b"]
[gd_scene load_steps=13 format=3 uid="uid://c2o7ggmbuyq3b"]

[ext_resource type="Script" path="res://scenes/game_object/player/player.gd" id="1_6ggdr"]
[ext_resource type="Texture2D" uid="uid://c3oci8ridauxg" path="res://scenes/game_object/player/player.png" id="2_1v84t"]
[ext_resource type="Script" path="res://scenes/components/health_component.gd" id="2_lfgfj"]
[ext_resource type="PackedScene" uid="uid://crwtyhsq062wk" path="res://scenes/ability/sword_ability_controller/sword_ability_controller.tscn" id="2_moccb"]
[ext_resource type="PackedScene" uid="uid://bq1vvw48qq8jf" path="res://scenes/components/velocity_component.tscn" id="2_x0b0i"]

[sub_resource type="Animation" id="Animation_q62fd"]
length = 0.001
Expand Down Expand Up @@ -118,6 +119,10 @@ libraries = {
"": SubResource("AnimationLibrary_u1xbg")
}

[node name="VelocityComponent" parent="." instance=ExtResource("2_x0b0i")]
max_speed = 90
acceleration = 25.0

[node name="HealthComponent" type="Node" parent="."]
script = ExtResource("2_lfgfj")

Expand Down
5 changes: 4 additions & 1 deletion 2d-survivors-course/scenes/main/main.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=12 format=3 uid="uid://cnfotfwbqn61p"]
[gd_scene load_steps=13 format=3 uid="uid://cnfotfwbqn61p"]

[ext_resource type="TileSet" uid="uid://wiluhbm2cyyi" path="res://resources/tileset.tres" id="1_3sopq"]
[ext_resource type="PackedScene" uid="uid://c2o7ggmbuyq3b" path="res://scenes/game_object/player/player.tscn" id="1_6q4rs"]
Expand All @@ -9,13 +9,16 @@
[ext_resource type="PackedScene" uid="uid://d0xuecycxu6ge" path="res://scenes/ui/end_screen.tscn" id="2_4kbya"]
[ext_resource type="PackedScene" uid="uid://bygs1oxednwvd" path="res://scenes/ui/experience_bar.tscn" id="2_l4is7"]
[ext_resource type="PackedScene" uid="uid://c4jyynk4gcgoj" path="res://scenes/manager/arena_time_manager.tscn" id="2_owxx1"]
[ext_resource type="PackedScene" uid="uid://dtryyvn2r6lfw" path="res://scenes/ui/vignette.tscn" id="3_ssem8"]
[ext_resource type="PackedScene" uid="uid://b0l3ryye8rljn" path="res://scenes/manager/experience_manager.tscn" id="4_b7oit"]
[ext_resource type="PackedScene" uid="uid://be7o1mfpaqldm" path="res://scenes/manager/upgrade_manager.tscn" id="6_88lkj"]

[node name="Main" type="Node"]
script = ExtResource("1_oij7i")
end_screen = ExtResource("2_4kbya")

[node name="Vignette" parent="." instance=ExtResource("3_ssem8")]

[node name="ArenaTimeUI" parent="." node_paths=PackedStringArray("arena_time_manager") instance=ExtResource("1_ni4o4")]
arena_time_manager = NodePath("../ArenaTimeManager")

Expand Down
2 changes: 2 additions & 0 deletions 2d-survivors-course/scenes/manager/upgrade_manager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ var upgrade_axe = preload("res://resources/upgrades/axe.tres")
var upgrade_axe_damage = preload("res://resources/upgrades/axe_damage.tres")
var upgrade_sword_rate = preload("res://resources/upgrades/sword_rate.tres")
var upgrade_sword_damage = preload("res://resources/upgrades/sword_damage.tres")
var upgrade_player_speed = preload("res://resources/upgrades/player_speed.tres")


func _ready():
upgrade_pool.add_item(upgrade_axe, 10)
upgrade_pool.add_item(upgrade_sword_rate, 10)
upgrade_pool.add_item(upgrade_sword_damage, 10)
upgrade_pool.add_item(upgrade_player_speed, 5)

experience_manager.level_up.connect(on_level_up)

Expand Down
9 changes: 9 additions & 0 deletions 2d-survivors-course/scenes/ui/vignette.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
extends CanvasLayer


func _ready():
GameEvents.player_damaged.connect(on_player_damaged)


func on_player_damaged():
$AnimationPlayer.play("hit")
20 changes: 20 additions & 0 deletions 2d-survivors-course/scenes/ui/vignette.gdshader
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
shader_type canvas_item;

uniform float vignette_intensity = 0.4;
uniform float vignette_opacity : hint_range(0.0, 1.0) = 0.5;
uniform vec4 vignette_rgb : source_color = vec4(0.0, 0.0, 0.0, 1.0);

float vignette(vec2 uv){
uv *= 1.0 - uv.xy;
float vignette = uv.x * uv.y * 15.0;
return pow(vignette, vignette_intensity * vignette_opacity);
}

void fragment(){
vec4 text = texture(TEXTURE, UV);

text.rgba *= (vignette_rgb.rgba);
text.rgba *= (1.0 - vignette(UV));

COLOR = vec4(text.rgb,text.a);
}
113 changes: 113 additions & 0 deletions 2d-survivors-course/scenes/ui/vignette.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
[gd_scene load_steps=7 format=3 uid="uid://dtryyvn2r6lfw"]

[ext_resource type="Script" path="res://scenes/ui/vignette.gd" id="1_18okb"]
[ext_resource type="Shader" path="res://scenes/ui/vignette.gdshader" id="1_hhs4t"]

[sub_resource type="Animation" id="Animation_ahjpg"]
resource_name = "hit"
length = 0.4
step = 0.05
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("ColorRect:material:shader_parameter/vignette_intensity")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0, 0.05, 0.4),
"transitions": PackedFloat32Array(1, 2.73208, 1),
"update": 0,
"values": [0.7, 3.0, 0.7]
}
tracks/1/type = "value"
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/path = NodePath("ColorRect:material:shader_parameter/vignette_rgb")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/keys = {
"times": PackedFloat32Array(0.05, 0.4),
"transitions": PackedFloat32Array(2.63902, 1),
"update": 0,
"values": [Color(0.909804, 0.270588, 0.215686, 1), Color(0.247059, 0.14902, 0.192157, 1)]
}
tracks/2/type = "value"
tracks/2/imported = false
tracks/2/enabled = true
tracks/2/path = NodePath("ColorRect:material:shader_parameter/vignette_opacity")
tracks/2/interp = 1
tracks/2/loop_wrap = true
tracks/2/keys = {
"times": PackedFloat32Array(0, 0.05, 0.4),
"transitions": PackedFloat32Array(1, 1, 1),
"update": 0,
"values": [0.125, 0.325, 0.125]
}

[sub_resource type="Animation" id="Animation_7hlu1"]
length = 0.001
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("ColorRect:material:shader_parameter/vignette_rgb")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 0,
"values": [Color(0.247059, 0.14902, 0.192157, 1)]
}
tracks/1/type = "value"
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/path = NodePath("ColorRect:material:shader_parameter/vignette_intensity")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 0,
"values": [0.7]
}
tracks/2/type = "value"
tracks/2/imported = false
tracks/2/enabled = true
tracks/2/path = NodePath("ColorRect:material:shader_parameter/vignette_opacity")
tracks/2/interp = 1
tracks/2/loop_wrap = true
tracks/2/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 0,
"values": [0.125]
}

[sub_resource type="AnimationLibrary" id="AnimationLibrary_o6tj5"]
_data = {
"RESET": SubResource("Animation_7hlu1"),
"hit": SubResource("Animation_ahjpg")
}

[sub_resource type="ShaderMaterial" id="ShaderMaterial_gihtf"]
shader = ExtResource("1_hhs4t")
shader_parameter/vignette_intensity = 0.7
shader_parameter/vignette_opacity = 0.125
shader_parameter/vignette_rgb = Color(0.247059, 0.14902, 0.192157, 1)

[node name="Vignette" type="CanvasLayer"]
script = ExtResource("1_18okb")

[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
libraries = {
"": SubResource("AnimationLibrary_o6tj5")
}

[node name="ColorRect" type="ColorRect" parent="."]
material = SubResource("ShaderMaterial_gihtf")
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
mouse_filter = 2

0 comments on commit a4416f4

Please sign in to comment.