11extends Node2D
22
3- @onready var labelnode = $ CanvasLayer/Label
43@onready var catnode = $ Cat
54
65@onready var hunger_bar_node = $ Hunger
@@ -10,6 +9,21 @@ extends Node2D
109
1110@onready var cat_node = $ Cat
1211
12+ @onready var min_idle_range = $ MinIdleRange .position
13+ @onready var max_idle_range = $ MaxIdleRange .position
14+ @onready var cat_idle_walk_node = $ CatIdleWalk
15+
16+ @onready var meow_node = $ Meow
17+ @onready var meow_timer_node = $ MeowTimer
18+
19+ @onready var object_use_timer_node = $ ObjectUseTimer
20+
21+ var target = null # used for State.WALKING and State.USING
22+ var current_state = State .IDLE
23+
24+ var idle_goal = Vector2 .ZERO
25+ @export var speed = 1.5 # Pixels
26+
1327signal cat_obliteration # emmited when game lost
1428
1529enum StatType {
@@ -31,17 +45,40 @@ var stats = {
3145 StatType .CLEANLINESS : 64 ,
3246}
3347
48+ @onready var stat_nodes = {
49+ StatType .HUNGER : $ Food ,
50+ StatType .THIRST : $ Water ,
51+ StatType .FUN : null ,
52+ StatType .HUMAN_TOLERANCE : null ,
53+ StatType .AWAKENESS : null ,
54+ StatType .CLEANLINESS : null ,
55+ }
56+
57+ var stat_use_times = {
58+ StatType .HUNGER : 1.5 ,
59+ StatType .THIRST : 1 ,
60+ StatType .FUN : 1 ,
61+ StatType .HUMAN_TOLERANCE : 1 ,
62+ StatType .AWAKENESS : 1 ,
63+ StatType .CLEANLINESS : 1 ,
64+ }
65+
3466enum State {
3567 IDLE , # Cat will stay still
3668 IDLE_MOVE , # Cat is moving to a random position
3769 WALKING , # Cat will move towards object
3870 USING , # Cat will be at object
3971}
4072
41- var target = null # used for State.WALKING and State.USING
42- var current_state = State .IDLE
4373
44- const labeltext = "Hunger: %s \n Thirst: %s \n Fun: %s \n Human Tolerance: %s \n Awakeness: %s \n Cleanliness: %s "
74+ func _on_cat_idle_walk_timeout ():
75+ idle_goal .x = randi_range (min_idle_range .x , max_idle_range .x )
76+ idle_goal .y = randi_range (min_idle_range .y , max_idle_range .y )
77+ change_state (State .IDLE_MOVE )
78+
79+ func change_state (state ):
80+ current_state = state
81+ $ Label .text = State .keys ()[state ]
4582
4683func _ready ():
4784 # set random initial seed for random functions
@@ -54,35 +91,66 @@ func _ready():
5491
5592 update_ui ()
5693
57- var idle_goal = Vector2 .ZERO
58- var speed = 1.5 # Pixels
94+
95+ func update_ui ():
96+ hunger_bar_node .value = stats [StatType .HUNGER ]
97+ thirst_bar_node .value = stats [StatType .THIRST ]
98+ fun_bar_node .value = stats [StatType .FUN ]
99+ human_tolerance_bar_node .value = stats [StatType .HUMAN_TOLERANCE ]
100+
101+ var lowest_object_stat
59102
60103func _process (delta ):
61104 match current_state :
62105 State .WALKING :
63106 if target :
64107 catnode .position = catnode .position .move_toward (target .position , speed * 16 * delta )
108+ if catnode .position == target .position :
109+ change_state (State .USING )
110+ object_use_timer_node .start ()
111+ else : change_state (State .IDLE_MOVE )
65112 State .IDLE_MOVE :
66113 catnode .position = catnode .position .move_toward (idle_goal , speed * 16 * delta )
67114 if catnode .position == idle_goal :
68115 change_state (State .IDLE )
69116 cat_idle_walk_node .start (randf_range (0.5 , 4 ))
70-
71-
72- func update_ui ():
73- labelnode .text = labeltext % [
74- stats [StatType .HUNGER ],
75- stats [StatType .THIRST ],
76- stats [StatType .FUN ],
77- stats [StatType .HUMAN_TOLERANCE ],
78- stats [StatType .AWAKENESS ],
79- stats [StatType .CLEANLINESS ]
80- ]
81117
82- hunger_bar_node .value = stats [StatType .HUNGER ]
83- thirst_bar_node .value = stats [StatType .THIRST ]
84- fun_bar_node .value = stats [StatType .FUN ]
85- human_tolerance_bar_node .value = stats [StatType .HUMAN_TOLERANCE ]
118+ if not State .USING :
119+ lowest_object_stat = get_lowest_object_stat ()
120+ var urgent = stats [lowest_object_stat ] <= 32
121+ if urgent :
122+ target = stat_nodes [lowest_object_stat ]
123+ change_state (State .WALKING )
124+
125+ func _on_object_use_timer_timeout ():
126+ _on_cat_idle_walk_timeout ()
127+ match lowest_object_stat :
128+ StatType .HUNGER : food ()
129+ StatType .THIRST : water ()
130+
131+ func _on_meow_timer_timeout ():
132+ meow_node .play ()
133+ meow_timer_node .start (randf_range (3 ,9 ))
134+
135+ # returns the stat with the lowest number
136+ func get_lowest_stat ():
137+ var lowest_stat
138+ var lowest_stat_num = 64
139+ for stat in stats .keys ():
140+ if stats [stat ] < lowest_stat_num :
141+ lowest_stat = stat
142+ lowest_stat_num = stats [stat ]
143+ return lowest_stat
144+
145+ # returns the stat with the lowest number out of the stats that have an accompying object
146+ func get_lowest_object_stat ():
147+ var lowest_stat
148+ var lowest_stat_num = 65
149+ for stat in stats .keys ():
150+ if stats [stat ] < lowest_stat_num and stat_nodes [stat ]:
151+ lowest_stat = stat
152+ lowest_stat_num = stats [stat ]
153+ return lowest_stat
86154
87155# adds a number to the stat while still respecting the maximum and minimum values
88156func capadd (stat , add ): stats [stat ] = max (min (stats [stat ]+ add , 64 ),0 )
@@ -100,42 +168,27 @@ func _on_stat_tick_timeout():
100168 update_ui ()
101169
102170
103-
104-
105-
106- # WHAT HAPPENS WHEN OBJECT CLICKED
171+ # WHAT HAPPENS WHEN OBJECT USED
107172
108173func food ():
109- capadd (StatType .HUNGER , 8 )
110- capadd (StatType .THIRST , - 4 )
174+ capadd (StatType .HUNGER , 16 )
175+ capadd (StatType .THIRST , - 8 )
111176 update_ui ()
112177
113178func water ():
114- capadd (StatType .THIRST , 8 )
179+ capadd (StatType .THIRST , 16 )
115180 update_ui ()
116181
182+
117183# As chaotic as possible while still being fair if you know what you're doing
118184func pet ():
185+ meow_node .play ()
119186 if stats [StatType .HUMAN_TOLERANCE ] >= 32 :
120187 capadd (StatType .FUN , 8 )
121188 elif stats [StatType .HUMAN_TOLERANCE ] < 48 :
122189 capadd (StatType .FUN , - 8 )
123190
124-
125191 var rand = 1 + randi () % 8
126192 if rand >= 7 : rand *= 3
127193 capadd (StatType .HUMAN_TOLERANCE , - rand )
128194 update_ui ()
129-
130- @onready var min_idle_range = $ MinIdleRange .position
131- @onready var max_idle_range = $ MaxIdleRange .position
132- @onready var cat_idle_walk_node = $ CatIdleWalk
133-
134- func _on_cat_idle_walk_timeout ():
135- idle_goal .x = randi_range (min_idle_range .x , max_idle_range .x )
136- idle_goal .y = randi_range (min_idle_range .y , max_idle_range .y )
137- change_state (State .IDLE_MOVE )
138-
139- func change_state (state ):
140- current_state = state
141- print ("STATE CHANGED TO:" + str (state ))
0 commit comments