Added WildPokemon Scene

Started work on Wild Poke`mon Scene for capturing wild pokemon.
This commit is contained in:
Mario Steele 2025-06-13 17:11:08 -05:00
parent 538f3b68df
commit af766236c1
3 changed files with 60 additions and 0 deletions

49
Scenes/WildPokemon.cs Normal file
View file

@ -0,0 +1,49 @@
using Godot;
using System;
using System.ComponentModel;
using System.Text;
using Godot.Sharp.Extras;
using PokeApiNet;
using PokePurple.Library.Singletons;
public partial class WildPokemon : Node2D
{
private Generations _generations = new();
private Pokemon _pokemon;
private PokeApiClient _apiClient;
private System.Net.Http.HttpClient _http;
[NodePath] private AnimatedSprite2D _pokemonSprite;
private String _animatedUrl =
"https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/other/showdown/{0}.gif";
public override void _Ready()
{
this.OnReady();
_apiClient = new();
_http = new();
_generations.Generation = 8;
InitPokemon();
}
public override void _Input(InputEvent @event)
{
if (Input.IsActionJustPressed("debug_pokemon"))
{
InitPokemon();
}
}
private async void InitPokemon()
{
var pokeId = _generations.PickRandomPokemon();
_pokemon = await _apiClient.GetResourceAsync<Pokemon>(pokeId);
GD.Print(_pokemon.Name);
var url = String.Format(_animatedUrl, pokeId);
GD.Print(url);
var data = await _http.GetByteArrayAsync(url);
var frames = GifManager.Instance.SpriteFramesFromBuffer(data);
_pokemonSprite.SpriteFrames = frames;
_pokemonSprite.Play();
}
}

View file

@ -0,0 +1 @@
uid://cfn824p8fcutm

10
Scenes/WildPokemon.tscn Normal file
View file

@ -0,0 +1,10 @@
[gd_scene load_steps=2 format=3 uid="uid://k73x3ho8t1x1"]
[ext_resource type="Script" uid="uid://cfn824p8fcutm" path="res://Scenes/WildPokemon.cs" id="1_3f6p2"]
[node name="WildPokemon" type="Node2D"]
script = ExtResource("1_3f6p2")
[node name="PokemonSprite" type="AnimatedSprite2D" parent="."]
position = Vector2(180, 180)
scale = Vector2(2, 2)