Skip to content

Commit

Permalink
Added monster drops (#61)
Browse files Browse the repository at this point in the history
* Added monster drops

Monsters can now be configured to drop items based on a resource (MonsterDrops.gd).
The usability is not the greatest, but that's due to not being able to export custom types. 
There were two possible approaches that I saw:
1. An `Array` holding dictionaries for each item that can be dropped along with the data needed for the drop behavior.
2.  Create a custom resource that would encapsulate all the data for the items that can be dropped from a monster: The item itself, min and max quantities, and the chance. 

I decided to go with the first one.

ps: The items are _NOT_ being added to the player's inventory . This is just the functionality needed for the monsters to start dropping items.

Closes #59

* Embedded drops array to battler template, fixed code issues
  • Loading branch information
guilhermehto authored and NathanLovato committed Nov 27, 2018
1 parent 225bb33 commit 882333b
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 17 deletions.
15 changes: 13 additions & 2 deletions godot/combat/Rewards.gd
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,26 @@ func initialize(battlers : Array):
$Panel.visible = false
party = []
experience_earned = 0
randomize()
for battler in battlers:
if not battler.party_member:
battler.stats.connect("health_depleted", self, "_add_reward", [battler])
else:
party.append(battler)

func _add_reward(battler : Battler):
"""
Appends dictionaries with the form { 'item': Item.tres, 'amount': amount } of dropped items to the drops array.
"""
experience_earned += battler.stats.experience
# TODO add item drops
for drop in battler.drops:
if drop.chance - randf() > drop.chance:
continue
var amount : int = 1 if drop.max_amount == 1 else round(rand_range(drop.min_amount, drop.max_amount))
drops.append({
'item': drop.item,
'amount': amount
})

func _reward_to_battlers() -> Array:
"""
Expand Down Expand Up @@ -56,7 +67,7 @@ func on_battle_completed():
$Panel/Label.text = "%s Leveled Up to %d" % [battler.name, battler.stats.level + 1]
yield(get_tree().create_timer(2.0), "timeout")
for drop in drops:
$Panel/Label.text = "Found %s" % drop.name
$Panel/Label.text = "Found %s %s(s)" % [drop.amount, drop.item.name]
yield(get_tree().create_timer(2.0), "timeout")
$Panel.visible = false

Expand Down
4 changes: 4 additions & 0 deletions godot/combat/battlers/Battler.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export var template : Resource

const DEFAULT_CHANCE = 0.75
var stats : CharacterStats
var drops : Array
onready var lifebar_anchor = $InterfaceAnchor
onready var skin = $Skin
onready var actions = $Actions
Expand All @@ -26,6 +27,9 @@ func _ready() -> void:

var _t = template as BattlerTemplate
actions.initialize(_t.skills)

drops = _t.drops

skin.add_child(_t.anim.instance())
if stats == null:
var starting_stats = _t.stats as CharacterStats
Expand Down
17 changes: 17 additions & 0 deletions godot/combat/battlers/BattlerTemplate.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,20 @@ class_name BattlerTemplate
export var anim : PackedScene
export var stats : Resource
export var skills : Array

"""
For drops, we're currently using an Array of dictionaries
Example:
{
'item': Sword.tres,
'min_amount': 1,
'max_amount': 1,
'chance': 0.5
}, {
'item': Potion.tres,
'min_amount': 3,
'max_amount': 20,
'chance': 0.25
},
"""
export var drops : Array
13 changes: 0 additions & 13 deletions godot/combat/battlers/enemies/Porcupine.tres

This file was deleted.

20 changes: 20 additions & 0 deletions godot/combat/battlers/enemies/porcupine/Porcupine.tres
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[gd_resource type="Resource" load_steps=5 format=2]

[ext_resource path="res://animation/PorcupineAnim.tscn" type="PackedScene" id=1]
[ext_resource path="res://items/Potion.tres" type="Resource" id=2]
[ext_resource path="res://combat/battlers/BattlerTemplate.gd" type="Script" id=3]
[ext_resource path="res://combat/battlers/jobs/Porcupine.tres" type="Resource" id=4]

[resource]

script = ExtResource( 3 )
anim = ExtResource( 1 )
stats = ExtResource( 4 )
skills = [ ]
drops = [ {
"chance": 0.75,
"item": ExtResource( 2 ),
"max_amount": 4,
"min_amount": 2
} ]

5 changes: 3 additions & 2 deletions godot/combat/battlers/formations/PorcupineFormation001.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
[ext_resource path="res://combat/battlers/Formation.gd" type="Script" id=1]
[ext_resource path="res://combat/background/StonePlatform.tscn" type="PackedScene" id=2]
[ext_resource path="res://combat/battlers/formations/FormationMember.tscn" type="PackedScene" id=3]
[ext_resource path="res://combat/battlers/enemies/Porcupine.tres" type="Resource" id=4]
[ext_resource path="res://combat/battlers/enemies/StrongerPorcupine.tres" type="Resource" id=5]
[ext_resource path="res://combat/battlers/enemies/porcupine/Porcupine.tres" type="Resource" id=4]
[ext_resource path="res://combat/battlers/enemies/porcupine/StrongerPorcupine.tres" type="Resource" id=5]


[node name="Formation" type="Node"]
script = ExtResource( 1 )
Expand Down

0 comments on commit 882333b

Please sign in to comment.