Refactor in support for branching evolutions

This commit is contained in:
zontreck 2025-03-23 18:14:32 -07:00
parent 8633d8952e
commit e0e926562b
6 changed files with 176 additions and 76 deletions

BIN
assets/sprites/flareon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

BIN
assets/sprites/jolteon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

View file

@ -1,7 +1,7 @@
import 'dart:io'; import 'dart:io';
class Constants { class Constants {
static const VERSION = "1.0.032325+1552"; static const VERSION = "1.0.032325+1814";
static bool get isMobile => Platform.isAndroid || Platform.isIOS; static bool get isMobile => Platform.isAndroid || Platform.isIOS;
} }

View file

@ -5,6 +5,8 @@ import 'package:pokedex/Session.dart';
import 'package:pokedex/pokemon.dart'; import 'package:pokedex/pokemon.dart';
class MainApp extends StatefulWidget { class MainApp extends StatefulWidget {
const MainApp({super.key});
@override @override
_MainAppState createState() => _MainAppState(); _MainAppState createState() => _MainAppState();
} }
@ -32,7 +34,7 @@ class _MainAppState extends State<MainApp> {
class Home extends StatefulWidget { class Home extends StatefulWidget {
final VoidCallback toggleTheme; final VoidCallback toggleTheme;
Home({required this.toggleTheme}); const Home({super.key, required this.toggleTheme});
@override @override
_HomeState createState() => _HomeState(); _HomeState createState() => _HomeState();
@ -128,6 +130,8 @@ class _HomeState extends State<Home> {
} }
class DexEntry extends StatefulWidget { class DexEntry extends StatefulWidget {
const DexEntry({super.key});
@override @override
_DexEntryState createState() => _DexEntryState(); _DexEntryState createState() => _DexEntryState();
} }
@ -184,11 +188,18 @@ class _DexEntryState extends State<DexEntry> {
SizedBox(height: 32), SizedBox(height: 32),
if (_pkmn.hasEvolutions) if (_pkmn.hasEvolutions)
if (Constants.isMobile) if (Constants.isMobile)
Column( SingleChildScrollView(
crossAxisAlignment: CrossAxisAlignment.start, scrollDirection: Axis.horizontal,
children: _pkmn.getEvolutions(0), child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: _pkmn.getEvolutions(0),
),
), ),
if (!Constants.isMobile) Row(children: _pkmn.getEvolutions(0)), if (!Constants.isMobile)
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(children: _pkmn.getEvolutions(0)),
),
], ],
), ),
), ),

View file

@ -1,5 +1,4 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:pokedex/Consts.dart'; import 'package:pokedex/Consts.dart';
enum Generation { enum Generation {
@ -84,6 +83,8 @@ enum EvolutionCondition {
abstract class Evolution { abstract class Evolution {
const Evolution(); const Evolution();
String printEvolution(); String printEvolution();
Widget getEvolution();
} }
class SingleEvolution extends Evolution { class SingleEvolution extends Evolution {
@ -91,7 +92,7 @@ class SingleEvolution extends Evolution {
final int level; final int level;
final List<EvolutionCondition>? condition; final List<EvolutionCondition>? condition;
const SingleEvolution(this.to, this.level, {this.condition = null}); const SingleEvolution(this.to, this.level, {this.condition});
@override @override
String printEvolution() { String printEvolution() {
@ -110,12 +111,47 @@ class SingleEvolution extends Evolution {
} }
return sRet; return sRet;
} }
@override
Widget getEvolution() {
Pokemon pkmn = Pokemon.values.where((x) => x.id == to).first;
var arrow = Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Icon(
Constants.isMobile ? Icons.arrow_downward : Icons.arrow_forward,
size: 48,
),
Text(printEvolution(), style: TextStyle(fontSize: 24)),
],
);
Column col = Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Image.asset(
pkmn.toDexPath(),
width: Constants.isMobile ? 64 : 128,
height: Constants.isMobile ? 64 : 128,
),
pkmn.getTypeWidgets(),
],
);
Column rw = Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [arrow, col],
);
return rw;
}
} }
class BranchedEvolution extends Evolution { class BranchedEvolution extends Evolution {
final List<int> alternates; final List<int> alternates;
final List<int> levels; final List<int> levels;
final Map<int, List<EvolutionCondition>?> conditions; final List<List<EvolutionCondition>> conditions;
const BranchedEvolution(this.alternates, this.levels, this.conditions); const BranchedEvolution(this.alternates, this.levels, this.conditions);
@ -123,6 +159,78 @@ class BranchedEvolution extends Evolution {
String printEvolution() { String printEvolution() {
return ""; return "";
} }
String _prntEV(List<EvolutionCondition> conds, int level) {
String sRet = "";
if (level != -1) {
sRet += "Level ${level}";
}
for (var condition in conds) {
sRet += condition.name;
}
return sRet;
}
@override
Widget getEvolution() {
// This is a branched evolution. So first, we print the arrows, and conditions.
Row arrowRow;
List<Widget> arrows = [];
List<Widget> pkmnRow = [];
Row rowPkmn;
for (var pkmn in alternates) {
int index = alternates.indexOf(pkmn);
Pokemon _pkmn = Pokemon.values.where((x) => x.id == pkmn).first;
arrows.add(
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Icon(
Constants.isMobile ? Icons.arrow_downward : Icons.arrow_forward,
size: 48,
),
Text(
_prntEV(conditions[index], levels[index]),
style: TextStyle(fontSize: 24),
),
//Text(printEvolution(), style: TextStyle(fontSize: 24)),
],
),
);
pkmnRow.add(
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Image.asset(
_pkmn.toDexPath(),
width: Constants.isMobile ? 64 : 128,
height: Constants.isMobile ? 64 : 128,
),
_pkmn.getTypeWidgets(),
],
),
);
}
arrowRow = Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: arrows,
);
rowPkmn = Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: pkmnRow,
);
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [arrowRow, rowPkmn],
);
}
} }
enum LearnType { TM, HM } enum LearnType { TM, HM }
@ -487,11 +595,15 @@ enum Pokemon {
"Eevee", "Eevee",
Generation.One, Generation.One,
[Type.Normal], [Type.Normal],
BranchedEvolution([134], [-1], { BranchedEvolution([134, 135, 136], [-1, -1, -1], [
134: [EvolutionCondition.WaterStone], [EvolutionCondition.WaterStone],
}), [EvolutionCondition.ThunderStone],
[EvolutionCondition.FireStone],
]),
), ),
Vaporeon(134, "Vaporeon", Generation.One, [Type.Water], null); Vaporeon(134, "Vaporeon", Generation.One, [Type.Water], null),
Jolteon(135, "Jolteon", Generation.One, [Type.Electric], null),
Flareon(136, "Flareon", Generation.One, [Type.Fire], null);
final int id; final int id;
final String properName; final String properName;
@ -539,29 +651,39 @@ enum Pokemon {
String printName({var proper = false}) { String printName({var proper = false}) {
String sRet; String sRet;
if (!proper) if (!proper) {
sRet = name.replaceAll("_", " "); sRet = name.replaceAll("_", " ");
else } else {
sRet = properName; sRet = properName;
}
if (sRet.endsWith("F")) sRet = sRet.substring(0, sRet.length - 1) + ""; if (sRet.endsWith("F")) sRet = "${sRet.substring(0, sRet.length - 1)}";
if (sRet.endsWith("M")) sRet = sRet.substring(0, sRet.length - 1) + ""; if (sRet.endsWith("M")) sRet = "${sRet.substring(0, sRet.length - 1)}";
return sRet; return sRet;
} }
List<Widget> getEvolutions(int subID) { List<Widget> getEvolutions(int subID) {
print("SUBID ${subID}");
if (!hasEvolutions) return []; if (!hasEvolutions) return [];
Pokemon ev = Pokemon.Abra; List<Pokemon> Evs = [];
if (evolution is SingleEvolution) print("Processing evolutions for ${properName}");
ev =
Pokemon.values if (evolution is SingleEvolution) {
.where((x) => x.id == (evolution! as SingleEvolution).to) print("Single Evolution identified");
.first; Evs.add(
else { Pokemon.values
.where((x) => x.id == (evolution! as SingleEvolution).to)
.first,
);
} else {
print("Branching Evolution identified");
// Handle branched evolutions. Refactor below to accomodate a list of pokemon. // Handle branched evolutions. Refactor below to accomodate a list of pokemon.
for (var ev in (evolution! as BranchedEvolution).alternates) {
Evs.add(Pokemon.values.where((x) => x.id == ev).first);
}
} }
List<Widget> sprites = []; List<Widget> sprites = [];
@ -569,64 +691,29 @@ enum Pokemon {
if (subID == 0) { if (subID == 0) {
sprites.add(Text("Evolutions: ", style: TextStyle(fontSize: 24))); sprites.add(Text("Evolutions: ", style: TextStyle(fontSize: 24)));
sprites.add( sprites.add(
Column( Image.asset(
crossAxisAlignment: CrossAxisAlignment.start, toDexPath(),
children: [ width: Constants.isMobile ? 64 : 128,
Image.asset( height: Constants.isMobile ? 64 : 128,
toDexPath(),
width: Constants.isMobile ? 64 : 128,
height: Constants.isMobile ? 64 : 128,
),
getTypeWidgets(),
],
),
);
sprites.add(
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Icon(
Constants.isMobile ? Icons.arrow_downward : Icons.arrow_forward,
size: 48,
),
Text(evolution!.printEvolution(), style: TextStyle(fontSize: 24)),
],
), ),
); );
sprites.add(getTypeWidgets());
sprites.add(evolution!.getEvolution());
print("Main page EV.");
} else {
sprites.add(evolution!.getEvolution());
}
List<Widget> afterEvs = [];
for (var ev in Evs) {
afterEvs.addAll(ev.getEvolutions(subID + 1));
print("Processing evolution: ${ev.properName}");
} }
sprites.add( sprites.add(
Column( Row(crossAxisAlignment: CrossAxisAlignment.start, children: afterEvs),
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Constants.isMobile
? Image.asset(ev.toDexPath(), width: 64, height: 64)
: Image.asset(ev.toDexPath(), width: 128, height: 128),
ev.getTypeWidgets(),
],
),
); );
if (ev.hasEvolutions) {
sprites.add(
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Icon(
Constants.isMobile ? Icons.arrow_downward : Icons.arrow_forward,
size: 48,
),
Text(
ev.evolution!.printEvolution(),
style: TextStyle(fontSize: 24),
),
],
),
);
//sprites.add(Icon(Icons.arrow_forward, size: 48));
sprites.addAll(ev.getEvolutions(subID + 1));
}
return sprites; return sprites;
} }
} }

View file

@ -16,7 +16,7 @@ publish_to: "none" # Remove this line if you wish to publish to pub.dev
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts # In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix. # of the product and file versions while build-number is used as the build suffix.
version: 1.0.032325+1552 version: 1.0.032325+1814
environment: environment:
sdk: ^3.7.0 sdk: ^3.7.0
@ -199,6 +199,8 @@ flutter:
- assets/sprites/ditto.png - assets/sprites/ditto.png
- assets/sprites/eevee.png - assets/sprites/eevee.png
- assets/sprites/vaporeon.png - assets/sprites/vaporeon.png
- assets/sprites/jolteon.png
- assets/sprites/flareon.png
# An image asset can refer to one or more resolution-specific "variants", see # An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/to/resolution-aware-images # https://flutter.dev/to/resolution-aware-images