diff --git a/Objects/Coin.gd b/Objects/Coin.gd new file mode 100644 index 0000000..15e7c8a --- /dev/null +++ b/Objects/Coin.gd @@ -0,0 +1,8 @@ +extends Area2D + +func _on_Coin_body_entered(body): + print (body.name) + if body.name == "Rocket": + var numOfCoins = get_parent().get("numOfCoins") + numOfCoins += 1 + queue_free() diff --git a/Objects/Coin.tscn b/Objects/Coin.tscn new file mode 100644 index 0000000..04f05bc --- /dev/null +++ b/Objects/Coin.tscn @@ -0,0 +1,54 @@ +[gd_scene load_steps=10 format=2] + +[ext_resource path="res://Images and Anims/In Game/coin.png" type="Texture" id=1] +[ext_resource path="res://Objects/Coin.gd" type="Script" id=2] + +[sub_resource type="CircleShape2D" id=1] +radius = 22.2384 + +[sub_resource type="AtlasTexture" id=2] +flags = 4 +atlas = ExtResource( 1 ) +region = Rect2( 0, 0, 16, 16 ) + +[sub_resource type="AtlasTexture" id=3] +flags = 4 +atlas = ExtResource( 1 ) +region = Rect2( 16, 0, 16, 16 ) + +[sub_resource type="AtlasTexture" id=4] +flags = 4 +atlas = ExtResource( 1 ) +region = Rect2( 32, 0, 16, 16 ) + +[sub_resource type="AtlasTexture" id=5] +flags = 4 +atlas = ExtResource( 1 ) +region = Rect2( 48, 0, 16, 16 ) + +[sub_resource type="AtlasTexture" id=6] +flags = 4 +atlas = ExtResource( 1 ) +region = Rect2( 64, 0, 16, 16 ) + +[sub_resource type="SpriteFrames" id=7] +animations = [ { +"frames": [ SubResource( 2 ), SubResource( 3 ), SubResource( 4 ), SubResource( 5 ), SubResource( 6 ) ], +"loop": true, +"name": "Spin", +"speed": 10.0 +} ] + +[node name="Coin" type="Area2D"] +script = ExtResource( 2 ) + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource( 1 ) + +[node name="AnimatedSprite" type="AnimatedSprite" parent="."] +scale = Vector2( 2, 2 ) +frames = SubResource( 7 ) +animation = "Spin" +frame = 2 +playing = true +[connection signal="body_entered" from="." to="." method="_on_Coin_body_entered"] diff --git a/Scripts/Game.gd b/Scripts/Game.gd new file mode 100644 index 0000000..174167f --- /dev/null +++ b/Scripts/Game.gd @@ -0,0 +1,56 @@ +extends Node2D + +var time = 0 +var asteroidObj = load("res://Objects/Asteroid.tscn") +var coinObj = load("res://Objects/Coin.tscn") +var spawnTime +var deltaSpawnTime = 1 +var asteroidSpawnRepeat = 0 +var hiscore = 0 +var numOfCoins = 0 + +func load_game(): + var save_game = File.new() + if not save_game.file_exists("user://savegame.save"): + return + save_game.open("user://savegame.save", File.READ) + while save_game.get_position() < save_game.get_len(): + var data = parse_json(save_game.get_line()) + var hiscore = data["hiscore"] + var numOfCoins = data["numOfCoins"] + +func _process(delta): + spawnTime = 500/get_viewport_rect().size.x * deltaSpawnTime + time += delta + #print (numOfCoins) + if time > spawnTime: + asteroidSpawnRepeat += 1 + time = 0 + if spawnTime > 300/get_viewport_rect().size.x: + deltaSpawnTime *= 0.995 + if has_node("Rocket"): + var asteroid = asteroidObj.instance() + add_child(asteroid) + asteroid.position.x = rand_range($Rocket.position.x - get_viewport_rect().size.x/2, $Rocket.position.x + get_viewport_rect().size.x/2) + asteroid.position.y = rand_range($Rocket.position.y - get_viewport_rect().size.y, $Rocket.position.y - 2 * get_viewport_rect().size.y) + asteroid.rotation = rand_range(0, 6.28) + if asteroidSpawnRepeat == 5: + asteroidSpawnRepeat = 0 + if has_node("Rocket"): + var coin = coinObj.instance() + add_child(coin) + coin.position.x = rand_range($Rocket.position.x - get_viewport_rect().size.x/2, $Rocket.position.x + get_viewport_rect().size.x/2) + coin.position.y = rand_range($Rocket.position.y - get_viewport_rect().size.y, $Rocket.position.y - 2 * get_viewport_rect().size.y) + +func save(): + var save_dict = { + "hiscore" : hiscore, + "numOfCoins" : numOfCoins + } + return save_dict + +func save_data(): + var save_game = File.new() + save_game.open("user://savegame.save", File.WRITE) + var data = call("save") + save_game.store_line(to_json(data))