Skip to content

Instantly share code, notes, and snippets.

@owenselles
Created January 6, 2019 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save owenselles/a3deb33d4c68617b43f830aa27a65d83 to your computer and use it in GitHub Desktop.
Save owenselles/a3deb33d4c68617b43f830aa27a65d83 to your computer and use it in GitHub Desktop.
package nl.delphinity.pokemon.model.trainer;
import nl.delphinity.pokemon.model.area.Area;
import nl.delphinity.pokemon.model.area.Pokecenter;
import nl.delphinity.pokemon.model.battle.Battle;
import nl.delphinity.pokemon.model.general.Pokemon;
import nl.delphinity.pokemon.model.general.PokemonData;
import nl.delphinity.pokemon.model.general.PokemonType;
import nl.delphinity.pokemon.model.item.Inventory;
import nl.delphinity.pokemon.model.item.ItemType;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
public class Trainer {
private final String name;
private final ArrayList<Pokemon> pokemonCollection = new ArrayList<>();
private final Inventory inventory = new Inventory();
private final Random r = new Random();
private final List<Badge> badges = new ArrayList<>();
private Pokemon activePokemon;
private Area currentArea;
public Trainer(String name, Area startingArea) {
this.name = name;
this.inventory.addItem(5, ItemType.POKEBALL);
this.currentArea = startingArea;
}
public Inventory getInventory() {
return inventory;
}
public Pokemon getActivePokemon() {
return activePokemon;
}
public void setActivePokemon(Pokemon activePokemon) {
this.activePokemon = activePokemon;
}
public String getName() {
return name;
}
public ArrayList<Pokemon> getPokemonCollection() {
return pokemonCollection;
}
public List<Badge> getBadges() {
return badges;
}
public Area getCurrentArea() {
return currentArea;
}
private void setCurrentArea(Area currentArea) {
this.currentArea = currentArea;
}
//TODO: US-PKM-O-5:
public Battle battle(Pokemon myPokemon, Pokemon randomPokemon) {
if (myPokemon.getOwner() != null && myPokemon.getOwner().equals(this)) {
Battle battle = new Battle(myPokemon, randomPokemon, Trainer.this);
return battle;
}
return null;
}
//TODO: US-PKM-O-7
private boolean catchPokemon(Pokemon pokemon) {
if (pokemon.getOwner() != null) {
return false;
}
int catchChance = r.nextInt(100);
if (catchChance > 50) {
pokemonCollection.add(pokemon);
pokemon.setOwner(this);
if (activePokemon == null) {
setActivePokemon(pokemon);
}
return true;
}
return false;
}
public List<Pokemon> getPokemonByType(PokemonType pokemonType) {
return pokemonCollection.stream().filter(p -> p.getPokedata().pokemonType.equals(pokemonType)).
collect(Collectors.toList());
}
public void useItem(ItemType item, Battle battle) {
if (battle == null) {
System.out.println("Used: " + item.name());
return;
}
switch (item) {
case POKEBALL:
if (this.catchPokemon(battle.getEnemy())) {
battle.setBattleComplete(true);
battle.setWinner(battle.getMyPokemon());
}
this.inventory.removeItem(ItemType.POKEBALL);
break;
default:
break;
}
}
//TODO: US-PKM-O-8
public Battle challengeTrainer(Trainer opponent) {
Pokemon myPokemon = getActivePokemon();
Pokemon otherpokemon = opponent.activePokemon;
Battle battle = battle(myPokemon, otherpokemon);
battle.start();
return battle;
}
//TODO: US-PKM-O-11 dis
public void travel(Area area) {
this.setCurrentArea(area);
}
//TODO: US-PKM-O-3
public void showPokemonColletion() {
//System.out.println(getPokemonCollection());
for (Pokemon temp : pokemonCollection) {
//System.out.println(temp.getPokedata());
temp.status();
}
}
//TODO: US-PKM-O-6
public Pokemon findPokemon() {
while (true) {
int findChance = r.nextInt(100);
if (findChance > 80) {
int level = activePokemon.getLevel();
Pokemon foundPokemon = currentArea.getRandomPokemonFromArea(level);
return foundPokemon;
} else {
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
}
//TODO: US-PKM-O-10
public void showBadges() {
//System.out.println("test show badge");
for (Badge b : badges) {
System.out.println(b.getName());
}
}
//TODO: US-PKM-O-9
public void addBadge(Badge newBadge) {
badges.add(newBadge);
}
//TODO: US-PKM-O-5:
public boolean canBattle() {
boolean canBattle = false;
for (int i = pokemonCollection.size(); i > 0; i--) {
if (activePokemon.isKnockout()) {
canBattle = false;
} else {
canBattle = true;
}
}
return canBattle;
}
//TODO: US-PKM-O-12
public void visitPokeCenter(Pokecenter pokecenter) {
ArrayList<Pokemon> pokemonToHeal = new ArrayList<>();
if (pokecenter != null) {
pokecenter.healPokemon(pokemonToHeal);
for (Pokemon p: pokemonToHeal)
{
p.setCurrentHp(p.getMaxHp());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment