Skip to content

Commit

Permalink
Added Jump and Attack controls (#4)
Browse files Browse the repository at this point in the history
* Added Jump and Attack controls

* Fixing magic numbers and removing unused vars

* Added click to fire

* Renamed Objects folder to Weapons

* Removed space in typing

* Adding types to parameters

* Simplified weapon

* Adjusted some static typing
  • Loading branch information
Gravedigger7789 committed Nov 7, 2021
1 parent 2f899df commit e07e9bc
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 3 deletions.
57 changes: 54 additions & 3 deletions main.tscn
Original file line number Diff line number Diff line change
@@ -1,9 +1,60 @@
[gd_scene load_steps=2 format=2]
[gd_scene load_steps=4 format=2]

[ext_resource path="res://icon.png" type="Texture" id=1]
[ext_resource path="res://src/Actors/Player.tscn" type="PackedScene" id=2]

[sub_resource type="RectangleShape2D" id=1]
extents = Vector2( 32, 32 )

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

[node name="icon" type="Sprite" parent="."]
position = Vector2( 500, 300 )
[node name="KinematicBody2D" parent="." instance=ExtResource( 2 )]
position = Vector2( 235.201, 58.2705 )

[node name="Floor" type="StaticBody2D" parent="."]
position = Vector2( 223.547, 412.131 )
collision_layer = 4

[node name="icon" type="Sprite" parent="Floor"]
texture = ExtResource( 1 )
__meta__ = {
"_edit_lock_": true
}

[node name="CollisionShape2D" type="CollisionShape2D" parent="Floor"]
shape = SubResource( 1 )
__meta__ = {
"_edit_lock_": true
}

[node name="Object" type="StaticBody2D" parent="."]
position = Vector2( 580.586, 340.087 )
collision_layer = 4

[node name="icon" type="Sprite" parent="Object"]
texture = ExtResource( 1 )
__meta__ = {
"_edit_lock_": true
}

[node name="CollisionShape2D" type="CollisionShape2D" parent="Object"]
shape = SubResource( 1 )
__meta__ = {
"_edit_lock_": true
}

[node name="Enemy" type="StaticBody2D" parent="."]
position = Vector2( 492.651, 336.909 )
collision_layer = 2

[node name="icon" type="Sprite" parent="Enemy"]
texture = ExtResource( 1 )
__meta__ = {
"_edit_lock_": true
}

[node name="CollisionShape2D" type="CollisionShape2D" parent="Enemy"]
shape = SubResource( 1 )
__meta__ = {
"_edit_lock_": true
}
49 changes: 49 additions & 0 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,28 @@

config_version=4

_global_script_classes=[ {
"base": "KinematicBody2D",
"class": "Player",
"language": "GDScript",
"path": "res://src/Actors/Player.gd"
}, {
"base": "Area2D",
"class": "Projectile",
"language": "GDScript",
"path": "res://src/Weapons/Projectile.gd"
}, {
"base": "Position2D",
"class": "Weapon",
"language": "GDScript",
"path": "res://src/Weapons/Weapon.gd"
} ]
_global_script_class_icons={
"Player": "",
"Projectile": "",
"Weapon": ""
}

[application]

config/name="gh-game-off-bug"
Expand All @@ -20,9 +42,36 @@ window/dpi/allow_hidpi=true
window/stretch/mode="viewport"
window/stretch/aspect="keep"

[input]

jump={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777232,"unicode":0,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":32,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":0,"pressure":0.0,"pressed":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":3,"pressure":0.0,"pressed":false,"script":null)
]
}
attack={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":90,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":2,"pressure":0.0,"pressed":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":1,"pressure":0.0,"pressed":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777238,"unicode":0,"echo":false,"script":null)
, Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"button_mask":0,"position":Vector2( 0, 0 ),"global_position":Vector2( 0, 0 ),"factor":1.0,"button_index":1,"pressed":false,"doubleclick":false,"script":null)
]
}

[layer_names]

2d_physics/layer_1="Player"
2d_physics/layer_2="Enemies"
2d_physics/layer_3="Objects"

[physics]

common/enable_pause_aware_picking=true
2d/default_gravity=980

[rendering]

Expand Down
21 changes: 21 additions & 0 deletions src/Actors/Player.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
extends KinematicBody2D
class_name Player

const JUMP_SPEED := 380
const TERMINAL_VELOCITY := 500

var velocity := Vector2()

onready var gravity: int = ProjectSettings.get_setting("physics/2d/default_gravity")
onready var weapon: Weapon = $Weapon

func _physics_process(delta: float) -> void:
velocity.y += gravity * delta
velocity.y = min(TERMINAL_VELOCITY, velocity.y)
velocity = move_and_slide(velocity, Vector2.UP)

if is_on_floor() and Input.is_action_just_pressed("jump"):
velocity.y = -JUMP_SPEED

if Input.is_action_just_pressed("attack"):
weapon.attack()
21 changes: 21 additions & 0 deletions src/Actors/Player.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[gd_scene load_steps=5 format=2]

[ext_resource path="res://icon.png" type="Texture" id=1]
[ext_resource path="res://src/Actors/Player.gd" type="Script" id=2]
[ext_resource path="res://src/Weapons/Weapon.tscn" type="PackedScene" id=3]

[sub_resource type="CapsuleShape2D" id=1]
radius = 32.0
height = 8.0

[node name="KinematicBody2D" type="KinematicBody2D"]
script = ExtResource( 2 )

[node name="Sprite" type="Sprite" parent="."]
texture = ExtResource( 1 )

[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource( 1 )

[node name="Weapon" parent="." instance=ExtResource( 3 )]
position = Vector2( 34, 0 )
16 changes: 16 additions & 0 deletions src/Weapons/Projectile.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
extends Area2D
class_name Projectile

const SPEED := 500.0
const ENEMY_LAYER := 2

func _physics_process(delta: float) -> void:
position.x += SPEED * delta

func _on_Projectile_body_entered(body: Node) -> void:
if body.collision_layer == ENEMY_LAYER:
body.queue_free()
queue_free()

func _on_Expiration_timeout():
queue_free()
26 changes: 26 additions & 0 deletions src/Weapons/Projectile.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[gd_scene load_steps=4 format=2]

[ext_resource path="res://icon.png" type="Texture" id=1]
[ext_resource path="res://src/Weapons/Projectile.gd" type="Script" id=2]

[sub_resource type="CircleShape2D" id=1]
radius = 32.0

[node name="Projectile" type="Area2D"]
collision_layer = 4
collision_mask = 6
script = ExtResource( 2 )

[node name="Sprite" type="Sprite" parent="."]
texture = ExtResource( 1 )

[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource( 1 )

[node name="Expiration" type="Timer" parent="."]
wait_time = 1.5
one_shot = true
autostart = true

[connection signal="body_entered" from="." to="." method="_on_Projectile_body_entered"]
[connection signal="timeout" from="Expiration" to="." method="_on_Expiration_timeout"]
14 changes: 14 additions & 0 deletions src/Weapons/Weapon.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
extends Position2D
class_name Weapon

const Projectile := preload("res://src/Weapons/Projectile.tscn")

onready var cooldown_timer: Timer = $Cooldown

func attack() -> void:
if cooldown_timer.is_stopped():
cooldown_timer.start()
var projectile = Projectile.instance()
projectile.global_position = global_position
projectile.set_as_toplevel(true)
add_child(projectile)
14 changes: 14 additions & 0 deletions src/Weapons/Weapon.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[gd_scene load_steps=2 format=2]

[ext_resource path="res://src/Weapons/Weapon.gd" type="Script" id=1]

[node name="Weapon" type="Position2D"]
script = ExtResource( 1 )
__meta__ = {
"_gizmo_extents_": 16.0
}

[node name="Cooldown" type="Timer" parent="."]
process_mode = 0
wait_time = 0.3
one_shot = true

0 comments on commit e07e9bc

Please sign in to comment.