Skip to content

Commit

Permalink
performance improvements and some javadocs
Browse files Browse the repository at this point in the history
  • Loading branch information
fazo96 committed Jul 21, 2015
1 parent abb3c96 commit 70089e4
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 75 deletions.
56 changes: 33 additions & 23 deletions core/src/logic/Creature.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import logic.neural.Brain;

/**
* A (hopefully) smart biological creature.
* A (hopefully) smart biological creature in the simulated world.
*
* @author fazo
*/
Expand All @@ -24,6 +24,12 @@ public class Creature extends Element implements Runnable {
private Sight[] sights;
private Thread workerThread;

/**
* Create a creature with a random mind at given position in space
*
* @param x
* @param y
*/
public Creature(float x, float y) {
super(x, y, default_radius);
dir = (float) (Math.random() * 2 * Math.PI);
Expand Down Expand Up @@ -157,13 +163,14 @@ public void update() {

@Override
public void render(ShapeRenderer s) {
// Body
// Draw Body
s.setColor(1 - (hp / max_hp), hp / max_hp, 0, 1);
s.circle(getX(), getY(), getSize());
// Vision
// Prepare vision stuff
double relX = Math.cos(dir), relY = Math.sin(dir);
float c = 0;
float eyeX = (float) (relX * getSize() * 0.6f), eyeY = (float) (relY * getSize() * 0.6f);
// Draw Sight Lines
if (Game.get().getWorld().getOptions().getOrDefault("draw_sight_lines", 0f) > 0) {
for (Sight sight : sights) {
if (sight != null) {
Expand All @@ -180,19 +187,21 @@ public void render(ShapeRenderer s) {
}
}
}
// Draw eye
if (sights[0] == null && sights[1] == null) {
s.setColor(1, 1, 1, 1);
} else {
s.setColor(sights[1] == null ? 0 : 1, sights[0] == null ? 0 : 1, 0, 1);
}
s.circle(getX() + eyeX, getY() + eyeY, 3);
//FOV
// Draw FOV cone
float degrees = fov * 360f / (float) Math.PI;
float orient = dir * 180f / (float) Math.PI - degrees / 2;
if (Game.get().getWorld().getOptions().getOrDefault("draw_view_cones", 0f) > 0) {
s.setColor(0.3f, 0.3f, 0.3f, 1);
s.arc((float) eyeX + getX(), (float) eyeY + getY(), sightRange, orient, degrees);
}
// Draw damage/heal marks
if (hp < prevHp) {
// Damage mark
s.set(ShapeRenderer.ShapeType.Filled);
Expand All @@ -205,11 +214,17 @@ public void render(ShapeRenderer s) {
s.circle(getX(), getY(), 5);
}
s.set(ShapeRenderer.ShapeType.Line);
// Beak
// Draw Beak
s.setColor(beak / max_beak, 1 - beak / max_beak, 0, 1);
s.line((float) (relX * getSize() * 0.8f + getX()), (float) (relY * getSize() * 0.8f + getY()), (float) (relX * getSize() * (1.5f + beak / max_beak) + getX()), (float) (relY * getSize() * (1.5f + beak / max_beak) + getY()));
}

/**
* Store Sight information (what the creature sees) and eat/attack if
* applicable
*
* @return the sight information retrieved
*/
public Sight[] interactWithWorld() {
Sight[] newSights = new Sight[2];
// Try to see plant
Expand Down Expand Up @@ -299,28 +314,21 @@ public Sight[] interactWithWorld() {
return newSights;
}

public void eat() {
eating = false;
for (Element e : Game.get().getWorld().getPlants()) {
if (overlaps(e)) {
eating = true;
e.setSize(e.getSize() - 0.1f);
if (e.getSize() == 0) {
e.setSize(0);
}
hp++;
fitness++;
if (hp > max_hp) {
hp = max_hp;
}
}
}
}

/**
* Apply a modification to this creature's health. Can be negative.
*
* @param amount how much to heal/damage
*/
private void heal(float amount) {
hp += amount;
}

/**
* Praise this creature by increasing fitness. Can be negative to decrease
* fitness
*
* @param amount how much
*/
private void praise(float amount) {
fitness += amount;
}
Expand All @@ -340,9 +348,11 @@ public boolean isWorkerDone() {
public void startWorker() {
workerDone = false;
if (workerThread == null) {
// Create a new thread
workerThread = new Thread(this);
workerThread.start();
} else {
// Interrupt current thread, throwing it out of sleep
workerThread.interrupt();
}
}
Expand Down
60 changes: 55 additions & 5 deletions core/src/logic/Element.java
Original file line number Diff line number Diff line change
@@ -1,49 +1,99 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package logic;

import com.badlogic.gdx.graphics.glutils.ShapeRenderer;

/**
* Represents a dynamic simulation element with a circular shape
*
* @author fazo
*/
public abstract class Element {

private float x, y, size;

/**
* Create an element at given position with given radius. Elements have a
* circular shape.
*
* @param x the x position
* @param y the y position
* @param size the element body radius
*/
public Element(float x, float y, float size) {
this.x = x;
this.y = y;
this.size = size;
}

/**
* Calculate the distance between the circumference of this element and
* another one's, taking into account the size. This means that two elements
* whose circumference is touching will have a distance of 0, and two
* overlapping elements will have a negative distance
*
* @param e the element whose distance from this one will be checked
* @return the distance from the element. It's 0 if they are just touching,
* negative if they are colliding and positive if they are not
*/
public float distanceFrom(Element e) {
return (float) Math.sqrt(Math.pow(e.x - x, 2) + Math.pow(e.y - y, 2)) - getSize() - e.getSize();
}

/**
* Checks if this element overlaps another one, causing a collision
*
* @param e the element which position will be checked to see if it overlaps
* this one
* @return true if the elements are overlapping, causing a collision
*/
public boolean overlaps(Element e) {
return distanceFrom(e) < 0;
}

/**
* Checks if this element overlaps a circular object, causing a collision
*
* @param x the position of the center of the circular object
* @param y the position of the center of the circular object
* @param radius the radius of the circular object
* @return true if the object overlaps this one
*/
public boolean overlaps(float x, float y, float radius) {
return (float) Math.sqrt(Math.pow(x - this.x, 2) + Math.pow(y - this.y, 2)) < getSize() + radius;
}

/**
* Check if a point in space occupies the same space as this object
*
* @param x the x position of the point to check
* @param y the y position of the point to check
* @return true if the point is part of this object's area
*/
public boolean overlaps(float x, float y) {
return overlaps(x, y, 1);
}

/**
* Translate this object is space
*
* @param deltaX x translation
* @param deltaY y translation
*/
public void move(float deltaX, float deltaY) {
x += deltaX;
y += deltaY;
}

/**
* Run one iteration of this object's logic
*/
public abstract void update();

/**
* Draw this object
*
* @param s the ShapeRenderer used to draw this object
*/
public abstract void render(ShapeRenderer s);

public float getX() {
Expand Down
9 changes: 3 additions & 6 deletions core/src/logic/Sight.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package logic;

/**
* Stores a sight of an element, as seen from a creature's eye
*
* @author fazo
*/
public class Sight {

private Element seen;
private float distance, angle;

Expand All @@ -30,5 +27,5 @@ public float getDistance() {
public float getAngle() {
return angle;
}

}
7 changes: 1 addition & 6 deletions core/src/logic/Vegetable.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package logic;

import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.mygdx.game.Game;

/**
*
* Represents a small plant-like vegetable with circular shape
* @author fazo
*/
public class Vegetable extends Element {
Expand Down
38 changes: 33 additions & 5 deletions core/src/logic/World.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package logic;

import com.mygdx.game.Game;
Expand All @@ -18,6 +13,8 @@
import java.util.logging.Logger;

/**
* This class represents an instance of a simulation, its world and its
* configuration
*
* @author fazo
*/
Expand All @@ -40,6 +37,12 @@ public class World implements Runnable {
private final ArrayList<Vegetable> deadPlants;
private final ArrayList<Listener> listeners;

/**
* Create a new World. Can be customized with given options.
*
* @param options customization options. Can be null. See the
* "reloadOptions" function for possible options
*/
public World(Map<String, Float> options) {
if (options == null) {
this.options = new HashMap<String, Float>();
Expand Down Expand Up @@ -240,6 +243,10 @@ private void newGen(boolean restart) {
}
}

/**
* Applies current options. Uses default alternatives if options are not
* provided
*/
public void reloadOptions() {
width = Math.round(options.getOrDefault("world_width", 2000f));
height = Math.round(options.getOrDefault("world_height", 2000f));
Expand All @@ -262,6 +269,14 @@ public void reloadOptions() {
mutationFactor = options.getOrDefault("nMutationFactor", 1f);
}

/**
* Spawn a new random element in the world, at a random position.
*
* @param isCreature true if you want to spawn a creature
* @param brainMap the brain configuration. Used if spawning a creature. If
* null, a random mind will be created
* @return the spawned element
*/
private Element spawn(boolean isCreature, float[][][] brainMap) {
int x, y, r;
boolean overlaps = false;
Expand Down Expand Up @@ -301,6 +316,13 @@ private Element spawn(boolean isCreature, float[][][] brainMap) {
}
}

/**
* Sets currently select creature to the first creature that overlaps given
* coordinates
*
* @param x the x coordinate of the creature you want to select
* @param y the x coordinate of the creature you want to select
*/
public void selectCreatureAt(int x, int y) {
selected = null; // Clear selection
try {
Expand All @@ -314,6 +336,12 @@ public void selectCreatureAt(int x, int y) {
}
}

/**
* Fire an event
*
* @param eventCode the event code. Look at the Listener class for event
* codes.
*/
public void fire(int eventCode) {
for (Listener f : listeners) {
f.on(eventCode);
Expand Down
Loading

0 comments on commit 70089e4

Please sign in to comment.