-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.gd
115 lines (96 loc) · 3.1 KB
/
main.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
extends Node2D
@export var DEBUG_MODE:bool = false
const savefileLocation = "user://fishgamesave.json"
const secondsBetweenAutosave = 10
var lastSave = Time.get_ticks_msec()
var rng = RandomNumberGenerator.new()
@onready var moneyCounter = get_node("UI/Money")
@onready var fishContainer = get_node("/root/Node2D/Fish")
@export var tankSize:int :
set(new_value):
tankSize = new_value
saveGame()
get: return tankSize
@export var money:int :
set(new_value):
money = new_value
moneyCounter.text = str(money)
saveGame()
get: return money
@export var fish = []
func _process(delta):
if (Time.get_ticks_msec() - lastSave > secondsBetweenAutosave*1000):
print("autosaving")
saveGame()
func addFish(spawnedFish):
var newFishData = {}
newFishData.type = spawnedFish.scene_file_path.get_file().get_basename()
newFishData.hunger = spawnedFish.hunger
newFishData.id = rng.randi()
spawnedFish.id = newFishData.id
fish.append(newFishData)
saveGame()
func remove_fish(fishNode):
var fish_data = fish.filter(func(f): return f.id == fishNode.id)
if fish_data.size() == 0:
push_error("tried to remove fish, but couldn't find the in the fish array", fishNode)
print("okay im ghonna remove this fish with his data:", fish_data[0])
var fish_index = fish.find(fish_data[0])
print("removing fish at ",fish_index)
fish.remove_at(fish_index)
fishNode.queue_free()
saveGame()
func spawnFish(fishData):
var fishScene = load("res://fish/"+fishData.type+".tscn")
var newFish = fishScene.instantiate()
# Update all variables that are stored in fishdata
newFish.id = fishData.id
newFish.hunger = fishData.hunger
if (fishData.birth): newFish.birth = fishData.birth
if (fishData.xp): newFish.xp = fishData.xp
if (fishData.level): newFish.level = fishData.level
if (fishData.has("petName")): newFish.petName = fishData.petName
# postion in tank
newFish.position.x = rng.randi_range(20, 380)
newFish.position.y = rng.randi_range(20, 250)
fishContainer.add_child(newFish)
fish.append(fishData)
var readyToSave = false
func _ready():
if FileAccess.file_exists(savefileLocation):
var gameData = FileAccess.open(savefileLocation, FileAccess.READ)
var parsed = JSON.parse_string(gameData.get_as_text())
money = parsed.money
tankSize = parsed.tankSize
for fish in parsed.fish: spawnFish(fish)
print("loaded save data", parsed)
readyToSave = true
else:
tankSize = 1
money = 50
fish = []
print("initialized save data")
readyToSave = true
saveGame()
func saveGame():
if (!readyToSave): return
var file = FileAccess.open(savefileLocation, FileAccess.WRITE)
var data = {}
data.money = money
data.tankSize = tankSize
for f in fish: updateFishData(f)
data.fish = fish
file.store_string(JSON.stringify(data))
lastSave = Time.get_ticks_msec()
print("saved save data")
func updateFishData(fishy):
var spawnedFish = fishContainer.get_children()
for f in spawnedFish:
if fishy.id == f.id:
fishy.hunger = f.hunger
fishy.level = f.level
fishy.xp = f.xp
fishy.birth = f.birth
fishy.petName = f.petName
return
print("failed to find matching fish when saving fish data. id:",fishy.id)