Compare commits
5 commits
09c99b6cf5
...
29420f40f8
| Author | SHA1 | Date | |
|---|---|---|---|
| 29420f40f8 | |||
| af766236c1 | |||
| 538f3b68df | |||
| 93f5b3dec9 | |||
| 8f1d66da1b |
7 changed files with 102 additions and 2 deletions
30
Library/Singletons/Generations.cs
Normal file
30
Library/Singletons/Generations.cs
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace PokePurple.Library.Singletons;
|
||||
|
||||
public class Generations
|
||||
{
|
||||
private List<Range> _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;
|
||||
}
|
||||
}
|
||||
1
Library/Singletons/Generations.cs.uid
Normal file
1
Library/Singletons/Generations.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://dbfww8rxe1kj2
|
||||
|
|
@ -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<AnimatedTexture>();
|
||||
|
|
|
|||
49
Scenes/WildPokemon.cs
Normal file
49
Scenes/WildPokemon.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
1
Scenes/WildPokemon.cs.uid
Normal file
1
Scenes/WildPokemon.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://cfn824p8fcutm
|
||||
10
Scenes/WildPokemon.tscn
Normal file
10
Scenes/WildPokemon.tscn
Normal 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)
|
||||
|
|
@ -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"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue