Skip to content
This repository has been archived by the owner on Jun 30, 2023. It is now read-only.

Commit

Permalink
Add attack mechanic demo - tutorial with Heartbeast
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanLovato committed Oct 20, 2017
1 parent eb7ee52 commit 48880f0
Show file tree
Hide file tree
Showing 120 changed files with 2,476 additions and 0 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
36 changes: 36 additions & 0 deletions final/17-Attack demo with Heartbeast/end/Game.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[gd_scene load_steps=4 format=2]

[ext_resource path="res://art/bg.png" type="Texture" id=1]
[ext_resource path="res://characters/player/Player.tscn" type="PackedScene" id=2]
[ext_resource path="res://characters/enemy/Enemy.tscn" type="PackedScene" id=3]

[node name="Game" type="Node"]

[node name="bg" type="Sprite" parent="."]

position = Vector2( 511.5, 320.5 )
scale = Vector2( 0.748902, 0.832031 )
texture = ExtResource( 1 )
_sections_unfolded = [ "Transform" ]
__meta__ = {
"_edit_lock_": true
}

[node name="Player" parent="." instance=ExtResource( 2 )]

position = Vector2( 329, 444 )

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

position = Vector2( 699, 371 )

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

position = Vector2( 622, 559 )

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

position = Vector2( 133, 365 )
max_health = 4


Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions final/17-Attack demo with Heartbeast/end/art/bg.png.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[remap]

importer="texture"
type="StreamTexture"
path="res://.import/bg.png-ca4dd02b5a08671d1e8200d74b5e8347.stex"

[params]

compress/mode=0
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/normal_map=0
flags/repeat=0
flags/filter=true
flags/mipmaps=false
flags/anisotropic=false
flags/srgb=2
process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
stream=false
size_limit=0
detect_3d=true
svg/scale=1.0
105 changes: 105 additions & 0 deletions final/17-Attack demo with Heartbeast/end/characters/Character.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
extends KinematicBody2D

signal health_changed
signal died

export(String) var weapon_scene_path = "res://characters/weapons/spear/Spear.tscn"

var weapon = null
var weapon_path = ""

onready var animation_player = $AnimationPlayer

enum STATES {IDLE, WALK, ATTACK, STAGGER, DIE, DEAD}
var current_state = null
var previous_state = null

var speed = 400
var look_direction = Vector2(1, 0)
var move_direction = Vector2()
var input_direction = Vector2()


export var max_health = 1
var health


func _ready():
health = max_health

# Weapon setup
var weapon_instance = load(weapon_scene_path).instance()
var weapon_anchor = $WeaponSpawnPoint/WeaponAnchorPoint
weapon_anchor.add_child(weapon_instance)

weapon = weapon_anchor.get_child(0)

weapon_path = weapon.get_path()
weapon.connect("attack_finished", self, "_on_Weapon_attack_finished")

_change_state(IDLE)



func _physics_process(delta):

# Movement
if current_state == IDLE:
if input_direction:
_change_state(WALK)

if current_state == WALK:
move_direction = input_direction

if move_direction.x:
look_direction.x = move_direction.x
$WeaponSpawnPoint.scale.x = look_direction.x
if move_direction.y:
look_direction.y = move_direction.y

if not move_direction:
_change_state(IDLE)

var velocity = move_direction * speed * delta
move_and_collide(velocity)


func _change_state(new_state):
previous_state = current_state
current_state = new_state

# initialize/enter the state
match new_state:
IDLE:
animation_player.play("idle")
ATTACK:
# see Weapon.gd for damage management
weapon.attack()
STAGGER:
animation_player.play("take_damage")
DEAD:
queue_free()


func take_damage(count):
if current_state == DEAD:
return

health -= count
if health <= 0:
health = 0
_change_state(DEAD)
emit_signal("died")
return

_change_state(STAGGER)
emit_signal("health_changed", health)


func _on_AnimationPlayer_animation_finished( name ):
if name == "take_damage":
_change_state(IDLE)


func _on_Weapon_attack_finished():
_change_state(IDLE)
123 changes: 123 additions & 0 deletions final/17-Attack demo with Heartbeast/end/characters/Character.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
[gd_scene load_steps=9 format=2]

[ext_resource path="res://characters/Character.gd" type="Script" id=1]
[ext_resource path="res://characters/shared/shadow.png" type="Texture" id=2]
[ext_resource path="res://characters/player/buddy_green.png" type="Texture" id=3]
[ext_resource path="res://characters/DebugCharacter.gd" type="Script" id=4]

[sub_resource type="Animation" id=1]

resource_name = "_setup"
length = 1.0
loop = false
step = 0.1

[sub_resource type="Animation" id=2]

resource_name = "idle"
length = 1.0
loop = false
step = 0.1

[sub_resource type="Animation" id=3]

resource_name = "take_damage"
length = 0.6
loop = false
step = 0.1
tracks/0/type = "value"
tracks/0/path = NodePath("Body:self_modulate")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/imported = false
tracks/0/keys = {
"times": PoolRealArray( 0, 0.6 ),
"transitions": PoolRealArray( 0.390557, 1 ),
"update": 0,
"values": [ Color( 1, 0, 0, 1 ), Color( 1, 1, 1, 1 ) ]
}

[sub_resource type="RectangleShape2D" id=4]

custom_solver_bias = 0.0
extents = Vector2( 19.9141, 16 )
_sections_unfolded = [ "Resource" ]

[node name="Character" type="KinematicBody2D" groups=[
"character",
]]

input_pickable = false
collision_layer = 1
collision_mask = 1
collision/safe_margin = 0.08
script = ExtResource( 1 )
_sections_unfolded = [ "Collision", "collision" ]
weapon_scene_path = "res://characters/weapons/spear/Spear.tscn"
max_health = 1

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

playback_process_mode = 1
playback_default_blend_time = 0.0
root_node = NodePath("..")
anims/_setup = SubResource( 1 )
anims/idle = SubResource( 2 )
anims/take_damage = SubResource( 3 )
playback/active = true
playback/speed = 1.0
blend_times = [ ]
autoplay = ""

[node name="Shadow" type="Sprite" parent="."]

scale = Vector2( 0.546225, 0.546225 )
texture = ExtResource( 2 )
_sections_unfolded = [ "Transform" ]

[node name="Body" type="Sprite" parent="."]

scale = Vector2( 0.446435, 0.446435 )
texture = ExtResource( 3 )
offset = Vector2( 0, -69 )
_sections_unfolded = [ "Offset", "Transform", "Visibility" ]

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

position = Vector2( 0, -15.8886 )
shape = SubResource( 4 )

[node name="WeaponSpawnPoint" type="Node2D" parent="."]

position = Vector2( 0, -30 )
_sections_unfolded = [ "Transform" ]

[node name="WeaponAnchorPoint" type="Node2D" parent="WeaponSpawnPoint"]

_sections_unfolded = [ "Transform" ]

[node name="DisplayDebugInfo" type="Label" parent="."]

anchor_left = 0.0
anchor_top = 0.0
anchor_right = 0.0
anchor_bottom = 0.0
margin_left = -46.0
margin_top = -84.0
margin_right = 47.0
margin_bottom = -68.0
rect_pivot_offset = Vector2( 0, 0 )
rect_clip_content = false
mouse_filter = 2
size_flags_horizontal = 1
size_flags_vertical = 4
text = "Debug info"
align = 1
percent_visible = 1.0
lines_skipped = 0
max_lines_visible = -1
script = ExtResource( 4 )

[connection signal="animation_finished" from="AnimationPlayer" to="." method="_on_AnimationPlayer_animation_finished"]


Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
extends Label

onready var Character = $'..'

func _ready():
visible = false

func _physics_process(delta):
text = str(Character.current_state)
67 changes: 67 additions & 0 deletions final/17-Attack demo with Heartbeast/end/characters/enemy/Enemy.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
extends "res://characters/Character.gd"

export(int) var patrol_distance = 200
var start_position = Vector2()
var end_position = Vector2()

enum STATES_MIND {WAIT, MOVE, ACT}
var current_mind_state = WAIT
var previous_mind_state = null

var last_move_direction = Vector2()

onready var wait_timer = $WaitTimer
onready var attack_timer = $AttackTimer

var first_ai_cycle = true

func _ready():
speed = 240
start_position = position
end_position = start_position + Vector2(patrol_distance, 0)

_change_mind_state(WAIT)
randomize()
wait_timer.stop()
wait_timer.wait_time = randf() * 3
wait_timer.start()


func _physics_process(delta):
if current_mind_state == MOVE:
if input_direction.x == 1 and position.x > end_position.x \
or input_direction.x == -1 and position.x < start_position.x:
last_move_direction = input_direction
_change_mind_state(WAIT)


func _change_mind_state(new_state):
previous_mind_state = current_mind_state
current_mind_state = new_state

match current_mind_state:
WAIT:
input_direction = Vector2()
wait_timer.wait_time = 1.0
wait_timer.start()
MOVE:
input_direction.x = -last_move_direction.x
if last_move_direction.x == 0:
input_direction.x = 1
wait_timer.stop()


func _on_WaitTimer_timeout():
wait_timer.stop()
if first_ai_cycle:
first_ai_cycle = false
_change_mind_state(MOVE)
return

_change_state(ATTACK)
_change_mind_state(ACT)


func _on_Weapon_attack_finished():
_change_mind_state(MOVE)
_change_state(IDLE)
Loading

0 comments on commit 48880f0

Please sign in to comment.