Compare commits

...

3 commits

Author SHA1 Message Date
4402a72ce2 player: add Economy integration 2026-01-26 13:57:19 +13:00
7a2e4111ac world: add Economy node 2026-01-26 13:57:12 +13:00
71abf7e34a economy: add class Economy 2026-01-26 13:56:49 +13:00
4 changed files with 108 additions and 2 deletions

79
economy/economy.gd Normal file
View 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
View 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")

View file

@ -1,10 +1,11 @@
[gd_scene load_steps=9 format=3 uid="uid://eiaw4xbs3suk"] [gd_scene load_steps=10 format=3 uid="uid://eiaw4xbs3suk"]
[ext_resource type="PackedScene" uid="uid://qb8cbljxgnub" path="res://world/killbrick.tscn" id="1_h2yge"] [ext_resource type="PackedScene" uid="uid://qb8cbljxgnub" path="res://world/killbrick.tscn" id="1_h2yge"]
[ext_resource type="PackedScene" uid="uid://cfceg80unq0pe" path="res://player/player.tscn" id="1_ig7tw"] [ext_resource type="PackedScene" uid="uid://cfceg80unq0pe" path="res://player/player.tscn" id="1_ig7tw"]
[ext_resource type="PackedScene" uid="uid://bcmrj6qkemrll" path="res://world/radiohead_cube.tscn" id="2_0xm2m"] [ext_resource type="PackedScene" uid="uid://bcmrj6qkemrll" path="res://world/radiohead_cube.tscn" id="2_0xm2m"]
[ext_resource type="PackedScene" uid="uid://of6tq8gpjxtu" path="res://gears/gear_pickup.tscn" id="3_lquwl"] [ext_resource type="PackedScene" uid="uid://of6tq8gpjxtu" path="res://gears/gear_pickup.tscn" id="3_lquwl"]
[ext_resource type="PackedScene" uid="uid://bafl8q0r61xrg" path="res://gears/gear.tscn" id="4_7mycd"] [ext_resource type="PackedScene" uid="uid://bafl8q0r61xrg" path="res://gears/gear.tscn" id="4_7mycd"]
[ext_resource type="PackedScene" uid="uid://cenj3dcweigwt" path="res://economy/economy.tscn" id="6_7mycd"]
[sub_resource type="BoxShape3D" id="BoxShape3D_7dm0k"] [sub_resource type="BoxShape3D" id="BoxShape3D_7dm0k"]
size = Vector3(100, 2, 100) size = Vector3(100, 2, 100)
@ -52,3 +53,5 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -12, 2, -21)
[node name="Gear" parent="GearPickup" instance=ExtResource("4_7mycd")] [node name="Gear" parent="GearPickup" instance=ExtResource("4_7mycd")]
transform = Transform3D(1, 0, 0, 0, -0.012967386, 0.9999159, 0, -0.9999159, -0.012967386, 0, 0.5, 0) transform = Transform3D(1, 0, 0, 0, -0.012967386, 0.9999159, 0, -0.9999159, -0.012967386, 0, 0.5, 0)
[node name="Economy" parent="." instance=ExtResource("6_7mycd")]

View file

@ -10,6 +10,7 @@ const gear_slots = ["1", "2", "3"]
var starting_gear = preload("res://gears/ball.tscn") var starting_gear = preload("res://gears/ball.tscn")
var ep = PlayerParticipant.new(self)
var suspended = false var suspended = false
var direction = Vector3.ZERO var direction = Vector3.ZERO
var target_velocity = Vector3.ZERO var target_velocity = Vector3.ZERO
@ -27,13 +28,17 @@ func resize_ui():
$HUD.position.x = 0 $HUD.position.x = 0
$HUD.position.y = size.y - hud_h $HUD.position.y = size.y - hud_h
func make_hud(): func make_hud(balance = ep.balance()):
if suspended: # TODO: CHANGE TO DEAD!
return
var text = "" var text = ""
for node in $Backpack.get_children(): for node in $Backpack.get_children():
if not node is Gear: if not node is Gear:
continue continue
text += "%s (%s)\n" % [node.gear_name(), node.name] text += "%s (%s)\n" % [node.gear_name(), node.name]
text += "Holding " + $Pivot/Container/Gear.gear_name() + "\n" text += "Holding " + $Pivot/Container/Gear.gear_name() + "\n"
text += str(balance) + " ₹ (rupees)\n"
text += str(health()) + " hp" text += str(health()) + " hp"
$HUD.text = text $HUD.text = text
@ -147,6 +152,18 @@ func equip(gear: Gear):
message("Backpack full") message("Backpack full")
return false return false
# Economy participant definition
class PlayerParticipant extends Economy.Participant:
var player: Player
func _init(p: Player):
player = p
participant_name = "Player participant"
func message(string):
player.message(string)
func on_balance_update(bal):
player.make_hud(bal)
# Player mechanics # Player mechanics
func move_player(x, z): func move_player(x, z):
@ -217,6 +234,7 @@ func do_gears():
# Engine callbacks # Engine callbacks
func _ready(): func _ready():
get_tree().current_scene.get_node("Economy").register(ep)
get_viewport().size_changed.connect(resize_ui) get_viewport().size_changed.connect(resize_ui)
resize_ui() resize_ui()
respawn() respawn()