diff --git a/Player.gd b/Player.gd deleted file mode 100644 index 1da20ef..0000000 --- a/Player.gd +++ /dev/null @@ -1,14 +0,0 @@ -extends KinematicBody - -const SPEED = 3.0 - - -# Called when the node enters the scene tree for the first time. -func _ready(): - pass # Replace with function body. - -func _physics_process(delta): - var input = Vector3(Input.get_axis("left", "right"), 0.0, Input.get_axis("up", "down")) - var motion = Plane.PLANE_XZ.project(get_viewport().get_camera().global_transform.basis.xform(input)).normalized() * SPEED - - move_and_slide(motion) diff --git a/project.godot b/project.godot index 40fc4f0..89add5f 100644 --- a/project.godot +++ b/project.godot @@ -11,7 +11,7 @@ config_version=4 [application] config/name="project-dung" -run/main_scene="res://Prototype.tscn" +run/main_scene="res://scenes/Prototype.tscn" config/icon="res://icon.png" [display] diff --git a/scenes/Prototype.tscn b/scenes/Prototype.tscn new file mode 100644 index 0000000..bee9d6b --- /dev/null +++ b/scenes/Prototype.tscn @@ -0,0 +1,10 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://scenes/player.tscn" type="PackedScene" id=1] +[ext_resource path="res://scenes/moon.tscn" type="PackedScene" id=2] + +[node name="Prototype" type="Spatial"] + +[node name="moon" parent="." instance=ExtResource( 2 )] + +[node name="Player" parent="." instance=ExtResource( 1 )] diff --git a/scenes/coin_machine.tscn b/scenes/coin_machine.tscn new file mode 100644 index 0000000..098f850 --- /dev/null +++ b/scenes/coin_machine.tscn @@ -0,0 +1,12 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://scripts/coin_machine.gd" type="Script" id=1] + +[sub_resource type="SphereShape" id=1] + +[node name="coin_machine" type="Area"] +script = ExtResource( 1 ) + +[node name="CollisionShape" type="CollisionShape" parent="."] +transform = Transform( 2, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0 ) +shape = SubResource( 1 ) diff --git a/scenes/dev-akshay.tscn b/scenes/dev-akshay.tscn new file mode 100644 index 0000000..63a8ac6 --- /dev/null +++ b/scenes/dev-akshay.tscn @@ -0,0 +1,15 @@ +[gd_scene load_steps=4 format=2] + +[ext_resource path="res://scenes/player.tscn" type="PackedScene" id=1] +[ext_resource path="res://scenes/moon.tscn" type="PackedScene" id=2] +[ext_resource path="res://scenes/power_station.tscn" type="PackedScene" id=3] + +[node name="dev_akshay" type="Spatial"] + +[node name="moon" parent="." instance=ExtResource( 2 )] + +[node name="power_station" parent="." instance=ExtResource( 3 )] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0.00300264, 1.00556, -4.18269 ) +time_to_generate = 5.0 + +[node name="Player" parent="." instance=ExtResource( 1 )] diff --git a/moon.tscn b/scenes/moon.tscn similarity index 100% rename from moon.tscn rename to scenes/moon.tscn diff --git a/Prototype.tscn b/scenes/player.tscn similarity index 56% rename from Prototype.tscn rename to scenes/player.tscn index f364f5c..de7e87a 100644 --- a/Prototype.tscn +++ b/scenes/player.tscn @@ -1,7 +1,6 @@ [gd_scene load_steps=5 format=2] -[ext_resource path="res://Player.gd" type="Script" id=1] -[ext_resource path="res://moon.tscn" type="PackedScene" id=2] +[ext_resource path="res://scripts/Player.gd" type="Script" id=1] [sub_resource type="CapsuleMesh" id=1] radius = 0.2 @@ -11,22 +10,25 @@ mid_height = 0.85 radius = 0.210213 height = 0.85525 -[node name="Prototype" type="Spatial"] +[sub_resource type="SphereShape" id=3] -[node name="moon" parent="." instance=ExtResource( 2 )] - -[node name="Player" type="KinematicBody" parent="."] +[node name="Player" type="KinematicBody"] script = ExtResource( 1 ) -[node name="MeshInstance" type="MeshInstance" parent="Player"] +[node name="MeshInstance" type="MeshInstance" parent="."] transform = Transform( 0.994286, 0, 0, 0, -4.34616e-08, -0.994286, 0, 0.994286, -4.34616e-08, 0, 0.666587, 0 ) mesh = SubResource( 1 ) skeleton = NodePath("../..") -[node name="CollisionShape" type="CollisionShape" parent="Player"] +[node name="CollisionShape" type="CollisionShape" parent="."] transform = Transform( 1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0.641703, 0 ) shape = SubResource( 2 ) -[node name="Camera" type="Camera" parent="Player"] +[node name="Camera" type="Camera" parent="."] transform = Transform( 1, 0, 0, 0, 0.5, 0.866025, 0, -0.866025, 0.5, 0, 5.44166, 3.39746 ) fov = 50.0 + +[node name="TriggerVolume" type="Area" parent="."] + +[node name="CollisionShape" type="CollisionShape" parent="TriggerVolume"] +shape = SubResource( 3 ) diff --git a/scenes/power_station.tscn b/scenes/power_station.tscn new file mode 100644 index 0000000..1f0f70d --- /dev/null +++ b/scenes/power_station.tscn @@ -0,0 +1,20 @@ +[gd_scene load_steps=4 format=2] + +[ext_resource path="res://scripts/power_station.gd" type="Script" id=1] +[ext_resource path="res://scenes/coin_machine.tscn" type="PackedScene" id=2] + +[sub_resource type="CubeMesh" id=1] + +[node name="power_station" type="Spatial"] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0 ) +script = ExtResource( 1 ) +time_to_generate = 1.0 + +[node name="coin_machine" parent="." instance=ExtResource( 2 )] +transform = Transform( 4, 0, 0, 0, 2, 0, 0, 0, 4, 0, 0, 0 ) +coins_required = 0 + +[node name="MeshInstance" type="MeshInstance" parent="."] +mesh = SubResource( 1 ) + +[connection signal="coin_requirement_met" from="coin_machine" to="." method="_on_coin_machine_coin_requirement_met"] diff --git a/scripts/Player.gd b/scripts/Player.gd new file mode 100644 index 0000000..37a078d --- /dev/null +++ b/scripts/Player.gd @@ -0,0 +1,63 @@ +extends KinematicBody + +const SPEED = 3.0 + +export var inventory = { "ore" : 0 } + +# Called when the node enters the scene tree for the first time. +func _ready(): + pass # Replace with function body. + +func _physics_process(delta): + var input = Vector3(Input.get_axis("left", "right"), 0.0, Input.get_axis("up", "down")) + var motion = Plane.PLANE_XZ.project(get_viewport().get_camera().global_transform.basis.xform(input)).normalized() * SPEED + + move_and_slide(motion) + +func can_afford(item_name, item_amount): + assert(item_amount > 0) + var current_amount : int = 0 + if(inventory.has(item_name)): + current_amount = inventory[item_name] + return current_amount >= item_amount + +func modify_inventory(item_name, item_amount): + if item_amount == 0: + return true + + var is_op_valid : bool = true + + # Note(asr): check if you can afford the item if we are removing + # for item grants op is always valid + if item_amount < 0: + is_op_valid = can_afford(item_name, abs(item_amount)) + + if not is_op_valid: + return false + + var current_amount : int = 0 + if inventory.has(item_name): + current_amount = inventory[item_name] + # Note(asr): clamp to 0. can't have negative items + var new_amount : int = max(current_amount + item_amount, 0) + inventory[item_name] = new_amount + print("inventory updated: ", item_name, " : ", new_amount) + return true + +func _unhandled_input(event): + if event.is_action_pressed("action"): + for area in $TriggerVolume.get_overlapping_areas(): + if try_trigger_interact(area): + break + +func try_trigger_interact(area): + var owner = area + if area.has_meta("owner"): + owner = area.get_meta("owner") + + if not owner.has_method("on_player_interact"): + return false + + owner.on_player_interact(self) + print("player interacted with ", owner) + return true diff --git a/scripts/coin_machine.gd b/scripts/coin_machine.gd new file mode 100644 index 0000000..a2cbd34 --- /dev/null +++ b/scripts/coin_machine.gd @@ -0,0 +1,16 @@ +extends Area + +export var coins_required : int = 1 + +signal coin_requirement_met; + +func _ready(): + assert(coins_required >= 0) + set_meta("owner", self) + +func on_player_interact(player): + assert(player.has_method("modify_inventory")) + if not player.modify_inventory("coins", -coins_required): + return + + emit_signal("coin_requirement_met", player) diff --git a/scripts/power_station.gd b/scripts/power_station.gd new file mode 100644 index 0000000..c90b2c5 --- /dev/null +++ b/scripts/power_station.gd @@ -0,0 +1,31 @@ +extends Spatial + +export var max_charge_capacity : int = 1 +export var time_to_generate : float = 120 + +var current_holding : int = 0 +var current_timer : float = 0 + +func _ready(): + pass # Replace with function body. + +func _process(delta): + current_timer += delta + if current_timer >= time_to_generate: + generate_charge() + current_timer = 0 + +func generate_charge(): + if current_holding >= max_charge_capacity: + return + current_holding += 1 + print("power station: charge generated") + +func collect_charges_from_station(): + var charges_to_return : int = current_holding + current_holding = 0 + return charges_to_return + +func _on_coin_machine_coin_requirement_met(player): + assert(player.has_method("modify_inventory")) + player.modify_inventory("coins", collect_charges_from_station())