diff --git a/Library/Singletons/Generations.cs b/Library/Singletons/Generations.cs new file mode 100644 index 0000000..3c682ed --- /dev/null +++ b/Library/Singletons/Generations.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; + +namespace PokePurple.Library.Singletons; + +public class Generations +{ + private List _generations = + [ + new Range(1,151), + new Range(152, 251), + new Range(252, 386), + new Range(387, 493), + new Range(494, 649), + new Range(650, 721), + new Range(722, 809), + new Range(810, 905), + new Range(906, 1025) + ]; + + public int Generation = 1; + + public int PickRandomPokemon() + { + var maxRange = _generations[Generation].End.Value; + var random = new Random(); + var id = random.Next(1, maxRange); + return id; + } +} \ No newline at end of file diff --git a/Library/Singletons/Generations.cs.uid b/Library/Singletons/Generations.cs.uid new file mode 100644 index 0000000..a4d73c0 --- /dev/null +++ b/Library/Singletons/Generations.cs.uid @@ -0,0 +1 @@ +uid://dbfww8rxe1kj2 diff --git a/Library/Singletons/GifManager.cs b/Library/Singletons/GifManager.cs index ca5d889..04f89b6 100644 --- a/Library/Singletons/GifManager.cs +++ b/Library/Singletons/GifManager.cs @@ -7,11 +7,12 @@ public class GifManager private static GifManager? _instance; public static GifManager Instance => _instance ?? (_instance = new GifManager()); - private Node _gifManager; + private GodotObject _gifManager; public GifManager() { - _gifManager = ((SceneTree)Engine.GetMainLoop()).GetRoot().GetNode("GifManager"); + _gifManager = Engine.GetSingleton("GifManager"); + //_gifManager = ((SceneTree)Engine.GetMainLoop()).GetRoot().GetNode("GifManager"); } public AnimatedTexture AnimatedTextureFromBuffer(byte[] data, int max_frames = 0) => _gifManager.Call("animated_texture_from_buffer", data, max_frames).As(); diff --git a/Scenes/WildPokemon.cs b/Scenes/WildPokemon.cs new file mode 100644 index 0000000..692e4ca --- /dev/null +++ b/Scenes/WildPokemon.cs @@ -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(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(); + } +} diff --git a/Scenes/WildPokemon.cs.uid b/Scenes/WildPokemon.cs.uid new file mode 100644 index 0000000..1db12bd --- /dev/null +++ b/Scenes/WildPokemon.cs.uid @@ -0,0 +1 @@ +uid://cfn824p8fcutm diff --git a/Scenes/WildPokemon.tscn b/Scenes/WildPokemon.tscn new file mode 100644 index 0000000..f0d44f9 --- /dev/null +++ b/Scenes/WildPokemon.tscn @@ -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) diff --git a/project.godot b/project.godot index 6a3096d..d7db4f8 100644 --- a/project.godot +++ b/project.godot @@ -27,6 +27,14 @@ project/assembly_name="PokePurple" enabled=PackedStringArray("res://addons/twitcher/plugin.cfg") +[input] + +debug_pokemon={ +"deadzone": 0.2, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":80,"key_label":0,"unicode":112,"location":0,"echo":false,"script":null) +] +} + [twitcher] editor/game_oauth_token="res://addons/twitcher/default_oauth_token.tres"