-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeatherScene.gd
More file actions
201 lines (160 loc) · 5.69 KB
/
Copy pathWeatherScene.gd
File metadata and controls
201 lines (160 loc) · 5.69 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# WeatherScene.gd
# Self-contained weather display using Open-Meteo (no API key required)
# Shows temperature, sunny/cloudy icons, and rain icon for a US ZIP code
extends Node2D
export(String) var zip_code
onready var http_request = $HTTPRequest
# UI nodes
onready var temperature_label = $TemperatureLabel
onready var sun_icon = $SunIcon
onready var cloud_icon = $CloudIcon
onready var rain_icon = $RainIcon
# Tracking for chained requests
var current_request_type: String = "" # "geocode" or "weather"
var current_zip: String = ""
# Signals (optional)
signal weather_updated(zip, temp_f, is_raining, is_sunny, sky_condition)
signal weather_error(zip, message)
func _ready():
http_request.connect("request_completed", self, "_on_request_completed")
# Hide all icons initially
if sun_icon: sun_icon.visible = false
if cloud_icon: cloud_icon.visible = false
if rain_icon: rain_icon.visible = false
if temperature_label: temperature_label.text = "-- °F"
# Auto-fetch on scene load (change ZIP as desired)
_on_Timer_timeout()
# fetch_weather("27312") # Pittsboro, NC
# Call this function to fetch weather for any ZIP code
func fetch_weather(zip_code: String) -> void:
if zip_code.empty():
_handle_error(zip_code, "Zip code cannot be empty")
return
var clean_zip = zip_code.strip_edges()
current_zip = clean_zip
# Step 1: Geocode ZIP to lat/lon
var geocode_url = "https://geocoding-api.open-meteo.com/v1/search?name=" + clean_zip + "&count=1&format=json"
var headers = PoolStringArray([
"User-Agent: Godot Weather Scene"
])
var error = http_request.request(geocode_url, headers, true, HTTPClient.METHOD_GET)
if error != OK:
_handle_error(clean_zip, "Geocode request failed (err " + str(error) + ")")
return
current_request_type = "geocode"
print("Geocoding ZIP: ", clean_zip)
func _on_request_completed(result, response_code, headers, body):
if current_request_type.empty():
return
var zip = current_zip
if current_request_type == "geocode":
current_request_type = ""
if response_code != 200:
_handle_error(zip, "Geocode HTTP " + str(response_code))
return
var json_string = body.get_string_from_utf8()
var parse_result = JSON.parse(json_string)
if parse_result.error != OK:
_handle_error(zip, "Geocode JSON error: " + parse_result.error_string)
return
var data = parse_result.result
if not data.has("results") or data.results.empty():
_handle_error(zip, "ZIP not found")
return
var loc = data.results[0]
var lat = str(loc.latitude)
var lon = str(loc.longitude)
# Step 2: Fetch current weather
var weather_url = (
"https://api.open-meteo.com/v1/forecast?" +
"latitude=" + lat +
"&longitude=" + lon +
"¤t=temperature_2m,rain,precipitation,weather_code,cloud_cover" +
"&temperature_unit=fahrenheit" +
"&timezone=America/New_York"
)
var error = http_request.request(weather_url, PoolStringArray(["User-Agent: Godot Weather Scene"]), true, HTTPClient.METHOD_GET)
if error != OK:
_handle_error(zip, "Weather request failed (err " + str(error) + ")")
return
current_request_type = "weather"
print("Fetching weather for ", zip, " at ", lat, ",", lon)
elif current_request_type == "weather":
current_request_type = ""
current_zip = ""
if response_code != 200:
_handle_error(zip, "Weather HTTP " + str(response_code))
return
var json_string = body.get_string_from_utf8()
var parse_result = JSON.parse(json_string)
if parse_result.error != OK:
_handle_error(zip, "Weather JSON error: " + parse_result.error_string)
return
var data = parse_result.result
if not data.has("current"):
_handle_error(zip, "No current weather data")
return
var current = data.current
var temp_f: float = current.temperature_2m
var rain_mm: float = current.get("rain", 0.0)
var precip_mm: float = current.get("precipitation", 0.0)
var wmo_code: int = current.get("weather_code", 0)
var cloud_pct: int = current.get("cloud_cover", 0)
# Rain detection
var is_raining = (
rain_mm > 0.0 or
precip_mm > 0.0 and wmo_code in [51,53,55,56,57,61,63,65,66,67,80,81,82]
)
# Sky condition logic
var sky_condition: String = "Unknown"
var is_sunny: bool = false
# is_raining = true #FOR TESTING
# wmo_code = 1 #FOR TESTING
match wmo_code:
0:
sky_condition = "Sunny"
is_sunny = true
1:
sky_condition = "Mostly Sunny"
is_sunny = true
2:
sky_condition = "Partly Cloudy"
is_sunny = false
3:
sky_condition = "Cloudy"
is_sunny = false
# _:
# # Fallback for rain/fog/etc. using cloud cover
# if cloud_pct <= 20:
# sky_condition = "Sunny"
# is_sunny = true
# elif cloud_pct <= 50:
# sky_condition = "Partly Cloudy"
# is_sunny = false
# else:
# sky_condition = "Cloudy"
# is_sunny = false
# Update UI
if temperature_label:
temperature_label.text = "%.1f°F" % temp_f
# Icon visibility logic
$RainIcon.visible = is_raining
$SunIcon.visible = is_sunny
$CloudIcon.visible = not(is_sunny)
print("Weather for ", zip, ": ", temp_f, "°F | ", sky_condition,
" | Raining: ", is_raining,
" (code: ", wmo_code, ", clouds: ", cloud_pct, "%)")
emit_signal("weather_updated", zip, temp_f, is_raining, is_sunny, sky_condition)
func _handle_error(zip: String, msg: String):
print("Weather error for ", zip, ": ", msg)
if temperature_label:
temperature_label.text = "Error"
if sun_icon: sun_icon.visible = false
if cloud_icon: cloud_icon.visible = false
if rain_icon: rain_icon.visible = false
emit_signal("weather_error", zip, msg)
# Optional: call this from a button or timer to refresh
# func refresh_weather():
# fetch_weather("27312")
func _on_Timer_timeout():
fetch_weather(zip_code) # Pittsboro, NC