Working on coin pickup

master
Martin Felis 2022-08-02 16:26:23 +02:00
parent 861f99b1cf
commit 5b1fcd069f
11 changed files with 208 additions and 41 deletions

View File

@ -23,6 +23,8 @@ var zoom_birdy = 4
var game_num_islands = 1 var game_num_islands = 1
var game_fixed_seed = 0
# Called when the node enters the scene tree for the first time. # Called when the node enters the scene tree for the first time.
func _ready(): func _ready():
HexGrid.hex_scale = Vector2(hex_size, hex_size) HexGrid.hex_scale = Vector2(hex_size, hex_size)

View File

@ -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"

View File

@ -46,6 +46,7 @@ export=false
environ=false environ=false
clear=false clear=false
clear_color=false clear_color=false
layer=false
[input] [input]
@ -79,6 +80,11 @@ interact={
] ]
} }
[layer_names]
2d_physics/layer_1="Player"
2d_physics/layer_2="Coin"
[physics] [physics]
common/enable_pause_aware_picking=true common/enable_pause_aware_picking=true

View File

@ -1,7 +1,8 @@
extends Node2D extends KinematicBody2D
onready var coin_sprite = get_node("CoinSprite") onready var coin_sprite = get_node("CoinSprite")
const COIN_INITIAL_LIFETIME = 10
var velocity = Vector2(0, 0) var velocity = Vector2(0, 0)
var z_pos = 0 var z_pos = 0
@ -9,9 +10,12 @@ var z_vel = 0
var gravity = 1500 var gravity = 1500
var bounciness = 0.5 var bounciness = 0.5
var lifetime = 10 var lifetime = 10
var pickup_timeout = 3
# Called when the node enters the scene tree for the first time. # Called when the node enters the scene tree for the first time.
func _ready(): func _ready():
lifetime = COIN_INITIAL_LIFETIME
set_collision_layer_bit(2, false)
pass # Replace with function body. pass # Replace with function body.
func _draw(): func _draw():
@ -20,6 +24,10 @@ func _draw():
pass pass
func _process(delta): 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_vel = z_vel - gravity * delta
z_pos = z_pos + z_vel * delta z_pos = z_pos + z_vel * delta

View File

@ -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://assets/coin.svg" type="Texture" id=1]
[ext_resource path="res://scenes/Coin.gd" type="Script" id=2] [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 ) modulate = Color( 1, 1, 1, 0.792157 )
z_index = 1
collision_layer = 0
collision_mask = 0
script = ExtResource( 2 ) script = ExtResource( 2 )
[node name="CoinSprite" type="Sprite" parent="."] [node name="CoinSprite" type="Sprite" parent="."]
texture = ExtResource( 1 ) texture = ExtResource( 1 )
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource( 1 )

View File

@ -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/World.gd" type="Script" id=1]
[ext_resource path="res://scenes/Grid.gd" type="Script" id=2] [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/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://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/Editor.gd" type="Script" id=7]
[ext_resource path="res://scenes/GridHighlight.gd" type="Script" id=8] [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/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/Yeah.png" type="Texture" id=11]
[ext_resource path="res://assets/shovel_button.svg" type="Texture" id=12] [ext_resource path="res://assets/shovel_button.svg" type="Texture" id=12]
[ext_resource path="res://scenes/SoundEffectsPlayer.gd" type="Script" id=13] [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/arrowback.svg" type="Texture" id=17]
[ext_resource path="res://assets/arrowrepeat.svg" type="Texture" id=18] [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://scenes/Water.tscn" type="PackedScene" id=19]
[ext_resource path="res://MainMenuTheme.tres" type="Theme" id=20]
[sub_resource type="DynamicFont" id=1] [sub_resource type="DynamicFont" id=1]
size = 27 size = 27
@ -191,7 +190,7 @@ script = ExtResource( 8 )
[node name="UI" type="CanvasLayer" parent="World"] [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_left = 10.0
margin_top = 10.0 margin_top = 10.0
margin_right = -10.0 margin_right = -10.0
@ -201,13 +200,13 @@ __meta__ = {
"_edit_use_anchors_": false "_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_right = 268.0
margin_bottom = 262.0 margin_bottom = 262.0
icon = ExtResource( 17 ) icon = ExtResource( 17 )
flat = true 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_left = 272.0
margin_right = 540.0 margin_right = 540.0
margin_bottom = 262.0 margin_bottom = 262.0
@ -229,7 +228,7 @@ __meta__ = {
[node name="Timer" type="Timer" parent="World/UI/IslandMap"] [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_left = 1.0
anchor_right = 1.0 anchor_right = 1.0
anchor_bottom = 1.0 anchor_bottom = 1.0
@ -241,14 +240,14 @@ __meta__ = {
"_edit_use_anchors_": false "_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_right = 291.0
margin_bottom = 262.0 margin_bottom = 262.0
rect_min_size = Vector2( 100, 100 ) rect_min_size = Vector2( 100, 100 )
icon = ExtResource( 15 ) icon = ExtResource( 15 )
flat = true 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_top = 283.0
margin_right = 291.0 margin_right = 291.0
margin_bottom = 545.0 margin_bottom = 545.0
@ -256,7 +255,7 @@ rect_min_size = Vector2( 100, 100 )
icon = ExtResource( 16 ) icon = ExtResource( 16 )
flat = true 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_top = 566.0
margin_right = 291.0 margin_right = 291.0
margin_bottom = 828.0 margin_bottom = 828.0
@ -298,7 +297,6 @@ __meta__ = {
} }
[node name="DebugContainer" type="HBoxContainer" parent="World/UI"] [node name="DebugContainer" type="HBoxContainer" parent="World/UI"]
visible = false
anchor_top = 1.0 anchor_top = 1.0
anchor_right = 1.0 anchor_right = 1.0
anchor_bottom = 1.0 anchor_bottom = 1.0
@ -440,22 +438,35 @@ text = "0"
align = 2 align = 2
script = ExtResource( 4 ) 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="DigSites" type="Node2D" parent="World"]
[node name="PlayerChar" type="Node2D" parent="World"] [node name="PlayerChar" parent="World" instance=ExtResource( 3 )]
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="Camera" type="Camera2D" parent="World"] [node name="Camera" type="Camera2D" parent="World"]
current = true 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="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="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="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/GameButtons/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/RepeatButton" to="World" method="_on_GenerateButton_pressed"]
[connection signal="visibility_changed" from="World/UI/IslandMap" to="World" method="_on_IslandMap_visibility_changed"] [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/ActionButtons/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/ActionButtons/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/DigButton" to="World" method="_on_DigButton_pressed"]
[connection signal="toggled" from="World/UI/DebugContainer/EditIslandButton" to="Editor" method="_on_EditIslandButton_toggled"] [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"]

32
scenes/Player.tscn Normal file
View File

@ -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"]

View File

@ -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!")

View File

@ -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"]

View File

@ -24,9 +24,11 @@ onready var FPSValueLabel = get_node("UI/DebugContainer/FPSValue")
onready var IslandMap = get_node("UI/IslandMap") onready var IslandMap = get_node("UI/IslandMap")
onready var IslandMapRenderer = get_node("../IslandMapRenderer") onready var IslandMapRenderer = get_node("../IslandMapRenderer")
onready var MapButton = get_node("UI/VBoxContainer/MapButton") onready var MapButton = get_node("UI/ActionButtons/MapButton")
onready var BirdyButton = get_node("UI/VBoxContainer/BirdyButton") onready var BirdyButton = get_node("UI/ActionButtons/BirdyButton")
onready var DigButton = get_node("UI/VBoxContainer/DigButton") 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 BirdyTimer = get_node("Birdy/Timer")
onready var MapTimer = get_node("UI/IslandMap/Timer") onready var MapTimer = get_node("UI/IslandMap/Timer")
@ -286,12 +288,16 @@ func reset():
clear_digsites() clear_digsites()
SuccessMessage.visible = false SuccessMessage.visible = false
CoinScoreLabel.text = "0"
func generate(): func generate():
reset() reset()
var rng = RandomNumberGenerator.new() var rng = RandomNumberGenerator.new()
if Globals.game_fixed_seed != 0:
rng.set_seed(0)
rng.randomize() rng.randomize()
randomize() randomize()
@ -684,6 +690,12 @@ func _on_GenerateButton_pressed():
RepeatButton.disabled = true RepeatButton.disabled = true
generate() generate()
func _on_BackButton_pressed(): func _on_BackButton_pressed():
get_tree().change_scene("res://Menu.tscn") get_tree().change_scene("res://Menu.tscn")
func _on_PlayerChar_coin_collected():
CoinScoreLabel.text = str(int (CoinScoreLabel.text) + 1)

View File

@ -9,9 +9,14 @@ enum State {
} }
signal dig_stopped signal dig_stopped
signal coin_collected
onready var Shovel = get_node("Shovel") onready var Shovel = get_node("Shovel")
onready var DigTimer = get_node("DigTimer") 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 velocity = Vector2()
var target = Vector2() var target = Vector2()
@ -49,6 +54,7 @@ func _physics_process(delta):
if state == State.Digging: if state == State.Digging:
velocity = Vector2.ZERO velocity = Vector2.ZERO
position = position + velocity * delta position = position + velocity * delta
var tile = Globals.WorldToHex(position) var tile = Globals.WorldToHex(position)
@ -81,3 +87,10 @@ func on_dig_start():
func on_dig_stop(): func on_dig_stop():
state = State.Walking state = State.Walking
emit_signal ("dig_stopped") emit_signal ("dig_stopped")
func _on_Collider_body_entered(body):
if body is Coin:
emit_signal ("coin_collected")
body.queue_free()