Added coin spawner.

master
Martin Felis 2022-01-23 11:47:18 +01:00
parent 1a820aa26a
commit 626238ddbe
6 changed files with 213 additions and 0 deletions

81
assets/coin.svg Normal file
View File

@ -0,0 +1,81 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="67.73333mm"
height="67.73333mm"
viewBox="0 0 67.73333 67.73333"
version="1.1"
id="svg1420"
inkscape:version="0.92.5 (2060ec1f9f, 2020-04-08)"
sodipodi:docname="coin.svg">
<defs
id="defs1414" />
<sodipodi:namedview
id="base"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageopacity="0.0"
inkscape:pageshadow="2"
inkscape:zoom="2.8"
inkscape:cx="164.5524"
inkscape:cy="128.13748"
inkscape:document-units="mm"
inkscape:current-layer="layer1"
showgrid="false"
inkscape:window-width="1916"
inkscape:window-height="1041"
inkscape:window-x="0"
inkscape:window-y="37"
inkscape:window-maximized="0"
fit-margin-top="0"
fit-margin-left="0"
fit-margin-right="0"
fit-margin-bottom="0"
showguides="false" />
<metadata
id="metadata1417">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1"
transform="translate(-22.735567,-21.843752)">
<rect
style="fill:none;fill-opacity:1;stroke:none;stroke-width:1.16499996;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
id="rect2086"
width="67.73333"
height="67.73333"
x="22.735567"
y="21.843752" />
<circle
style="fill:#ffcc00;fill-opacity:1;stroke:#d4aa00;stroke-width:0.0973914;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
id="path903"
cx="56.60223"
cy="55.710419"
r="1.9024498" />
<circle
style="fill:#aa8800;fill-opacity:1;stroke:#d4aa00;stroke-width:0.07251166;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
id="path905"
cx="56.60223"
cy="55.710419"
r="1.2034044" />
</g>
</svg>

After

Width:  |  Height:  |  Size: 2.6 KiB

35
assets/coin.svg.import Normal file
View File

@ -0,0 +1,35 @@
[remap]
importer="texture"
type="StreamTexture"
path="res://.import/coin.svg-4f408cc0c27738ba7d156f441f501d5e.stex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://assets/coin.svg"
dest_files=[ "res://.import/coin.svg-4f408cc0c27738ba7d156f441f501d5e.stex" ]
[params]
compress/mode=0
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/bptc_ldr=0
compress/normal_map=0
flags/repeat=0
flags/filter=true
flags/mipmaps=false
flags/anisotropic=false
flags/srgb=2
process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
process/normal_map_invert_y=false
stream=false
size_limit=0
detect_3d=true
svg/scale=1.0

41
scenes/Coin.gd Normal file
View File

@ -0,0 +1,41 @@
extends Node2D
onready var coin_sprite = get_node("CoinSprite")
var velocity = Vector2(0, 0)
var z_pos = 0
var z_vel = 0
var gravity = 1500
var bounciness = 0.5
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
func _draw():
coin_sprite.offset.y = -z_pos
draw_circle(Vector2.ZERO, 5, Color("88000000"))
pass
func _process(delta):
z_vel = z_vel - gravity * delta
z_pos = z_pos + z_vel * delta
if z_pos < 0:
z_vel = - z_vel * bounciness
z_pos = 0
if z_vel < 0.01:
z_vel = 0
velocity = velocity * 0.5
position = position + velocity * delta
update()
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass

10
scenes/Coin.tscn Normal file
View File

@ -0,0 +1,10 @@
[gd_scene load_steps=3 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"]
script = ExtResource( 2 )
[node name="CoinSprite" type="Sprite" parent="."]
texture = ExtResource( 1 )

40
scenes/CoinSpawner.gd Normal file
View File

@ -0,0 +1,40 @@
extends Node2D
onready var Coin = preload("./Coin.tscn")
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
var coin_spread = 50
# Called when the node enters the scene tree for the first time.
func _ready():
assert (Coin != null)
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass
func emit_coins (num, _position = null):
for i in range (num):
var coin = Coin.instance()
coin.position = position
if _position != null:
coin.position = _position
coin.velocity = Vector2 (rand_range(-coin_spread, coin_spread), rand_range (-coin_spread, coin_spread))
coin.z_pos = 0
coin.z_vel = rand_range(300, 800)
add_child(coin)
func _unhandled_input(event):
if 'position' in event:
var relative_pos = self.transform.affine_inverse() * event.position
if event is InputEventMouseButton:
if event.button_index == BUTTON_LEFT and event.pressed:
emit_coins(25, relative_pos)

6
scenes/CoinSpawner.tscn Normal file
View File

@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://scenes/CoinSpawner.gd" type="Script" id=1]
[node name="CoinSpawner" type="Node2D"]
script = ExtResource( 1 )