Skip to content

Commit

Permalink
initial prize packs code
Browse files Browse the repository at this point in the history
  • Loading branch information
IQGobo committed Aug 2, 2020
1 parent bb71f7f commit 2432827
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
25 changes: 25 additions & 0 deletions engine/prizepack.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
extends Node

var index : int = 0
var pool : Array = []
var rng : RandomNumberGenerator = RandomNumberGenerator.new()
var chance : float = 0.5

func _ready():
rng.randomize()

func _init(items : Array, threshold : float):
pool = items
chance = threshold

# returns the loot drop item for the current kill count in the prize pack
# keeps track of which item to drop and only provides an item if a random
# value matches the chance to drop an item
func get_drop() -> String:
var drop = ""
if rng.randf() <= chance:
drop = pool[index % pool.size()]
index += 1
if index > pool.size() * 100:
index = 0
return drop
43 changes: 43 additions & 0 deletions engine/prizepacks.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
extends Node
class_name prizepacks

# will hold an array of prizepack objects
var packs : Array = []

# will remap prizepack indexes, usefull for randomized games
var remapped : Array = []

func _init():
# default prize pack definitions, array of items to drop and the chance to drop
var packdefs : Array = [
{"pool": [""], "chance": 1.0},
{"pool": ["smallheart","whitetetran"], "chance": 0.5},
{"pool": ["whitetetran","whitetetran","smallheart","redtetran"], "chance" : 0.5},
{"pool": ["smallheart","redtetran","smallheart","bluetetran"], "chance": 0.5},
{"pool": ["whitetetran","redtetran","bluetetran","greentetran"], "chance": 0.5}
]
var pp = load("res://engine/prizepack.gd")
for p in packdefs:
packs.append(pp.new(packdefs[p].pool, packdefs[p].chance))

func remap():
if packs.size() > 0:
for i in range(packs.size()):
remapped[i] = i
else:
remapped = []

func set_map(map : Array):
if map.size() > packs.size():
remap()
else:
remapped = map

func _ready():
remap()

# each enemy should have a prizepack property (integer) to pass to this function on kill
func get_drop_from_pack(pack : int) -> String:
if pack >= packs.size():
return ""
return packs[remapped[pack]].get_drop()
8 changes: 7 additions & 1 deletion project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,18 @@ _global_script_classes=[ {
"class": "Player",
"language": "GDScript",
"path": "res://player/player.gd"
}, {
"base": "Node",
"class": "prizepacks",
"language": "GDScript",
"path": "res://engine/prizepacks.gd"
} ]
_global_script_class_icons={
"Enemy": "",
"Entity": "",
"Item": "",
"Player": ""
"Player": "",
"prizepacks": ""
}

[application]
Expand Down

0 comments on commit 2432827

Please sign in to comment.