Created
March 20, 2019 01:31
-
-
Save Andrewcpu/8186ec274ed79f01aa93040e5b57e2a7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.Andrewcpu.morph.graphics; | |
import net.Andrewcpu.morph.graphics.render.Renderer; | |
import net.Andrewcpu.morph.logic.World; | |
import javax.swing.*; | |
import java.awt.*; | |
public class GPanel extends JComponent { | |
private World world; | |
private Renderer renderer; | |
public GPanel(World world){ | |
this.world = world; | |
this.renderer = new Renderer(world, 1000, 800); | |
} | |
public Renderer getRenderer() { | |
return renderer; | |
} | |
@Override | |
public void paint(Graphics g){ | |
renderer.render(g); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.Andrewcpu.morph.graphics.render; | |
import net.Andrewcpu.morph.logic.Entity; | |
import net.Andrewcpu.morph.logic.World; | |
import net.Andrewcpu.morph.logic.elements.interfaces.Drawable; | |
import java.awt.*; | |
public class Renderer { | |
private World world; | |
private int xOffset = 0, yOffset = 0; | |
private int width, height; | |
private Entity followingEntity = null; | |
public Renderer(World world, int width, int height) { | |
this.world = world; | |
this.width = width; | |
this.height = height; | |
} | |
public void setFollowingEntity(Entity entity){ | |
this.followingEntity = entity; | |
} | |
public int getWidth() { | |
return width; | |
} | |
public void setWidth(int width) { | |
this.width = width; | |
} | |
public int getHeight() { | |
return height; | |
} | |
public void setHeight(int height) { | |
this.height = height; | |
} | |
public void render(Graphics g){ | |
updateOffsets(); | |
// world.getEntities().stream().parallel().filter(e -> e instanceof Drawable).map(e -> (Drawable)e).forEach(e -> e.draw(g, xOffset, yOffset)); | |
for(int i = 0; i<world.getEntities().length; i++){ | |
Entity entity = world.getEntities()[i]; | |
if( entity instanceof Drawable ){ | |
((Drawable)entity).draw(g, xOffset, yOffset); | |
} | |
} | |
} | |
private void updateOffsets(){ | |
if(followingEntity == null) return; | |
xOffset = (getWidth() / 2) - followingEntity.getLocation().getPoint().x; | |
yOffset = (getHeight() / 2) - followingEntity.getLocation().getPoint().y; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.Andrewcpu.morph.logic; | |
import net.Andrewcpu.morph.logic.elements.Nexus; | |
import net.Andrewcpu.morph.logic.elements.Team; | |
import net.Andrewcpu.morph.logic.elements.interfaces.*; | |
import net.Andrewcpu.morph.logic.elements.tools.ElementTools; | |
import net.Andrewcpu.morph.utils.Location; | |
import java.awt.*; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
public class Battery extends Entity implements Drawable, Teamable, PowerSource, Powerable, Levelable { | |
private Team team; | |
private int power = 0; | |
private int level = 1; | |
public Battery(Location location, Team team) { | |
super(location, EntityType.BATTERY); | |
this.team = team; | |
} | |
@Override | |
public void draw(Graphics g, int x, int y) { | |
int range = ElementTools.levelToRange(getLevel()); | |
g.setColor(ElementTools.teamToColor(getTeam())); | |
g.drawOval(getLocation().getPoint().x + x - (range / 2) + 25, getLocation().getPoint().y + y - (range / 2) + 25, range, range); | |
g.fillRoundRect(getLocation().getPoint().x + x, getLocation().getPoint().y + y, 50,50, 5, 5); | |
g.setColor(Color.BLACK); | |
g.drawString(getStepsToPowerSource(0) + "s " , getLocation().getPoint().x + x, getLocation().getPoint().y + y); | |
g.drawString(getPower() + "p " , getLocation().getPoint().x + x + 55, getLocation().getPoint().y + y + 25); | |
List<Drawable> drawables = getPowering().stream().filter(e -> e instanceof Drawable).map(e -> (Drawable)e).collect(Collectors.toList()); | |
for(Drawable d : drawables){ | |
Entity e = (Entity)d; | |
g.drawLine(getLocation().getPoint().x + x + 25, getLocation().getPoint().y + y + 25, e.getLocation().getPoint().x + x, e.getLocation().getPoint().y + y); | |
} | |
} | |
public int getStepsToPowerSource(int steps){ | |
steps++; | |
if(steps >= 10) return -1; | |
List<PowerSource> powerSources = getLocation().getWorld().getEntitiesByType(Nexus.class).stream().map(e -> (PowerSource)e).collect(Collectors.toList()); | |
List<Integer> amounts = new ArrayList<>(); | |
for(Powerable powerable : getPowering()){ | |
if(!(powerable instanceof PowerSource)) continue; | |
// now we know its a battery | |
for(PowerSource source : powerSources) | |
if(source.getPowering().contains(this)) | |
return 0; | |
amounts.add(((Battery)powerable).getStepsToPowerSource(steps) + 1); | |
} | |
int minPath = Integer.MAX_VALUE; | |
for(int i : amounts){ | |
if(i < minPath) | |
minPath = i; | |
} | |
return minPath; | |
} | |
@Override | |
public void distributePower() { | |
int powerUsed = 0; | |
List<Powerable> powerables = getPowering(); | |
List<Battery> batteries = getPowering().stream().filter(b ->( b instanceof Battery)).map(b -> ((Battery)b)).collect(Collectors.toList()); | |
powerables.removeAll(batteries); | |
double perPowerable = getOutputEnergyLevel() * 1.0 / powerables.size(); | |
if(perPowerable > 10) perPowerable = 10; | |
for(Powerable p : powerables){ | |
p.addPower((int)perPowerable); | |
powerUsed += perPowerable; | |
} | |
if(batteries.size() == 0) return; | |
int leftOver = (int)(getOutputEnergyLevel() - (powerables.size() * perPowerable)); | |
leftOver /= batteries.size(); | |
for(Battery battery : batteries){ | |
battery.addPower(leftOver); | |
powerUsed += leftOver; | |
} | |
power -= powerUsed; | |
} | |
@Override | |
public void tick(){ | |
super.tick(); | |
distributePower(); | |
} | |
@Override | |
public List<Powerable> getPowering() { | |
List<Powerable> entitiesWithinRange = getLocation().getWorld().getEntitiesWithinRange(this, ElementTools.levelToRange(getLevel())).stream().filter(e -> e instanceof Powerable).map(e -> (Powerable)e).collect(Collectors.toList()); | |
return entitiesWithinRange; | |
} | |
@Override | |
public int getLevel() { | |
return level; | |
} | |
@Override | |
public void setLevel(int level) { | |
this.level = level; | |
} | |
@Override | |
public int getOutputEnergyLevel() { | |
return power > ElementTools.levelToPower(getLevel()) ? ElementTools.levelToPower(getLevel()) : power; | |
} | |
@Override | |
public void addPower(int power) { | |
this.power += power; | |
} | |
@Override | |
public int getPower() { | |
return power; | |
} | |
@Override | |
public Team getTeam() { | |
return team; | |
} | |
@Override | |
public void setTeam(Team team) { | |
this.team = team; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.Andrewcpu.morph.logic; | |
import net.Andrewcpu.morph.logic.elements.interfaces.Damageable; | |
import net.Andrewcpu.morph.logic.events.EntityDamageEvent; | |
import net.Andrewcpu.morph.utils.Location; | |
public class DamageableEntity extends Entity implements Damageable { | |
private int health = 100; | |
public DamageableEntity(Location location, EntityType type) { | |
super(location, type); | |
} | |
@Override | |
public int getHealth() { | |
return health; | |
} | |
@Override | |
public void damage(int damage, Entity damager) { | |
EntityDamageEvent event = (EntityDamageEvent)EventManager.getInstance().handleEvent(new EntityDamageEvent(damager, this, damage)); | |
if(event.isCancelled()) return; | |
health -= event.getDamage(); | |
if(health <= 0) | |
death(); | |
} | |
@Override | |
public void death() { | |
getLocation().getWorld().removeEntity(this); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.Andrewcpu.morph.logic.elements; | |
import net.Andrewcpu.morph.logic.DamageableEntity; | |
import net.Andrewcpu.morph.logic.Entity; | |
import net.Andrewcpu.morph.logic.EntityType; | |
import net.Andrewcpu.morph.logic.elements.interfaces.Drawable; | |
import net.Andrewcpu.morph.logic.elements.interfaces.Teamable; | |
import net.Andrewcpu.morph.logic.elements.tools.ElementTools; | |
import net.Andrewcpu.morph.utils.Location; | |
import java.awt.*; | |
public class Grunt extends DamageableEntity implements Teamable, Drawable { | |
private Team team; | |
private int speed = 1; | |
public Grunt(Location location, Team team) { | |
super(location, EntityType.GRUNT); | |
this.team = team; | |
} | |
@Override | |
public void draw(Graphics g, int x, int y) { | |
g.setColor(ElementTools.teamToColor(getTeam())); | |
g.fillRect(getLocation().getPoint().x + x, getLocation().getPoint().y + y, 10, 20); | |
} | |
@Override | |
public void tick(){ | |
super.tick(); | |
//move | |
Entity target = getLocation().getWorld().getNearestEnemyByType(this, Nexus.class); | |
if(target == null) return; | |
Location diff = getLocation().difference(target.getLocation()); //after point - current point 50, 45 | |
if(diff.getPoint().x > 0){ // move right0 | |
getLocation().travel(speed,0); | |
} | |
else if(diff.getPoint().x < 0){ // left | |
getLocation().travel(-speed,0); | |
} | |
if(diff.getPoint().y > 0) { // move down | |
getLocation().travel(0,speed); | |
} | |
else if(diff.getPoint().y < 0){ // move up | |
getLocation().travel(0,-speed); | |
} | |
} | |
@Override | |
public Team getTeam() { | |
return this.team; | |
} | |
@Override | |
public void setTeam(Team team) { | |
this.team = team; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.Andrewcpu.morph.logic.elements; | |
import net.Andrewcpu.morph.logic.Entity; | |
import net.Andrewcpu.morph.logic.EntityType; | |
import net.Andrewcpu.morph.logic.elements.interfaces.Drawable; | |
import net.Andrewcpu.morph.logic.elements.interfaces.Teamable; | |
import net.Andrewcpu.morph.logic.elements.tools.ElementTools; | |
import net.Andrewcpu.morph.utils.Location; | |
import java.awt.*; | |
public class Hut extends Entity implements Drawable, Teamable { | |
private int spawnRate = 500; | |
private int currentSpawn = (int)(Math.random() * spawnRate); | |
private Team team; | |
public Hut(Location location, Team team) { | |
super(location, EntityType.GRUNT_HUT); | |
this.team = team; | |
} | |
@Override | |
public void draw(Graphics g, int x, int y) { | |
g.setColor(ElementTools.teamToColor(getTeam())); | |
g.fillRect(getLocation().getPoint().x + x, getLocation().getPoint().y + y, 10, 50); | |
g.fillRect(getLocation().getPoint().x + x + 30, getLocation().getPoint().y + y, 10, 50); | |
g.fillRect(getLocation().getPoint().x + x, getLocation().getPoint().y + y, 40, 10); | |
} | |
@Override | |
public void tick(){ | |
super.tick(); | |
currentSpawn++; | |
if(currentSpawn >= spawnRate){ | |
currentSpawn = 0; | |
//spawn grunt | |
Grunt grunt = new Grunt(getLocation().clone().travel((int)(Math.random() * 50 * (Math.random() < .5 ? -1 : 1)), (int)(Math.random() * 50 * (Math.random() < .5 ? -1 : 1))), getTeam()); | |
getLocation().getWorld().addEntity(grunt); | |
} | |
} | |
@Override | |
public Team getTeam() { | |
return team; | |
} | |
@Override | |
public void setTeam(Team team) { | |
this.team = team; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.Andrewcpu.morph.logic.elements.interfaces; | |
import net.Andrewcpu.morph.logic.Entity; | |
public interface Damageable { | |
int getHealth(); | |
void damage(int damage, Entity damager); | |
void death(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.Andrewcpu.morph.logic.elements.interfaces; | |
import java.awt.*; | |
public interface Drawable { | |
void draw(Graphics g, int x, int y); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.Andrewcpu.morph.logic.elements.interfaces; | |
public interface Levelable { | |
int getLevel(); | |
void setLevel(int level); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.Andrewcpu.morph.logic.elements.interfaces; | |
public interface Powerable { | |
void addPower(int power); | |
int getPower(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.Andrewcpu.morph.logic.elements.interfaces; | |
import java.util.List; | |
public interface PowerSource { | |
void distributePower(); | |
List<Powerable> getPowering(); | |
int getOutputEnergyLevel(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.Andrewcpu.morph.logic.elements.interfaces; | |
import net.Andrewcpu.morph.logic.elements.Team; | |
public interface Teamable { | |
Team getTeam(); | |
void setTeam(Team team); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.Andrewcpu.morph.logic.elements.interfaces; | |
public interface Tickable { | |
void tick(); | |
int getLife(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.Andrewcpu.morph.logic.elements; | |
import net.Andrewcpu.morph.logic.Battery; | |
import net.Andrewcpu.morph.logic.Entity; | |
import net.Andrewcpu.morph.logic.EntityType; | |
import net.Andrewcpu.morph.logic.LeveledEntity; | |
import net.Andrewcpu.morph.logic.elements.interfaces.Drawable; | |
import net.Andrewcpu.morph.logic.elements.interfaces.PowerSource; | |
import net.Andrewcpu.morph.logic.elements.interfaces.Powerable; | |
import net.Andrewcpu.morph.logic.elements.interfaces.Teamable; | |
import net.Andrewcpu.morph.logic.elements.tools.ElementTools; | |
import net.Andrewcpu.morph.utils.Location; | |
import java.awt.*; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
public class Nexus extends LeveledEntity implements Teamable, PowerSource, Drawable { | |
private Team team; | |
public Nexus(Location location, Team team) { | |
super(location, EntityType.NEXUS); | |
this.team = team; | |
} | |
@Override | |
public Team getTeam() { | |
return team; | |
} | |
@Override | |
public void setTeam(Team team) { | |
this.team = team; | |
} | |
public void levelUp(){ | |
setLevel(getLevel() + 1); | |
} | |
@Override | |
public int getOutputEnergyLevel() { | |
return ElementTools.levelToPower(getLevel()); | |
} | |
@Override | |
public void tick(){ | |
super.tick(); | |
distributePower(); | |
} | |
@Override | |
public void distributePower() { | |
List<Powerable> powerables = getPowering(); | |
List<Battery> batteries = getPowering().stream().filter(b ->( b instanceof Battery)).map(b -> ((Battery)b)).collect(Collectors.toList()); | |
powerables.removeAll(batteries); | |
double perPowerable = getOutputEnergyLevel() * 1.0 / powerables.size(); | |
if(perPowerable > 10) perPowerable = 10; | |
for(Powerable p : powerables){ | |
p.addPower((int)perPowerable); | |
} | |
if(batteries.size() == 0) return; | |
int leftOver = (int)(getOutputEnergyLevel() - (powerables.size() * perPowerable)); | |
leftOver /= batteries.size(); | |
for(Battery battery : batteries){ | |
battery.addPower(leftOver); | |
} | |
} | |
@Override | |
public List<Powerable> getPowering() { | |
List<Powerable> entitiesWithinRange = getLocation().getWorld().getEntitiesWithinRange(this, ElementTools.levelToRange(getLevel())).stream().filter(e -> e instanceof Powerable).map(e -> (Powerable)e).collect(Collectors.toList()); | |
return entitiesWithinRange; | |
} | |
@Override | |
public void draw(Graphics g, int x, int y) { | |
g.setColor(ElementTools.teamToColor(getTeam())); | |
int range = ElementTools.levelToRange(getLevel()); | |
g.drawOval(getLocation().getPoint().x + x - (range / 2) + 25, getLocation().getPoint().y + y - (range / 2) + 25, range, range); | |
g.fillOval(getLocation().getPoint().x + x, getLocation().getPoint().y + y, 50, 50); | |
g.setColor(Color.BLACK); | |
List<Drawable> drawables = getPowering().stream().filter(e -> e instanceof Drawable).map(e -> (Drawable)e).collect(Collectors.toList()); | |
for(Drawable d : drawables){ | |
Entity e = (Entity)d; | |
g.drawLine(getLocation().getPoint().x + x + 25, getLocation().getPoint().y + y + 25, e.getLocation().getPoint().x + x, e.getLocation().getPoint().y + y); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.Andrewcpu.morph.logic.elements; | |
import net.Andrewcpu.morph.logic.DamageableEntity; | |
import net.Andrewcpu.morph.logic.Entity; | |
import net.Andrewcpu.morph.logic.EntityType; | |
import net.Andrewcpu.morph.logic.EventManager; | |
import net.Andrewcpu.morph.logic.elements.Team; | |
import net.Andrewcpu.morph.logic.elements.interfaces.Damageable; | |
import net.Andrewcpu.morph.logic.elements.interfaces.Drawable; | |
import net.Andrewcpu.morph.logic.elements.interfaces.Teamable; | |
import net.Andrewcpu.morph.logic.elements.interfaces.Tickable; | |
import net.Andrewcpu.morph.logic.elements.tools.ElementTools; | |
import net.Andrewcpu.morph.logic.events.EntityDamageEvent; | |
import net.Andrewcpu.morph.logic.input.Movement; | |
import net.Andrewcpu.morph.utils.Location; | |
import java.awt.*; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class Player extends DamageableEntity implements Teamable, Drawable { | |
private Team team; | |
private List<Movement> movementList = new ArrayList<>(); | |
private int speed = 5; | |
public Player(Location location, Team team) { | |
super(location, EntityType.PLAYER); | |
this.team = team; | |
} | |
@Override | |
public Team getTeam() { | |
return team; | |
} | |
@Override | |
public void setTeam(Team team) { | |
this.team = team; | |
} | |
@Override | |
public void draw(Graphics g, int x, int y) { | |
g.setColor(ElementTools.teamToColor(getTeam())); | |
g.fillRect(getLocation().getPoint().x + x, getLocation().getPoint().y + y, 50, 100); | |
} | |
@Override | |
public void tick(){ | |
super.tick(); | |
for(Movement movement : movementList){ | |
int x = 0; | |
int y = 0; | |
if(movement == Movement.LEFT || movement == Movement.RIGHT) | |
x = movement == Movement.LEFT ? -speed : speed; | |
else | |
y = movement == Movement.UP ? -speed : speed; | |
getLocation().travel(x, y); | |
} | |
} | |
public void addMovement(Movement movement){ | |
if(movementList.contains(movement)) return; | |
movementList.add(movement); | |
} | |
public void removeMovement(Movement movement){ | |
if(!movementList.contains(movement)) return; | |
movementList.remove(movement); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.Andrewcpu.morph.logic.elements; | |
public enum Team { | |
RED, BLACK; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.Andrewcpu.morph.logic.elements.tools; | |
import net.Andrewcpu.morph.logic.elements.Team; | |
import java.awt.*; | |
public class ElementTools { | |
public static Color teamToColor(Team team){ | |
switch (team){ | |
case RED: | |
return Color.RED; | |
case BLACK: | |
return Color.BLACK; | |
} | |
return Color.GREEN; | |
} | |
public static int levelToRange(int level){ | |
return level * 200; | |
} | |
public static int levelToPower(int level){ | |
return level * 20; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.Andrewcpu.morph.logic.elements; | |
import net.Andrewcpu.morph.logic.Entity; | |
import net.Andrewcpu.morph.logic.EntityType; | |
import net.Andrewcpu.morph.logic.LeveledEntity; | |
import net.Andrewcpu.morph.logic.elements.interfaces.*; | |
import net.Andrewcpu.morph.logic.elements.tools.ElementTools; | |
import net.Andrewcpu.morph.utils.Location; | |
import java.awt.*; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
public class Turret extends LeveledEntity implements Teamable, Powerable, Drawable { | |
private Team team; | |
private int power = 0; | |
public Turret(Location location, Team team) { | |
super(location, EntityType.TURRET); | |
this.team = team; | |
} | |
@Override | |
public void draw(Graphics g, int x, int y) { | |
g.setColor(Color.BLACK); | |
g.drawString(getPower() + "p", getLocation().getPoint().x + x, getLocation().getPoint().y + y - 10); | |
g.setColor(ElementTools.teamToColor(getTeam())); | |
g.fillRect(getLocation().getPoint().x + x, getLocation().getPoint().y + y, 50, 50); | |
} | |
private List<Damageable> getEnemiesWithinRange(){ | |
List<Damageable> enemies = getLocation().getWorld().getEntitiesWithinRange(this, ElementTools.levelToRange(getLevel())).stream().filter(e -> e instanceof Damageable).filter(e -> e instanceof Teamable).filter(e -> ((Teamable)e).getTeam() != getTeam()).map(e -> (Damageable)e).collect(Collectors.toList()); | |
return enemies; | |
} | |
public void shootWithinRange(){ | |
for(Damageable d : getEnemiesWithinRange()){ | |
if(power < 10) return; | |
power-= 10; | |
d.damage(1, this); | |
} | |
} | |
@Override | |
public void tick(){ | |
super.tick(); | |
shootWithinRange(); | |
} | |
@Override | |
public void addPower(int power) { | |
this.power += power; | |
} | |
@Override | |
public int getPower() { | |
return power; | |
} | |
@Override | |
public Team getTeam() { | |
return team; | |
} | |
@Override | |
public void setTeam(Team team) { | |
this.team = team; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.Andrewcpu.morph.logic; | |
import net.Andrewcpu.morph.logic.elements.interfaces.Tickable; | |
import net.Andrewcpu.morph.utils.Location; | |
public abstract class Entity implements Tickable { | |
private Location location; | |
private EntityType type; | |
private int life; | |
public Entity(Location location, EntityType type) { | |
this.location = location; | |
this.type = type; | |
} | |
public Location getLocation() { | |
return location; | |
} | |
public void setLocation(Location location) { | |
this.location = location; | |
} | |
public EntityType getType() { | |
return type; | |
} | |
public void setType(EntityType type) { | |
this.type = type; | |
} | |
@Override | |
public void tick() { | |
life++; | |
} | |
@Override | |
public int getLife() { | |
return life; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.Andrewcpu.morph.logic; | |
public enum EntityType { | |
PLAYER, NEXUS, TURRET, GRUNT, BATTERY, GRUNT_HUT | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.Andrewcpu.morph.logic; | |
import net.Andrewcpu.morph.logic.events.EntityDamageEvent; | |
import net.Andrewcpu.morph.logic.events.Event; | |
import net.Andrewcpu.morph.utils.EventListener; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class EventManager { | |
private static EventManager instance = null; | |
public static EventManager getInstance(){ | |
if(instance == null) | |
instance = new EventManager(); | |
return instance; | |
} | |
private List<EventListener> listeners = new ArrayList<>(); | |
public void registerListener(EventListener listener){ | |
listeners.add(listener); | |
} | |
public Event handleEvent(Event event){ | |
if(event instanceof EntityDamageEvent){ | |
for(EventListener listener : listeners){ | |
listener.onEntityDamagedByEntityEvent((EntityDamageEvent)event); | |
} | |
} | |
return event; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.Andrewcpu.morph.logic.events; | |
import net.Andrewcpu.morph.logic.Entity; | |
public class EntityDamageEvent extends Event{ | |
private Entity damager; | |
private Entity damaged; | |
private int damage; | |
public EntityDamageEvent(Entity damager, Entity damaged, int damage) { | |
this.damager = damager; | |
this.damaged = damaged; | |
this.damage = damage; | |
} | |
public Entity getDamager() { | |
return damager; | |
} | |
public void setDamager(Entity damager) { | |
this.damager = damager; | |
} | |
public Entity getDamaged() { | |
return damaged; | |
} | |
public void setDamaged(Entity damaged) { | |
this.damaged = damaged; | |
} | |
public int getDamage() { | |
return damage; | |
} | |
public void setDamage(int damage) { | |
this.damage = damage; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.Andrewcpu.morph.logic.events; | |
import java.util.UUID; | |
public class Event { | |
private UUID eventID; | |
private boolean cancelled = false; | |
public Event() { | |
this.eventID = UUID.randomUUID(); | |
} | |
public UUID getEventID() { | |
return eventID; | |
} | |
public boolean isCancelled() { | |
return cancelled; | |
} | |
public void setCancelled(boolean cancelled) { | |
this.cancelled = cancelled; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.Andrewcpu.morph.logic.input; | |
import net.Andrewcpu.morph.logic.Battery; | |
import net.Andrewcpu.morph.logic.elements.Player; | |
import net.Andrewcpu.morph.logic.elements.Nexus; | |
import net.Andrewcpu.morph.logic.elements.Turret; | |
import net.Andrewcpu.morph.utils.Controls; | |
public class ActionInterpreter { | |
public static void listen(Player player, int action){ | |
if(action == Controls.PLACE_NEXUS){ | |
Nexus nexus = new Nexus(player.getLocation().clone(), player.getTeam()); | |
player.getLocation().getWorld().addEntity(nexus); | |
} | |
else if(action == Controls.PLACE_TURRET){ | |
Turret turret = new Turret(player.getLocation().clone(), player.getTeam()); | |
player.getLocation().getWorld().addEntity(turret); | |
} | |
else if(action == Controls.PLACE_BATTERY){ | |
Battery battery = new Battery(player.getLocation().clone(), player.getTeam()); | |
player.getLocation().getWorld().addEntity(battery); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.Andrewcpu.morph.logic.input; | |
import net.Andrewcpu.morph.logic.elements.Player; | |
import net.Andrewcpu.morph.utils.Controls; | |
import java.awt.event.KeyEvent; | |
import java.awt.event.KeyListener; | |
public class Controller implements KeyListener { | |
private Player player; | |
public Controller(Player player){ | |
this.player = player; | |
} | |
@Override | |
public void keyTyped(KeyEvent e) { | |
} | |
@Override | |
public void keyPressed(KeyEvent e) { | |
if(e.getKeyCode() == Controls.DOWN){ | |
player.addMovement(Movement.DOWN); | |
} | |
if(e.getKeyCode() == Controls.UP){ | |
player.addMovement(Movement.UP); | |
} | |
if(e.getKeyCode() == Controls.LEFT){ | |
player.addMovement(Movement.LEFT); | |
} | |
if(e.getKeyCode() == Controls.RIGHT){ | |
player.addMovement(Movement.RIGHT); | |
} | |
if(e.getKeyCode() == Controls.PLACE_NEXUS || e.getKeyCode() == Controls.PLACE_TURRET || e.getKeyCode() == Controls.PLACE_BATTERY){ | |
ActionInterpreter.listen(player, e.getKeyCode()); | |
} | |
} | |
@Override | |
public void keyReleased(KeyEvent e) { | |
if(e.getKeyCode() == Controls.DOWN){ | |
player.removeMovement(Movement.DOWN); | |
} | |
if(e.getKeyCode() == Controls.UP){ | |
player.removeMovement(Movement.UP); | |
} | |
if(e.getKeyCode() == Controls.LEFT){ | |
player.removeMovement(Movement.LEFT); | |
} | |
if(e.getKeyCode() == Controls.RIGHT){ | |
player.removeMovement(Movement.RIGHT); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.Andrewcpu.morph.logic.input; | |
public enum Movement { | |
UP, DOWN, LEFT, RIGHT; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.Andrewcpu.morph.logic; | |
import net.Andrewcpu.morph.logic.elements.interfaces.Levelable; | |
import net.Andrewcpu.morph.utils.Location; | |
public class LeveledEntity extends Entity implements Levelable { | |
private int level = 1; | |
public LeveledEntity(Location location, EntityType type) { | |
super(location, type); | |
} | |
@Override | |
public int getLevel() { | |
return level; | |
} | |
@Override | |
public void setLevel(int level) { | |
this.level = level; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.Andrewcpu.morph.logic; | |
import net.Andrewcpu.morph.logic.elements.Team; | |
import net.Andrewcpu.morph.logic.elements.interfaces.Teamable; | |
import net.Andrewcpu.morph.logic.elements.interfaces.Tickable; | |
import java.awt.*; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
public class World implements Tickable { | |
private List<Entity> entities = new ArrayList<>(); | |
private List<Entity> inbox = new ArrayList<>(); | |
private List<Entity> outbox = new ArrayList<>(); | |
private int life = 0; | |
public Entity[] getEntities() { | |
return entities.toArray(new Entity[entities.size()]); | |
} | |
public void addEntity(Entity entity){ | |
inbox.add(entity); | |
} | |
public void removeEntity(Entity entity){ | |
outbox.add(entity); | |
} | |
public List<Entity> getEntitiesByType(Class i){ | |
List<Entity> ents = new ArrayList<>(); | |
for(int n = 0; n<entities.size(); n++){ | |
if(getEntities()[n].getClass() == i) | |
ents.add(getEntities()[n]); | |
} | |
return ents; | |
} | |
public Entity getNearestEntityByType(Point p, Class i){ | |
Entity closest = null; | |
double range = Double.MAX_VALUE; | |
for(Entity e : entities){ | |
if(e.getClass() == i && e.getLocation().getPoint().distance(p) < range){ | |
range = e.getLocation().getPoint().distance(p); | |
closest = e; | |
} | |
} | |
return closest; | |
} | |
public Entity getNearestEntityByType(Entity e, Class i){ | |
return getNearestEntityByType(e.getLocation().getPoint(), i); | |
} | |
public Entity getNearestEnemyByType(Teamable team, Class i){ | |
Entity closest = null; | |
double range = Double.MAX_VALUE; | |
for(Entity e : entities){ | |
if(e instanceof Teamable && ((Teamable)e).getTeam() != team.getTeam() && e.getClass() == i && e.getLocation().getPoint().distance(((Entity)team).getLocation().getPoint()) < range){ | |
range = e.getLocation().getPoint().distance(((Entity)team).getLocation().getPoint()); | |
closest = e; | |
} | |
} | |
return closest; | |
} | |
public List<Entity> getEntitiesWithinRange(Point point, int range){ | |
List<Entity> nearby = new ArrayList<>(); | |
for(int i= 0; i<entities.size(); i++){ | |
Entity entity = entities.get(i); | |
if(entity.getLocation().getPoint().distance(point) <= range) | |
nearby.add(entity); | |
} | |
return nearby; | |
} | |
public List<Entity> getEntitiesWithinRange(Entity entity, int range){ | |
List<Entity> nearby = getEntitiesWithinRange(entity.getLocation().getPoint(), range); | |
nearby.remove(entity); | |
return nearby; | |
} | |
@Override | |
public void tick() { | |
life++; | |
for(Entity entity : getEntities()){ | |
entity.tick(); | |
} | |
entities.addAll(inbox); | |
entities.removeAll(outbox); | |
inbox.clear(); | |
outbox.clear(); | |
} | |
@Override | |
public int getLife() { | |
return life; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.Andrewcpu.morph; | |
import net.Andrewcpu.morph.graphics.GPanel; | |
import net.Andrewcpu.morph.logic.elements.*; | |
import net.Andrewcpu.morph.logic.World; | |
import net.Andrewcpu.morph.logic.input.Controller; | |
import net.Andrewcpu.morph.utils.Location; | |
import javax.swing.*; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.TimeUnit; | |
public class Main extends JFrame { | |
public static void main(String[] args) { | |
new Main(); | |
} | |
public Main(){ | |
setBounds(0,0,1000,800); | |
World world = new World(); | |
Nexus nexus = new Nexus(new Location(world, 50, 50), Team.RED); | |
world.addEntity(nexus); | |
nexus.levelUp(); | |
Turret turret = new Turret(new Location(world, 100, 100), Team.RED); | |
world.addEntity(turret); | |
Player player = new Player(new Location(world, 0,0),Team.RED); | |
world.addEntity(player); | |
for(int x = -1000; x<= 1000; x+=20){ | |
for(int y = -1000; y<= 1000; y+=20){ | |
if(Math.random() <= 0.0005){ | |
Hut hut = new Hut(new Location(world, x, y), Team.BLACK); | |
world.addEntity(hut); | |
} | |
} | |
} | |
GPanel panel = new GPanel(world); | |
panel.setBounds(getBounds()); | |
addKeyListener(new Controller(player)); | |
panel.getRenderer().setFollowingEntity(player); | |
add(panel); | |
setVisible(true); | |
Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(()->{ | |
world.tick(); | |
panel.repaint(); | |
}, 10, 10, TimeUnit.MILLISECONDS); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.Andrewcpu.morph.utils; | |
import java.awt.event.KeyEvent; | |
public class Controls { | |
public static final int LEFT = KeyEvent.VK_A; | |
public static final int RIGHT = KeyEvent.VK_D; | |
public static final int UP = KeyEvent.VK_W; | |
public static final int DOWN = KeyEvent.VK_S; | |
public static final int PLACE_TURRET = KeyEvent.VK_T; | |
public static final int PLACE_NEXUS = KeyEvent.VK_N; | |
public static final int PLACE_BATTERY = KeyEvent.VK_B; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.Andrewcpu.morph.utils; | |
import net.Andrewcpu.morph.logic.events.EntityDamageEvent; | |
public class EventListener { | |
public void onEntityDamagedByEntityEvent(EntityDamageEvent event){ | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.Andrewcpu.morph.utils; | |
import net.Andrewcpu.morph.logic.World; | |
import java.awt.*; | |
public class Location { | |
private World world; | |
private Point point; | |
public Location(World world, double x, double y) { | |
this.world = world; | |
this.point = new Point((int)x,(int)y); | |
} | |
public Location(World world, Point point) { | |
this.world = world; | |
this.point = point; | |
} | |
public World getWorld() { | |
return world; | |
} | |
public void setWorld(World world) { | |
this.world = world; | |
} | |
public Point getPoint() { | |
return point; | |
} | |
public void setLocation(int x, int y){ | |
this.point = new Point(x,y); | |
} | |
//add to location | |
public Location travel(int x, int y){ | |
setLocation(getPoint().x + x, getPoint().y + y); | |
return this; | |
} | |
public Location clone(){ | |
return new Location(getWorld(), getPoint().x, getPoint().y); | |
} | |
/* | |
if comparing point is to the right, this will be positive | |
if comparing point is above, this will be negative | |
*/ | |
public Location difference(Location l){ | |
return new Location(l.getWorld(), l.getPoint().x - getPoint().x, l.getPoint().y - getPoint().y); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment