From 5b1fcd069f049271a89da2bbe75014471071a3e2 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Tue, 2 Aug 2022 16:26:23 +0200 Subject: [PATCH] Working on coin pickup --- Globals.gd | 2 + addons/gdhexgrid/project.godot | 27 +++++++++++ project.godot | 6 +++ scenes/Coin.gd | 10 ++++- scenes/Coin.tscn | 13 +++++- scenes/Game.tscn | 74 ++++++++++++++++++------------- scenes/Player.tscn | 32 +++++++++++++ scenes/TestScenes/PlayerCoin.gd | 30 +++++++++++++ scenes/TestScenes/PlayerCoin.tscn | 16 +++++++ scenes/World.gd | 22 ++++++--- scenes/pirate.gd | 17 ++++++- 11 files changed, 208 insertions(+), 41 deletions(-) create mode 100644 addons/gdhexgrid/project.godot create mode 100644 scenes/Player.tscn create mode 100644 scenes/TestScenes/PlayerCoin.gd create mode 100644 scenes/TestScenes/PlayerCoin.tscn diff --git a/Globals.gd b/Globals.gd index ca4785b..7c8c361 100644 --- a/Globals.gd +++ b/Globals.gd @@ -23,6 +23,8 @@ var zoom_birdy = 4 var game_num_islands = 1 +var game_fixed_seed = 0 + # Called when the node enters the scene tree for the first time. func _ready(): HexGrid.hex_scale = Vector2(hex_size, hex_size) diff --git a/addons/gdhexgrid/project.godot b/addons/gdhexgrid/project.godot new file mode 100644 index 0000000..89039ec --- /dev/null +++ b/addons/gdhexgrid/project.godot @@ -0,0 +1,27 @@ +; Engine configuration file. +; It's best edited using the editor UI and not directly, +; since the parameters that go here are not all obvious. +; +; Format: +; [section] ; section goes between [] +; param=value ; assign values to parameters + +config_version=4 + +_global_script_classes=[ ] +_global_script_class_icons={ +} + +[application] + +config/name="HexGrid" +run/main_scene="res://demo_2d.tscn" +config/icon="res://icon.png" + +[editor_plugins] + +enabled=PoolStringArray( "gut" ) + +[rendering] + +environment/default_environment="res://default_env.tres" diff --git a/project.godot b/project.godot index 354375f..7d1c46d 100644 --- a/project.godot +++ b/project.godot @@ -46,6 +46,7 @@ export=false environ=false clear=false clear_color=false +layer=false [input] @@ -79,6 +80,11 @@ interact={ ] } +[layer_names] + +2d_physics/layer_1="Player" +2d_physics/layer_2="Coin" + [physics] common/enable_pause_aware_picking=true diff --git a/scenes/Coin.gd b/scenes/Coin.gd index b805e39..7725d91 100644 --- a/scenes/Coin.gd +++ b/scenes/Coin.gd @@ -1,7 +1,8 @@ -extends Node2D +extends KinematicBody2D onready var coin_sprite = get_node("CoinSprite") +const COIN_INITIAL_LIFETIME = 10 var velocity = Vector2(0, 0) var z_pos = 0 @@ -9,9 +10,12 @@ var z_vel = 0 var gravity = 1500 var bounciness = 0.5 var lifetime = 10 +var pickup_timeout = 3 # Called when the node enters the scene tree for the first time. func _ready(): + lifetime = COIN_INITIAL_LIFETIME + set_collision_layer_bit(2, false) pass # Replace with function body. func _draw(): @@ -20,6 +24,10 @@ func _draw(): pass func _process(delta): + print (COIN_INITIAL_LIFETIME - lifetime) + if COIN_INITIAL_LIFETIME - lifetime >= pickup_timeout: + set_collision_layer_bit(2, true) + z_vel = z_vel - gravity * delta z_pos = z_pos + z_vel * delta diff --git a/scenes/Coin.tscn b/scenes/Coin.tscn index 0018a3f..2f3b144 100644 --- a/scenes/Coin.tscn +++ b/scenes/Coin.tscn @@ -1,11 +1,20 @@ -[gd_scene load_steps=3 format=2] +[gd_scene load_steps=4 format=2] [ext_resource path="res://assets/coin.svg" type="Texture" id=1] [ext_resource path="res://scenes/Coin.gd" type="Script" id=2] -[node name="Node2D" type="Node2D"] +[sub_resource type="CircleShape2D" id=1] +radius = 7.0 + +[node name="Coin" type="KinematicBody2D"] modulate = Color( 1, 1, 1, 0.792157 ) +z_index = 1 +collision_layer = 0 +collision_mask = 0 script = ExtResource( 2 ) [node name="CoinSprite" type="Sprite" parent="."] texture = ExtResource( 1 ) + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource( 1 ) diff --git a/scenes/Game.tscn b/scenes/Game.tscn index ea04c42..aeddcaa 100644 --- a/scenes/Game.tscn +++ b/scenes/Game.tscn @@ -1,15 +1,13 @@ -[gd_scene load_steps=30 format=2] +[gd_scene load_steps=29 format=2] [ext_resource path="res://scenes/World.gd" type="Script" id=1] [ext_resource path="res://scenes/Grid.gd" type="Script" id=2] -[ext_resource path="res://assets/pirate.svg" type="Texture" id=3] +[ext_resource path="res://scenes/Player.tscn" type="PackedScene" id=3] [ext_resource path="res://scenes/FPSValue.gd" type="Script" id=4] -[ext_resource path="res://scenes/pirate.gd" type="Script" id=5] [ext_resource path="res://fonts/Roboto/Roboto-Regular.ttf" type="DynamicFontData" id=6] [ext_resource path="res://scenes/Editor.gd" type="Script" id=7] [ext_resource path="res://scenes/GridHighlight.gd" type="Script" id=8] [ext_resource path="res://assets/boat.svg" type="Texture" id=9] -[ext_resource path="res://assets/shovel.svg" type="Texture" id=10] [ext_resource path="res://assets/Yeah.png" type="Texture" id=11] [ext_resource path="res://assets/shovel_button.svg" type="Texture" id=12] [ext_resource path="res://scenes/SoundEffectsPlayer.gd" type="Script" id=13] @@ -19,6 +17,7 @@ [ext_resource path="res://assets/arrowback.svg" type="Texture" id=17] [ext_resource path="res://assets/arrowrepeat.svg" type="Texture" id=18] [ext_resource path="res://scenes/Water.tscn" type="PackedScene" id=19] +[ext_resource path="res://MainMenuTheme.tres" type="Theme" id=20] [sub_resource type="DynamicFont" id=1] size = 27 @@ -191,7 +190,7 @@ script = ExtResource( 8 ) [node name="UI" type="CanvasLayer" parent="World"] -[node name="HBoxContainer" type="HBoxContainer" parent="World/UI"] +[node name="GameButtons" type="HBoxContainer" parent="World/UI"] margin_left = 10.0 margin_top = 10.0 margin_right = -10.0 @@ -201,13 +200,13 @@ __meta__ = { "_edit_use_anchors_": false } -[node name="BackButton" type="Button" parent="World/UI/HBoxContainer"] +[node name="BackButton" type="Button" parent="World/UI/GameButtons"] margin_right = 268.0 margin_bottom = 262.0 icon = ExtResource( 17 ) flat = true -[node name="RepeatButton" type="Button" parent="World/UI/HBoxContainer"] +[node name="RepeatButton" type="Button" parent="World/UI/GameButtons"] margin_left = 272.0 margin_right = 540.0 margin_bottom = 262.0 @@ -229,7 +228,7 @@ __meta__ = { [node name="Timer" type="Timer" parent="World/UI/IslandMap"] -[node name="VBoxContainer" type="VBoxContainer" parent="World/UI"] +[node name="ActionButtons" type="VBoxContainer" parent="World/UI"] anchor_left = 1.0 anchor_right = 1.0 anchor_bottom = 1.0 @@ -241,14 +240,14 @@ __meta__ = { "_edit_use_anchors_": false } -[node name="MapButton" type="Button" parent="World/UI/VBoxContainer"] +[node name="MapButton" type="Button" parent="World/UI/ActionButtons"] margin_right = 291.0 margin_bottom = 262.0 rect_min_size = Vector2( 100, 100 ) icon = ExtResource( 15 ) flat = true -[node name="BirdyButton" type="Button" parent="World/UI/VBoxContainer"] +[node name="BirdyButton" type="Button" parent="World/UI/ActionButtons"] margin_top = 283.0 margin_right = 291.0 margin_bottom = 545.0 @@ -256,7 +255,7 @@ rect_min_size = Vector2( 100, 100 ) icon = ExtResource( 16 ) flat = true -[node name="DigButton" type="Button" parent="World/UI/VBoxContainer"] +[node name="DigButton" type="Button" parent="World/UI/ActionButtons"] margin_top = 566.0 margin_right = 291.0 margin_bottom = 828.0 @@ -298,7 +297,6 @@ __meta__ = { } [node name="DebugContainer" type="HBoxContainer" parent="World/UI"] -visible = false anchor_top = 1.0 anchor_right = 1.0 anchor_bottom = 1.0 @@ -440,22 +438,35 @@ text = "0" align = 2 script = ExtResource( 4 ) +[node name="GameScoreHud" type="HBoxContainer" parent="World/UI"] +anchor_right = 1.0 +margin_right = 40.0 +margin_bottom = 40.0 +alignment = 1 +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="Label" type="Label" parent="World/UI/GameScoreHud"] +margin_left = 412.0 +margin_right = 611.0 +margin_bottom = 76.0 +theme = ExtResource( 20 ) +text = "Coins: " + +[node name="CoinScoreLabel" type="Label" parent="World/UI/GameScoreHud"] +margin_left = 615.0 +margin_right = 652.0 +margin_bottom = 76.0 +theme = ExtResource( 20 ) +text = "0" +__meta__ = { +"_edit_use_anchors_": false +} + [node name="DigSites" type="Node2D" parent="World"] -[node name="PlayerChar" type="Node2D" parent="World"] -script = ExtResource( 5 ) - -[node name="Pirate" type="Sprite" parent="World/PlayerChar"] -scale = Vector2( 0.45498, 0.45498 ) -texture = ExtResource( 3 ) - -[node name="Shovel" type="Sprite" parent="World/PlayerChar"] -visible = false -scale = Vector2( 0.455, 0.455 ) -texture = ExtResource( 10 ) - -[node name="DigTimer" type="Timer" parent="World/PlayerChar"] -one_shot = true +[node name="PlayerChar" parent="World" instance=ExtResource( 3 )] [node name="Camera" type="Camera2D" parent="World"] current = true @@ -507,10 +518,11 @@ render_target_update_mode = 1 [connection signal="treasure_found" from="World" to="SoundEffectsPlayer" method="_on_World_treasure_found"] [connection signal="world_generation_triggered" from="World" to="SoundEffectsPlayer" method="_on_World_world_generation_triggered"] [connection signal="wrong_digsite" from="World" to="SoundEffectsPlayer" method="_on_World_wrong_digsite"] -[connection signal="pressed" from="World/UI/HBoxContainer/BackButton" to="World" method="_on_BackButton_pressed"] -[connection signal="pressed" from="World/UI/HBoxContainer/RepeatButton" to="World" method="_on_GenerateButton_pressed"] +[connection signal="pressed" from="World/UI/GameButtons/BackButton" to="World" method="_on_BackButton_pressed"] +[connection signal="pressed" from="World/UI/GameButtons/RepeatButton" to="World" method="_on_GenerateButton_pressed"] [connection signal="visibility_changed" from="World/UI/IslandMap" to="World" method="_on_IslandMap_visibility_changed"] -[connection signal="pressed" from="World/UI/VBoxContainer/MapButton" to="World" method="_on_MapButton_pressed"] -[connection signal="pressed" from="World/UI/VBoxContainer/BirdyButton" to="World" method="_on_BirdyButton_pressed"] -[connection signal="pressed" from="World/UI/VBoxContainer/DigButton" to="World" method="_on_DigButton_pressed"] +[connection signal="pressed" from="World/UI/ActionButtons/MapButton" to="World" method="_on_MapButton_pressed"] +[connection signal="pressed" from="World/UI/ActionButtons/BirdyButton" to="World" method="_on_BirdyButton_pressed"] +[connection signal="pressed" from="World/UI/ActionButtons/DigButton" to="World" method="_on_DigButton_pressed"] [connection signal="toggled" from="World/UI/DebugContainer/EditIslandButton" to="Editor" method="_on_EditIslandButton_toggled"] +[connection signal="coin_collected" from="World/PlayerChar" to="World" method="_on_PlayerChar_coin_collected"] diff --git a/scenes/Player.tscn b/scenes/Player.tscn new file mode 100644 index 0000000..8bf31cd --- /dev/null +++ b/scenes/Player.tscn @@ -0,0 +1,32 @@ +[gd_scene load_steps=5 format=2] + +[ext_resource path="res://assets/shovel.svg" type="Texture" id=1] +[ext_resource path="res://assets/pirate.svg" type="Texture" id=2] +[ext_resource path="res://scenes/pirate.gd" type="Script" id=3] + +[sub_resource type="RectangleShape2D" id=11] +extents = Vector2( 23, 46.5 ) + +[node name="PlayerChar" type="Node2D"] +script = ExtResource( 3 ) + +[node name="Pirate" type="Sprite" parent="."] +scale = Vector2( 0.45498, 0.45498 ) +texture = ExtResource( 2 ) + +[node name="Shovel" type="Sprite" parent="."] +visible = false +scale = Vector2( 0.455, 0.455 ) +texture = ExtResource( 1 ) + +[node name="DigTimer" type="Timer" parent="."] +one_shot = true + +[node name="Collider" type="Area2D" parent="."] +collision_mask = 2 + +[node name="ColShape" type="CollisionShape2D" parent="Collider"] +position = Vector2( 4, -1.5 ) +shape = SubResource( 11 ) + +[connection signal="body_entered" from="Collider" to="." method="_on_Collider_body_entered"] diff --git a/scenes/TestScenes/PlayerCoin.gd b/scenes/TestScenes/PlayerCoin.gd new file mode 100644 index 0000000..0a04098 --- /dev/null +++ b/scenes/TestScenes/PlayerCoin.gd @@ -0,0 +1,30 @@ +extends Node2D + + +# Declare member variables here. Examples: +# var a = 2 +# var b = "text" + +onready var player = $PlayerChar +onready var coin = $Coin + +# Called when the node enters the scene tree for the first time. +func _ready(): + print (player) + print (coin) + +func _unhandled_input(event): + if event is InputEventMouseButton: +# var input_tile = Globals.HexGrid.get_hex_center(Globals.ScreenToHex(mouse_pos, WorldCamera)) + # Move main character + if event.pressed and event.button_index == BUTTON_LEFT: + player.target = event.position + + +# Called every frame. 'delta' is the elapsed time since the previous frame. +#func _process(delta): +# pass + + +func _on_PlayerChar_coin_collected(): + print ("Coin Collected!") diff --git a/scenes/TestScenes/PlayerCoin.tscn b/scenes/TestScenes/PlayerCoin.tscn new file mode 100644 index 0000000..2d5c199 --- /dev/null +++ b/scenes/TestScenes/PlayerCoin.tscn @@ -0,0 +1,16 @@ +[gd_scene load_steps=4 format=2] + +[ext_resource path="res://scenes/Coin.tscn" type="PackedScene" id=1] +[ext_resource path="res://scenes/Player.tscn" type="PackedScene" id=2] +[ext_resource path="res://scenes/TestScenes/PlayerCoin.gd" type="Script" id=3] + +[node name="Node2D" type="Node2D"] +script = ExtResource( 3 ) + +[node name="PlayerChar" parent="." instance=ExtResource( 2 )] +position = Vector2( 276, 241 ) + +[node name="Coin" parent="." instance=ExtResource( 1 )] +position = Vector2( 436, 266 ) + +[connection signal="coin_collected" from="PlayerChar" to="." method="_on_PlayerChar_coin_collected"] diff --git a/scenes/World.gd b/scenes/World.gd index f91afab..0e3e8cf 100644 --- a/scenes/World.gd +++ b/scenes/World.gd @@ -24,9 +24,11 @@ onready var FPSValueLabel = get_node("UI/DebugContainer/FPSValue") onready var IslandMap = get_node("UI/IslandMap") onready var IslandMapRenderer = get_node("../IslandMapRenderer") -onready var MapButton = get_node("UI/VBoxContainer/MapButton") -onready var BirdyButton = get_node("UI/VBoxContainer/BirdyButton") -onready var DigButton = get_node("UI/VBoxContainer/DigButton") +onready var MapButton = get_node("UI/ActionButtons/MapButton") +onready var BirdyButton = get_node("UI/ActionButtons/BirdyButton") +onready var DigButton = get_node("UI/ActionButtons/DigButton") + +onready var CoinScoreLabel = get_node("UI/GameScoreHud/CoinScoreLabel") onready var BirdyTimer = get_node("Birdy/Timer") onready var MapTimer = get_node("UI/IslandMap/Timer") @@ -286,14 +288,18 @@ func reset(): clear_digsites() SuccessMessage.visible = false + CoinScoreLabel.text = "0" func generate(): reset() var rng = RandomNumberGenerator.new() - rng.randomize() - randomize() + + if Globals.game_fixed_seed != 0: + rng.set_seed(0) + rng.randomize() + randomize() var radius = 800 var num_islands = Globals.game_num_islands @@ -684,6 +690,12 @@ func _on_GenerateButton_pressed(): RepeatButton.disabled = true generate() + func _on_BackButton_pressed(): get_tree().change_scene("res://Menu.tscn") + + +func _on_PlayerChar_coin_collected(): + CoinScoreLabel.text = str(int (CoinScoreLabel.text) + 1) + diff --git a/scenes/pirate.gd b/scenes/pirate.gd index 28e692f..79bb7bd 100644 --- a/scenes/pirate.gd +++ b/scenes/pirate.gd @@ -9,9 +9,14 @@ enum State { } signal dig_stopped +signal coin_collected onready var Shovel = get_node("Shovel") onready var DigTimer = get_node("DigTimer") +onready var Collider = get_node("Collider") + +var Coin = preload ("Coin.gd") +var coin = Coin.new() var velocity = Vector2() var target = Vector2() @@ -49,15 +54,16 @@ func _physics_process(delta): if state == State.Digging: velocity = Vector2.ZERO + position = position + velocity * delta - + var tile = Globals.WorldToHex(position) if tile != cur_tile: prev_tile = cur_tile cur_tile = tile Shovel.visible = state == State.Digging - + # Called when the node enters the scene tree for the first time. func _ready(): @@ -81,3 +87,10 @@ func on_dig_start(): func on_dig_stop(): state = State.Walking emit_signal ("dig_stopped") + + +func _on_Collider_body_entered(body): + if body is Coin: + emit_signal ("coin_collected") + body.queue_free() +