Skip to content

Commit

Permalink
Added monster drops
Browse files Browse the repository at this point in the history
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 gdquest-demos#59
  • Loading branch information
guilhermehto committed Nov 26, 2018
1 parent f3e19d8 commit d5b2211
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 17 deletions.
13 changes: 11 additions & 2 deletions godot/combat/Rewards.gd
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ 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])
Expand All @@ -21,7 +22,15 @@ func initialize(battlers : Array):

func _add_reward(battler : Battler):
experience_earned += battler.stats.experience
# TODO add item drops
if battler.drops != null:
for drop in battler.drops:
if drop.chance < 1.0 and rand_range(0.0, 1.0) > drop.chance:
continue
var quantity : int = 1 if drop.max_quantity == 1 else rand_range(drop.min_quantity, drop.max_quantity) as int
drops.append({
'item': drop.item,
'quantity': quantity
})

func _reward_to_battlers() -> Array:
"""
Expand Down Expand Up @@ -56,7 +65,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.quantity, 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
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.items if _t.drops != null else null

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

"""
While we still can't export custom types, we'll have to use an Array of dictionaries
Example:
{
'item': Sword.tres,
'min_quantity': 1,
'max_quantity': 1,
'chance': 0.5
}, {
'item': Potion.tres,
'min_quantity': 3,
'max_quantity': 20,
'chance': 0.25
},
"""

export var items : Array
13 changes: 0 additions & 13 deletions godot/combat/battlers/enemies/Porcupine.tres

This file was deleted.

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

[ext_resource path="res://animation/PorcupineAnim.tscn" type="PackedScene" id=1]
[ext_resource path="res://combat/battlers/enemies/porcupine/PorcupineDrops.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 = ExtResource( 2 )

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

[ext_resource path="res://items/Potion.tres" type="Resource" id=1]
[ext_resource path="res://combat/battlers/enemies/MonsterDrops.gd" type="Script" id=2]

[resource]

script = ExtResource( 2 )
items = [ {
"chance": 0.75,
"item": ExtResource( 1 ),
"max_quantity": 4,
"min_quantity": 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 d5b2211

Please sign in to comment.