Skip to content

Commit

Permalink
added knife, solved bow bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ProfAndreaPollini committed Jul 26, 2022
1 parent 016a6ad commit 91cd532
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 38 deletions.
5 changes: 1 addition & 4 deletions GameScreen.tscn
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
[gd_scene load_steps=16 format=2]
[gd_scene load_steps=15 format=2]

[ext_resource path="res://GameScreen.gd" type="Script" id=1]
[ext_resource path="res://assets/0x72_16x16DungeonTileset.v4.png" type="Texture" id=2]
[ext_resource path="res://entites/King.tscn" type="PackedScene" id=3]
[ext_resource path="res://lib/player/Player.tscn" type="PackedScene" id=4]
[ext_resource path="res://lib/bow/Bow.tscn" type="PackedScene" id=6]

[sub_resource type="ConvexPolygonShape2D" id=11]
points = PoolVector2Array( 16, 16, 0, 16, 0, 16, 16, 16 )
Expand Down Expand Up @@ -181,6 +180,4 @@ position = Vector2( 63, 7 )
current = true
zoom = Vector2( 0.75, 0.75 )

[node name="Bow" parent="Level/Level0" instance=ExtResource( 6 )]

[node name="Overlays" type="Node" parent="."]
4 changes: 2 additions & 2 deletions TestTerrain.tscn

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ compress/hdr_mode=0
compress/bptc_ldr=0
compress/normal_map=0
flags/repeat=0
flags/filter=true
flags/filter=false
flags/mipmaps=false
flags/anisotropic=false
flags/srgb=2
Expand Down
2 changes: 1 addition & 1 deletion lib/KinematicMovementComponent.gd
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func get_is_moving():
return is_moving


func update(delta):
func update(_delta):
var dx = int(Input.is_action_pressed("ui_right"))-int(Input.is_action_pressed("ui_left"))
var dy = int(Input.is_action_pressed("ui_down"))-int(Input.is_action_pressed("ui_up"))
var ds = Vector2(dx,dy).normalized()
Expand Down
15 changes: 14 additions & 1 deletion lib/bow/Bow.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ extends Node2D
export(Texture) var icon_texture

var direction = Vector2.ZERO
var entity
export(NodePath)var entity

var Arrow = preload("res://entites/weapons/Arrow.tscn")

# Called when the node enters the scene tree for the first time.
func _ready():
Expand All @@ -21,3 +23,14 @@ func on_mouse_moved(pos: Vector2,dir: Vector2):
global_position.y = clamp(global_position.y,pos.y-15,pos.y+15)

look_at(global_position + 200*direction)


func shoot():
#if not cool_down.can_fire(): return

#cool_down.start()
var arrow = Arrow.instance()
arrow.global_position = self.global_position + 1*direction
arrow.look_at(global_position + 200*direction)
arrow.direction = direction
get_node(entity).get_parent().add_child(arrow)
4 changes: 2 additions & 2 deletions lib/bow/Bow.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ script = ExtResource( 1 )
icon_texture = ExtResource( 2 )

[node name="Sprite" type="Sprite" parent="."]
position = Vector2( 4, 0 )
texture = ExtResource( 2 )
offset = Vector2( 10, 0 )

[node name="BodyArea" type="Area2D" parent="." groups=["weapon"]]
collision_layer = 4

[node name="CollisionShape2D" type="CollisionShape2D" parent="BodyArea"]
position = Vector2( 10, 0 )
position = Vector2( 6, 0 )
shape = SubResource( 1 )
47 changes: 47 additions & 0 deletions lib/knife/Knife.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
extends Node2D

export(Texture) var icon_texture
export(NodePath)var entity

var direction = Vector2.ZERO
var is_firing :=false
var old_position := Vector2.ZERO

onready var fire_timer = $FireTimer
onready var tween = $Tween

func _ready():
add_to_group("weapon")


func on_mouse_moved(pos: Vector2,dir: Vector2):
#print("On mouse moved pos = ", pos, " dir = ", dir)
direction = dir

global_position = pos + 10*dir
#print("pos(pre) = ",global_position)
global_position.x = clamp(global_position.x,pos.x-5,pos.x+5)
global_position.y = clamp(global_position.y,pos.y-15,pos.y+15)

look_at(global_position + 200*direction)

func shoot():
if not is_firing:
is_firing = true
fire_timer.start()
old_position = global_position
#global_position = global_position + 10*direction
tween.interpolate_property(self,
"global_position",
global_position,
global_position + 10*direction,0.1,Tween.TRANS_QUAD
)
tween.start()





func _on_FireTimer_timeout():
global_position = old_position
is_firing = false
23 changes: 23 additions & 0 deletions lib/knife/Knife.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[gd_scene load_steps=4 format=2]

[ext_resource path="res://lib/knife/Knife.gd" type="Script" id=1]
[ext_resource path="res://assets/0x72_DungeonTilesetII_v1.4/0x72_DungeonTilesetII_v1.4/frames/weapon_knife.png" type="Texture" id=2]
[ext_resource path="res://assets/Minifantasy_Dungeon_SFX/27_sword_miss_1.wav" type="AudioStream" id=3]

[node name="Knife" type="Node2D"]
script = ExtResource( 1 )

[node name="Sprite" type="Sprite" parent="."]
position = Vector2( 1, 0 )
rotation = 1.5708
texture = ExtResource( 2 )
offset = Vector2( 0, 0.034 )

[node name="FireTimer" type="Timer" parent="."]

[node name="Tween" type="Tween" parent="."]

[node name="AudioStreamPlayer" type="AudioStreamPlayer" parent="."]
stream = ExtResource( 3 )

[connection signal="timeout" from="FireTimer" to="." method="_on_FireTimer_timeout"]
43 changes: 36 additions & 7 deletions lib/player/Player.gd
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,65 @@ onready var weapon_position = $WeaponPosition
onready var animation_tree = $AnimationTree
onready var animation_state = animation_tree.get("parameters/playback")

onready var weapon = $Bow
onready var weapon = $WeaponPosition/Knife

var is_moving := false
var weapon_position_distance :=0.0

# Called when the node enters the scene tree for the first time.
func _ready():
movement.connect("moved",self,"on_moved")
movement.connect("stopped",self,"on_stopped")
animation_state.travel("idle")
weapon_position_distance = global_position.distance_to(weapon_position.position)


func _physics_process(delta):
movement.update(delta)
#if is_moving:
var mouse_position = get_global_mouse_position()
movement.direction = (mouse_position - get_weapon_position()).normalized()
movement.direction = lerp(movement.direction,(mouse_position - global_position).normalized(),0.9)
weapon_position.look_at(mouse_position)
weapon.on_mouse_moved(get_weapon_position(),movement.direction)
# var mouse_position = get_global_mouse_position()

# movement.direction = (mouse_position - get_weapon_position()).normalized()

var animation_param = movement.direction

# weapon_position.look_at(mouse_position)
var animation_param = movement.direction #Vector2(stepify(movement.direction.x,0.1),stepify(movement.direction.y,0.1))
#print("weapon = ",weapon_position.position, get_weapon_position(),weapon_position.global_position)


animation_tree.set("parameters/idle/blend_position", animation_param)
animation_tree.set("parameters/walk/blend_position", animation_param)
if weapon:
weapon.on_mouse_moved(mouse_position,animation_param)

if Input.is_action_just_pressed("fire") and weapon:
weapon.shoot()


update()

func _input(event):
if event is InputEventMouseMotion:
is_moving = true

# if event is InputEventKey:
# if event.scancode == KEY_SPACE:
# weapon.shoot()



func _draw():
draw_circle(to_local(get_weapon_position()),2,Color(1,1,0))
# if is_moving:
# draw_circle(Vector2(10,10),2,Color.green)

func on_stopped():
animation_state.travel("idle")
is_moving = false

func on_moved():
print("moved: ",movement.direction)
#print("moved: ",movement.direction)
animation_state.travel("walk")
is_moving = true

Expand Down
35 changes: 15 additions & 20 deletions lib/player/Player.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[ext_resource path="res://assets/RPGMCharacter_v1.0/_down walk.png" type="Texture" id=8]
[ext_resource path="res://assets/RPGMCharacter_v1.0/_side walk.png" type="Texture" id=9]
[ext_resource path="res://assets/RPGMCharacter_v1.0/_up walk.png" type="Texture" id=10]
[ext_resource path="res://lib/bow/Bow.tscn" type="PackedScene" id=11]
[ext_resource path="res://lib/knife/Knife.tscn" type="PackedScene" id=12]

[sub_resource type="CapsuleShape2D" id=21]
radius = 7.0
Expand Down Expand Up @@ -202,7 +202,6 @@ blend_point_2/node = SubResource( 63 )
blend_point_2/pos = Vector2( -0.8, 0 )
blend_point_3/node = SubResource( 64 )
blend_point_3/pos = Vector2( 0.9, 0 )
blend_mode = 1

[sub_resource type="AnimationNodeAnimation" id=75]
animation = "walk_down"
Expand All @@ -225,7 +224,6 @@ blend_point_2/node = SubResource( 77 )
blend_point_2/pos = Vector2( -0.8, 0 )
blend_point_3/node = SubResource( 78 )
blend_point_3/pos = Vector2( 0, -0.8 )
blend_mode = 1

[sub_resource type="AnimationNodeStateMachineTransition" id=80]

Expand Down Expand Up @@ -261,7 +259,7 @@ tracks/1/path = NodePath("WeaponPosition:position")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/enabled = false
tracks/1/keys = {
"times": PoolRealArray( 0 ),
"transitions": PoolRealArray( 1 ),
Expand Down Expand Up @@ -301,7 +299,7 @@ tracks/2/path = NodePath("WeaponPosition:position")
tracks/2/interp = 1
tracks/2/loop_wrap = true
tracks/2/imported = false
tracks/2/enabled = true
tracks/2/enabled = false
tracks/2/keys = {
"times": PoolRealArray( 0 ),
"transitions": PoolRealArray( 1 ),
Expand Down Expand Up @@ -341,7 +339,7 @@ tracks/2/path = NodePath("WeaponPosition:position")
tracks/2/interp = 1
tracks/2/loop_wrap = true
tracks/2/imported = false
tracks/2/enabled = true
tracks/2/enabled = false
tracks/2/keys = {
"times": PoolRealArray( 0 ),
"transitions": PoolRealArray( 1 ),
Expand Down Expand Up @@ -369,7 +367,7 @@ tracks/1/path = NodePath("WeaponPosition:position")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/enabled = false
tracks/1/keys = {
"times": PoolRealArray( 0 ),
"transitions": PoolRealArray( 1 ),
Expand Down Expand Up @@ -397,7 +395,7 @@ tracks/1/path = NodePath("WeaponPosition:position")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/enabled = false
tracks/1/keys = {
"times": PoolRealArray( 0 ),
"transitions": PoolRealArray( 1 ),
Expand Down Expand Up @@ -437,7 +435,7 @@ tracks/2/path = NodePath("WeaponPosition:position")
tracks/2/interp = 1
tracks/2/loop_wrap = true
tracks/2/imported = false
tracks/2/enabled = true
tracks/2/enabled = false
tracks/2/keys = {
"times": PoolRealArray( 0 ),
"transitions": PoolRealArray( 1 ),
Expand Down Expand Up @@ -477,7 +475,7 @@ tracks/2/path = NodePath("WeaponPosition:position")
tracks/2/interp = 1
tracks/2/loop_wrap = true
tracks/2/imported = false
tracks/2/enabled = true
tracks/2/enabled = false
tracks/2/keys = {
"times": PoolRealArray( 0 ),
"transitions": PoolRealArray( 1 ),
Expand Down Expand Up @@ -505,7 +503,7 @@ tracks/1/path = NodePath("WeaponPosition:position")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/enabled = false
tracks/1/keys = {
"times": PoolRealArray( 0 ),
"transitions": PoolRealArray( 1 ),
Expand Down Expand Up @@ -537,19 +535,17 @@ group = "player"

[node name="AnimatedSprite" type="AnimatedSprite" parent="."]
frames = SubResource( 37 )
animation = "walk_up"
frame = 1
animation = "idle_left"
frame = 2
playing = true
flip_h = true

[node name="AnimationTree" type="AnimationTree" parent="."]
tree_root = SubResource( 69 )
anim_player = NodePath("../AnimationPlayer")
active = true
process_mode = 0
parameters/playback = SubResource( 70 )
parameters/idle/blend_position = Vector2( -0.000947893, -0.677054 )
parameters/walk/blend_position = Vector2( -0.165877, -0.569405 )
parameters/idle/blend_position = Vector2( -0.699919, 0.362069 )
parameters/walk/blend_position = Vector2( 0.130576, -0.0603448 )

[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
anims/idle_down = SubResource( 59 )
Expand All @@ -562,7 +558,6 @@ anims/walk_right = SubResource( 72 )
anims/walk_up = SubResource( 74 )

[node name="WeaponPosition" type="Position2D" parent="."]
position = Vector2( 0, -18 )
position = Vector2( -11.3785, 5.77687 )

[node name="Bow" parent="." instance=ExtResource( 11 )]
position = Vector2( 14, 0 )
[node name="Knife" parent="WeaponPosition" instance=ExtResource( 12 )]
5 changes: 5 additions & 0 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ debug_mes_test={
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":true,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":84,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
]
}
fire={
"deadzone": 0.5,
"events": [ 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]

Expand Down

0 comments on commit 91cd532

Please sign in to comment.