Skip to content

Commit

Permalink
Merge pull request #8 from KartikWatts/2d-survivors-course
Browse files Browse the repository at this point in the history
improving game features
  • Loading branch information
KartikWatts authored Dec 17, 2023
2 parents 3dfc878 + ac7670f commit 5162767
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 48 deletions.
8 changes: 8 additions & 0 deletions 2d-survivors-course/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ move_down={
]
}

[layer_names]

2d_physics/layer_1="Terrain"
2d_physics/layer_2="Player"
2d_physics/layer_3="Enemy"
2d_physics/layer_4="EnemyCollision"

[rendering]

textures/canvas_textures/default_texture_filter=0
2d/snap/snap_2d_transforms_to_pixel=true
90 changes: 63 additions & 27 deletions 2d-survivors-course/scenes/ability/sword_ability/sword_ability.tscn
Original file line number Diff line number Diff line change
@@ -1,7 +1,46 @@
[gd_scene load_steps=5 format=3 uid="uid://b0i6v3ywsxu8u"]
[gd_scene load_steps=6 format=3 uid="uid://b0i6v3ywsxu8u"]

[ext_resource type="Texture2D" uid="uid://cntpppigmvsmw" path="res://scenes/ability/sword_ability/sword.png" id="1_mlw7u"]

[sub_resource type="Animation" id="Animation_bt863"]
length = 0.001
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("Sprite2D:rotation")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 0,
"values": [0.0]
}
tracks/1/type = "value"
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/path = NodePath("Sprite2D:scale")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 0,
"values": [Vector2(1, 1)]
}
tracks/2/type = "value"
tracks/2/imported = false
tracks/2/enabled = true
tracks/2/path = NodePath("Area2D/CollisionShape2D:disabled")
tracks/2/interp = 1
tracks/2/loop_wrap = true
tracks/2/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [true]
}

[sub_resource type="Animation" id="Animation_noyxp"]
resource_name = "swing"
length = 0.75
Expand Down Expand Up @@ -44,32 +83,17 @@ tracks/2/keys = {
"method": &"queue_free"
}]
}

[sub_resource type="Animation" id="Animation_bt863"]
length = 0.001
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("Sprite2D:rotation")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 0,
"values": [0.0]
}
tracks/1/type = "value"
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/path = NodePath("Sprite2D:scale")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 0,
"values": [Vector2(1, 1)]
tracks/3/type = "value"
tracks/3/imported = false
tracks/3/enabled = true
tracks/3/path = NodePath("Area2D/CollisionShape2D:disabled")
tracks/3/interp = 1
tracks/3/loop_wrap = true
tracks/3/keys = {
"times": PackedFloat32Array(0, 0.2, 0.4),
"transitions": PackedFloat32Array(1, 1, 1),
"update": 1,
"values": [true, false, true]
}

[sub_resource type="AnimationLibrary" id="AnimationLibrary_qricj"]
Expand All @@ -78,6 +102,9 @@ _data = {
"swing": SubResource("Animation_noyxp")
}

[sub_resource type="RectangleShape2D" id="RectangleShape2D_hbrnx"]
size = Vector2(30, 30)

[node name="SwordAbility" type="Node2D"]

[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
Expand All @@ -89,3 +116,12 @@ autoplay = "swing"
[node name="Sprite2D" type="Sprite2D" parent="."]
texture = ExtResource("1_mlw7u")
offset = Vector2(0, -4)

[node name="Area2D" type="Area2D" parent="."]
collision_layer = 4
collision_mask = 0

[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"]
position = Vector2(15, 0)
shape = SubResource("RectangleShape2D_hbrnx")
disabled = true
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ func on_timer_timeout():

var sword_instance = sword_ability.instantiate() as Node2D
player.get_parent().add_child(sword_instance)
sword_instance.global_position = enemies[0].global_position
sword_instance.global_position = enemies[0].global_position
sword_instance.global_position += Vector2.RIGHT.rotated(randf_range(0, TAU)) * 4

var enemy_direction = enemies[0].global_position - sword_instance.global_position
sword_instance.rotation = enemy_direction.angle()
10 changes: 7 additions & 3 deletions 2d-survivors-course/scenes/basic_enemy/basic_enemy.gd
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
extends CharacterBody2D

const MAX_SPEED = 75
const MAX_SPEED = 40

# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
$Area2D.area_entered.connect(on_area_entered)


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
func _process(_delta):
var direction = get_direction_to_player()
velocity = direction * MAX_SPEED
move_and_slide()
Expand All @@ -19,3 +19,7 @@ func get_direction_to_player():
if player_node != null:
return (player_node.global_position - global_position).normalized()
return Vector2.ZERO


func on_area_entered(_other_area: Area2D):
queue_free()
17 changes: 15 additions & 2 deletions 2d-survivors-course/scenes/basic_enemy/basic_enemy.tscn
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
[gd_scene load_steps=4 format=3 uid="uid://dlsoablcbbnbn"]
[gd_scene load_steps=5 format=3 uid="uid://dlsoablcbbnbn"]

[ext_resource type="Script" path="res://scenes/basic_enemy/basic_enemy.gd" id="1_btoq0"]
[ext_resource type="Texture2D" uid="uid://c4wr81m8ifp8g" path="res://scenes/basic_enemy/basic_enemy.png" id="1_tp3eb"]

[sub_resource type="CircleShape2D" id="CircleShape2D_3fx0j"]
radius = 12.0

[sub_resource type="CircleShape2D" id="CircleShape2D_dkycg"]
radius = 5.0

[node name="BasicEnemy" type="CharacterBody2D" groups=["enemy"]]
collision_layer = 0
collision_layer = 8
collision_mask = 9
motion_mode = 1
script = ExtResource("1_btoq0")

[node name="Area2D" type="Area2D" parent="."]
collision_layer = 0
collision_mask = 4

[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"]
position = Vector2(0, -5)
shape = SubResource("CircleShape2D_3fx0j")

[node name="Sprite2D" type="Sprite2D" parent="."]
texture = ExtResource("1_tp3eb")
offset = Vector2(0, -4)
Expand Down
2 changes: 1 addition & 1 deletion 2d-survivors-course/scenes/game_camera/game_camera.gd
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func _ready():
func _process(delta):
acquire_target()
# lerp will implement Linear Interpolate for smoothness
global_position = global_position.lerp(target_position, 1.0 - exp(-delta * 10))
global_position = global_position.lerp(target_position, 1.0 - exp(-delta * 20))


func acquire_target():
Expand Down
15 changes: 4 additions & 11 deletions 2d-survivors-course/scenes/main/main.tscn

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions 2d-survivors-course/scenes/manager/enemy_manager.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
extends Node

const SPAWN_RADIUS = 350

@export var basic_enemy_scene: PackedScene

# Called when the node enters the scene tree for the first time.
func _ready():
$Timer.timeout.connect(on_timer_timeout)

func on_timer_timeout():
var player = get_tree().get_first_node_in_group("player") as Node2D
if player == null:
return

var random_direction = Vector2.RIGHT.rotated(randf_range(0, TAU))
var spawn_position = player.global_position + (random_direction * SPAWN_RADIUS)

var enemy = basic_enemy_scene.instantiate() as Node2D
get_parent().add_child(enemy)
enemy.global_position = spawn_position
11 changes: 11 additions & 0 deletions 2d-survivors-course/scenes/manager/enemy_manager.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[gd_scene load_steps=3 format=3 uid="uid://dyy6q3goih542"]

[ext_resource type="Script" path="res://scenes/manager/enemy_manager.gd" id="1_1cnff"]
[ext_resource type="PackedScene" uid="uid://dlsoablcbbnbn" path="res://scenes/basic_enemy/basic_enemy.tscn" id="2_rsw1k"]

[node name="EnemyManager" type="Node"]
script = ExtResource("1_1cnff")
basic_enemy_scene = ExtResource("2_rsw1k")

[node name="Timer" type="Timer" parent="."]
autostart = true
9 changes: 6 additions & 3 deletions 2d-survivors-course/scenes/player/player.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
extends CharacterBody2D

const MAX_SPEED = 200
const MAX_SPEED = 125
const ACCELERATION_SMOOTHING = 25

# Called when the node enters the scene tree for the first time.
func _ready():
Expand All @@ -11,11 +12,13 @@ func _ready():
func _process(delta):
var movement_vector = get_movement_vector()
var direction = movement_vector.normalized()
velocity = direction * MAX_SPEED
var target_velocity = direction * MAX_SPEED

velocity = velocity.lerp(target_velocity, 1 - exp(-delta * ACCELERATION_SMOOTHING))
move_and_slide()

func get_movement_vector():
var movement_vector = Vector2.ZERO
#var movement_vector = Vector2.ZERO

var x_movement = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
var y_movement = Input.get_action_strength("move_down") - Input.get_action_strength("move_up")
Expand Down
1 change: 1 addition & 0 deletions 2d-survivors-course/scenes/player/player.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ radius = 5.0

[node name="Player" type="CharacterBody2D" groups=["player"]]
collision_layer = 0
motion_mode = 1
script = ExtResource("1_6ggdr")

[node name="AbilityManager" type="Node" parent="."]
Expand Down

0 comments on commit 5162767

Please sign in to comment.