Feel free to join the next Helmholtz Hacky Hour #26 on Wednesday, April 21, 2021 from 2PM to 3PM!

Commit 66dbe4a5 authored by Martin Lange's avatar Martin Lange

reworked Grid.Float to simpler FloatGrid

parent 4b26987c
Pipeline #12169 passed with stage
in 12 seconds
......@@ -143,14 +143,14 @@ Grassers live in a two-dimensional world. The world is represented by a grid of
/// file:src/main/java/grassing/res/Grass.java
package grassing.res;
import grassing.util.Grid;
import grassing.util.FloatGrid;
public class Grass {
final public Grid.Float grass;
final public FloatGrid grass;
public Grass(int width, int height, float initial) {
this.grass = new Grid.Float(width, height);
this.grass = new FloatGrid(width, height);
for (int i = 0; i < grass.length; i++) {
grass.set(i, initial);
}
......
......@@ -25,7 +25,7 @@ import com.artemis.annotations.Wire;
import com.artemis.utils.IntBag;
import grassing.comp.*;
import grassing.res.Grass;
import grassing.util.Grid;
import grassing.util.FloatGrid;
import grassing.util.MathUtil;
import javax.swing.*;
......@@ -77,7 +77,7 @@ public class Graphics extends BaseEntitySystem {
}
void paint(Graphics2D g2d) {
Grid.Float grass = this.grass.grass;
FloatGrid grass = this.grass.grass;
int s = cellSize;
g2d.setBackground(Color.BLACK);
......@@ -120,20 +120,20 @@ public class Graphics extends BaseEntitySystem {
## Utilities
```java
/// file:src/main/java/grassing/util/Grid.java
/// file:src/main/java/grassing/util/FloatGrid.java
package grassing.util;
public abstract class Grid {
public class FloatGrid {
final public int width;
final public int height;
final public int length;
protected final Object data;
protected final float[] data;
protected Grid(Object data, int width, int height) {
this.data = data;
public FloatGrid(int width, int height) {
this.data = new float[width * height];
this.width = width;
this.height = height;
......@@ -148,25 +148,17 @@ public abstract class Grid {
return x >= 0 && y >= 0 && x < width && y < height;
}
public static final class Float extends Grid {
public Float(int width, int height) {
super((Object) (new float[width * height]), width, height);
}
private float[] getData() {
return (float[]) data;
}
public final float get(int idx) {
return getData()[idx];
}
public final float get(int x, int y) {
return getData()[getIndex(x, y)];
}
public final void set(int idx, float value) {
getData()[idx] = value;
}
public final void set(int x, int y, float value) {
getData()[getIndex(x, y)] = value;
}
public final float get(int idx) {
return data[idx];
}
public final float get(int x, int y) {
return data[getIndex(x, y)];
}
public final void set(int idx, float value) {
data[idx] = value;
}
public final void set(int x, int y, float value) {
data[getIndex(x, y)] = value;
}
}
```
......@@ -177,8 +169,8 @@ package grassing.util;
public abstract class MathUtil {
public static Point heading(float angle) {
return new Point((float) Math.cos(angle), (float) Math.sin(angle));
public static Vector heading(float angle) {
return new Vector((float) Math.cos(angle), (float) Math.sin(angle));
}
public static float deg2rad(float deg) {
return (float) (deg * Math.PI / 180f);
......@@ -187,14 +179,14 @@ public abstract class MathUtil {
```
```java
/// file:src/main/java/grassing/util/Point.java
/// file:src/main/java/grassing/util/Vector.java
package grassing.util;
public class Point {
public class Vector {
public float x;
public float y;
public Point(float x, float y) {
public Vector(float x, float y) {
this.x = x;
this.y = y;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment