WIP: economy: add money system #7
2 changed files with 85 additions and 0 deletions
79
economy/economy.gd
Normal file
79
economy/economy.gd
Normal file
|
|
@ -0,0 +1,79 @@
|
||||||
|
# Economy is a game entity that tracks money.
|
||||||
|
|
||||||
|
# How money is minted, how money changes hands, and which
|
||||||
|
# money is valid is all controlled by Economy. When an
|
||||||
|
# in-game item is purchased, the money does not disappear
|
||||||
|
# but rather goes to the Market, an economic entity which
|
||||||
|
# can give back to players through investments or other means.
|
||||||
|
|
||||||
|
# Economy also tracks GDP, which is the total amount of
|
||||||
|
# money that has changed hands so far during the game.
|
||||||
|
class_name Economy extends Node
|
||||||
|
|
||||||
|
# Participant in the game economy
|
||||||
|
@abstract class Participant:
|
||||||
|
var participant_name = "Unnamed participant"
|
||||||
|
var _tokens = PackedInt32Array()
|
||||||
|
|
||||||
|
func balance():
|
||||||
|
return _tokens.size()
|
||||||
|
|
||||||
|
func hold_tokens(tokens):
|
||||||
|
_tokens.append_array(tokens)
|
||||||
|
on_balance_update(balance())
|
||||||
|
|
||||||
|
func pop_tokens(amount):
|
||||||
|
if balance() < amount:
|
||||||
|
return []
|
||||||
|
|
||||||
|
var dropped = []
|
||||||
|
for i in range(amount):
|
||||||
|
dropped.append(_tokens[_tokens.size() - 1])
|
||||||
|
_tokens.remove_at(_tokens.size() - 1)
|
||||||
|
on_balance_update(balance())
|
||||||
|
return dropped
|
||||||
|
|
||||||
|
@abstract func message(string)
|
||||||
|
@abstract func on_balance_update(bal)
|
||||||
|
|
||||||
|
class Market extends Participant:
|
||||||
|
func _init():
|
||||||
|
participant_name = "Market"
|
||||||
|
func message(_s):
|
||||||
|
pass
|
||||||
|
func on_balance_update(_bal):
|
||||||
|
pass
|
||||||
|
|
||||||
|
const max_money_supply = 1_000_000
|
||||||
|
|
||||||
|
var _gdp = 0
|
||||||
|
var _market = Market.new()
|
||||||
|
var _participants = []
|
||||||
|
var _token_ptr = 0
|
||||||
|
|
||||||
|
# register a Participant to receive benefits
|
||||||
|
func register(p: Participant):
|
||||||
|
_participants.append(p)
|
||||||
|
|
||||||
|
func transfer(amount, sender: Participant, recipient: Participant):
|
||||||
|
if sender.balance() < amount:
|
||||||
|
return false
|
||||||
|
recipient.hold_tokens(sender.pop_tokens(amount))
|
||||||
|
_gdp += amount
|
||||||
|
|
||||||
|
func mint(amount):
|
||||||
|
var tokens = PackedInt32Array()
|
||||||
|
if _token_ptr + amount > max_money_supply:
|
||||||
|
return false
|
||||||
|
for i in range(amount):
|
||||||
|
tokens.append(_token_ptr)
|
||||||
|
_token_ptr += 1
|
||||||
|
_market.hold_tokens(tokens)
|
||||||
|
return true
|
||||||
|
|
||||||
|
func _ready():
|
||||||
|
while true:
|
||||||
|
if len(_participants) > 0:
|
||||||
|
mint(1000)
|
||||||
|
transfer(1000, _market, _participants[0])
|
||||||
|
break
|
||||||
6
economy/economy.tscn
Normal file
6
economy/economy.tscn
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
[gd_scene load_steps=2 format=3 uid="uid://cenj3dcweigwt"]
|
||||||
|
|
||||||
|
[ext_resource type="Script" uid="uid://dl1jbgt83gocy" path="res://economy/economy.gd" id="1_n84lp"]
|
||||||
|
|
||||||
|
[node name="Economy" type="Node"]
|
||||||
|
script = ExtResource("1_n84lp")
|
||||||
Loading…
Add table
Add a link
Reference in a new issue