Added an animated main menu

This commit is contained in:
josua 2019-12-05 13:21:46 +11:00
parent 9584f7c882
commit b8d6f2720b
24 changed files with 702 additions and 183 deletions

View File

@ -8,6 +8,7 @@ import projectzombie.audio.AudioSources;
import projectzombie.cheats.Cheats;
import projectzombie.display.DisplayStatsEventHandler;
import projectzombie.display.DisplayWindow;
import projectzombie.display.bossbar.BossBars;
import projectzombie.entity.EntityEventHandler;
import projectzombie.entity.player.EntityPlayer;
import projectzombie.init.Layers;
@ -19,6 +20,7 @@ import projectzombie.input.KeyCallback;
import projectzombie.mainloop.MainloopEventHandler;
import projectzombie.menu.Menu;
import projectzombie.menu.MenuGame;
import projectzombie.menu.MenuMain;
import projectzombie.tiles.LightLevelNoise;
import projectzombie.time.GameTimer;
import projectzombie.time.NoSleep;
@ -29,7 +31,7 @@ public class Main
{
public static MainloopManager mainloop;
public static DisplayWindow window;
public static EntityPlayer player = new EntityPlayer();
public static EntityPlayer player;
public static World world;
public static AudioEngine audio;
public static Random rand = new Random();
@ -37,6 +39,15 @@ public class Main
public static boolean game_paused = false;
public static int tickrate = 10;
public static void respawn()
{
// Reset the world and the player
Layers.init(rand.nextLong());
player = new EntityPlayer();
GameTimer.resetTime();
BossBars.clear();
}
public static void main(String[] args)
{
// Initialize the cheats
@ -74,14 +85,11 @@ public class Main
// Initialize the gamepad
JoystickCallback.JOYSTICK_CALLBACK.init();
// Create the world
Layers.init(rand.nextLong());
// Initialise the entities
mainloop.register(EntityEventHandler.ENTITY_EVENT_HANDLER);
// Start the mainloop
menu = new MenuGame();
menu = new MenuMain();
mainloop.start();
}
}

View File

@ -13,10 +13,23 @@ import projectzombie.util.math.vec.Vec2d;
public class DisplayRenderUI
{
public static boolean showFPS = false;
public static boolean showPos = false;
public static void render()
{
// Get some text settings
Vec2d text_size = new Vec2d(0.5, 0.5);
// Render the fps
if(showFPS) {
GlHelpers.pushMatrix();
GlHelpers.translate2(-9.5, 9.5);
GlHelpers.color3(1, 1, 0);
Text.render("FPS: " + DisplayStatsEventHandler.fps, text_size);
GlHelpers.popMatrix();
}
// Get the player
EntityPlayer player = Main.player;
@ -29,25 +42,17 @@ public class DisplayRenderUI
((double) Main.window.getWidth()) /
((double) Main.window.getHeight()));
// Get some text settings
Vec2d text_size = new Vec2d(0.45, 0.45);
/*// Render the fps
GlHelpers.pushMatrix();
GlHelpers.translate2(-9.5, 9.5);
GlHelpers.color3(1, 1, 0);
Text.render("FPS: " + DisplayStatsEventHandler.fps, text_size);
GlHelpers.popMatrix();*/
if(Main.menu.doGameRender)
if(Main.menu.doGameRender && Main.menu.showIngameGUI)
{
/*// Render the fps and the position
// Render the position
if(showPos) {
GlHelpers.pushMatrix();
GlHelpers.translate2(-9.5, 9);
GlHelpers.color3(1, 1, 0);
Text.render("x: " + (int) player.pos.x + ", y: " + (int) player.pos.y, text_size);
GlHelpers.color3(1, 1, 1);
GlHelpers.popMatrix();*/
GlHelpers.popMatrix();
}
// Render the healthbar
double max_health = player.maxHealth();

View File

@ -7,6 +7,7 @@ public abstract class Menu
public boolean doGameloop;
public boolean doGameRender;
public boolean keepMouse = true;
public boolean showIngameGUI = true;
public Input input;
public abstract void render();

View File

@ -2,7 +2,8 @@ package projectzombie.menu;
import projectzombie.input.types.InputDeath;
import projectzombie.menu.gui.GUI;
import projectzombie.menu.gui.components.ButtonRespawn;
import projectzombie.menu.gui.components.LabelRespawn;
import projectzombie.menu.gui.components.OverlayBackground;
import projectzombie.text.Text;
import projectzombie.util.gl.GlHelpers;
import projectzombie.util.math.vec.Vec2d;
@ -19,30 +20,12 @@ public class MenuDeath extends Menu
this.gui = new GUI();
this.input = new InputDeath(this.gui);
gui.add(new ButtonRespawn());
gui.add(new OverlayBackground());
gui.add(new LabelRespawn("You Died"));
}
@Override
public void render()
{
// Render the screen darkening of the gameplay
GlHelpers.disableTexture2d();
GlHelpers.color4(0, 0, 0, 0.5);
GlHelpers.begin();
GlHelpers.vertex3(-10, -10, 0);
GlHelpers.vertex3(-10, 10, 0);
GlHelpers.vertex3(10, 10, 0);
GlHelpers.vertex3(10, -10, 0);
GlHelpers.end();
GlHelpers.color4(1, 1, 1, 1);
GlHelpers.enableTexture2d();
// Render some text to say the player is dead
GlHelpers.pushMatrix();
GlHelpers.translate2(0, 2.4);
Text.renderCenter("You Died", new Vec2d(1, 1));
GlHelpers.popMatrix();
public void render() {
gui.render();
}

View File

@ -1,10 +1,12 @@
package projectzombie.menu;
import projectzombie.Main;
import projectzombie.input.types.InputGamePause;
import projectzombie.menu.gui.ButtonGroup;
import projectzombie.menu.gui.GUI;
import projectzombie.menu.gui.components.ButtonRespawn;
import projectzombie.text.Text;
import projectzombie.util.gl.GlHelpers;
import projectzombie.menu.gui.components.ButtonBasic;
import projectzombie.menu.gui.components.LabelRespawn;
import projectzombie.menu.gui.components.OverlayBackground;
import projectzombie.util.math.vec.Vec2d;
public class MenuGamePause extends Menu
@ -19,30 +21,25 @@ public class MenuGamePause extends Menu
this.gui = new GUI();
this.input = new InputGamePause(this.gui);
gui.add(new ButtonRespawn());
gui.add(new OverlayBackground());
gui.add(new LabelRespawn("Game Paused"));
ButtonGroup group = new ButtonGroup();
group.add(new ButtonBasic("Settings", button -> {
Main.menu = new MenuSettings(new MenuGamePause());
}));
group.add(new ButtonBasic("Quit", button -> {
Main.menu = new MenuMain();
}));
group.setPos(new Vec2d(0, -2));
gui.add(group);
}
@Override
public void render()
{
// Render the screen darkening of the gameplay
GlHelpers.disableTexture2d();
GlHelpers.color4(0, 0, 0, 0.5);
GlHelpers.begin();
GlHelpers.vertex3(-10, -10, 0);
GlHelpers.vertex3(-10, 10, 0);
GlHelpers.vertex3(10, 10, 0);
GlHelpers.vertex3(10, -10, 0);
GlHelpers.end();
GlHelpers.color4(1, 1, 1, 1);
GlHelpers.enableTexture2d();
// Render some text to say the game is paused
GlHelpers.pushMatrix();
GlHelpers.translate2(0, 2.4);
Text.renderCenter("Game Paused", new Vec2d(1, 1));
GlHelpers.popMatrix();
public void render() {
gui.render();
}
}

View File

@ -1,7 +1,11 @@
package projectzombie.menu;
import projectzombie.Main;
import projectzombie.input.types.InputGUI;
import projectzombie.menu.gui.ButtonGroup;
import projectzombie.menu.gui.GUI;
import projectzombie.menu.gui.components.ButtonBasic;
import projectzombie.menu.gui.components.LabelMain;
public class MenuMain extends Menu
{
@ -9,10 +13,34 @@ public class MenuMain extends Menu
public MenuMain() {
this.doGameloop = false;
this.doGameRender = false;
this.doGameRender = true;
this.keepMouse = false;
this.showIngameGUI = false;
this.gui = new GUI();
this.input = new InputGUI(gui);
gui.add(new LabelMain());
ButtonGroup group = new ButtonGroup();
group.add(new ButtonBasic("Play", button -> {
Main.respawn();
Main.menu = new MenuGame();
}));
group.add(new ButtonBasic("Settings", button -> {
Main.menu = new MenuSettings(Main.menu);
}));
group.add(new ButtonBasic("Quit", button -> {
Main.mainloop.stop();
}));
gui.add(group);
Main.respawn();
Main.player.dead = true;
}
@Override
@ -20,4 +48,13 @@ public class MenuMain extends Menu
this.gui.render();
}
@Override
public void update() {
super.update();
Main.player.angle += 0.05;
Main.player.angle %= 360;
Main.player.pos.x += 0.005;
}
}

View File

@ -1,60 +0,0 @@
package projectzombie.menu;
import java.util.Random;
import projectzombie.Main;
import projectzombie.display.bossbar.BossBars;
import projectzombie.entity.player.EntityPlayer;
import projectzombie.init.Layers;
import projectzombie.text.Text;
import projectzombie.time.GameTimer;
import projectzombie.util.gl.GlHelpers;
import projectzombie.util.math.vec.Vec2d;
class MenuRespawn
{
private int reset_progress = 0;
private static final Random rand = new Random();
void update(boolean resetting)
{
if(resetting) {
this.reset_progress += 10;
} else if(this.reset_progress > 0) {
this.reset_progress -= 5;
}
if(this.reset_progress >= 1000)
{
// Reset the world and the player
Layers.init(rand.nextLong());
Main.player = new EntityPlayer();
Main.menu = new MenuGame();
Main.game_paused = false;
GameTimer.resetTime();
BossBars.clear();
}
}
void render()
{
GlHelpers.pushMatrix();
GlHelpers.translate2(0, 2.5);
Text.renderCenter("Press and hold fire to respawn", new Vec2d(0.5, 0.5));
GlHelpers.popMatrix();
double p = reset_progress/1000.0*8;
GlHelpers.disableTexture2d();
GlHelpers.color4(1, 1, 0, 1);
GlHelpers.begin();
{
GlHelpers.vertex2(-4.0, -3.2);
GlHelpers.vertex2(-4+p, -3.2);
GlHelpers.vertex2(-4+p, -3.0);
GlHelpers.vertex2(-4.0, -3.0);
}
GlHelpers.end();
GlHelpers.color3(0, 0, 0);
GlHelpers.enableTexture2d();
}
}

View File

@ -0,0 +1,82 @@
package projectzombie.menu;
import projectzombie.Main;
import projectzombie.display.DisplayRenderUI;
import projectzombie.input.types.InputGUI;
import projectzombie.menu.gui.Button;
import projectzombie.menu.gui.ButtonGroup;
import projectzombie.menu.gui.GUI;
import projectzombie.menu.gui.Label;
import projectzombie.menu.gui.components.ButtonBasic;
import projectzombie.menu.gui.components.ButtonSetting;
import projectzombie.menu.gui.components.OverlayBackground;
import projectzombie.util.math.vec.Vec2d;
public class MenuSettings extends Menu
{
private GUI gui;
private Menu menuOld;
public MenuSettings(Menu menuOld) {
this.menuOld = menuOld;
doGameloop = false;
doGameRender = menuOld.doGameRender;
showIngameGUI = menuOld.showIngameGUI;
keepMouse = false;
ButtonGroup group = new ButtonGroup();
group.setPos(new Vec2d(-1, 4));
group.add(new ButtonSetting(DisplayRenderUI.showFPS ? "FPS: On" : "FPS: Off", button -> {
if(DisplayRenderUI.showFPS) {
button.setText("FPS: Off");
} else {
button.setText("FPS: On");
}
DisplayRenderUI.showFPS = !DisplayRenderUI.showFPS;
}));
group.add(new ButtonSetting(DisplayRenderUI.showPos ? "Pos Indicator: On" : "Pos Indicator: Off",
button -> {
if(DisplayRenderUI.showPos) {
button.setText("Pos Indicator: Off");
} else {
button.setText("Pos Indicator: On");
}
DisplayRenderUI.showPos = !DisplayRenderUI.showPos;
}));
gui = new GUI();
input = new InputGUI(gui);
if(doGameRender) {
gui.add(new OverlayBackground());
}
gui.add(group);
Label labelSettings = new Label();
labelSettings.setText("Settings");
labelSettings.setSize(new Vec2d(1, 1));
labelSettings.setPos(new Vec2d(0, 6.8));
gui.add(labelSettings);
gui.add(new ButtonBasic("Back", new Vec2d(0, -8), button -> {
Main.menu = menuOld;
}));
}
@Override
public void render() {
gui.render();
}
@Override
public void update() {
super.update();
this.menuOld.update();
}
}

View File

@ -0,0 +1,5 @@
package projectzombie.menu.gui;
public enum Alignment {
LEFT, CENTRE, RIGHT
}

View File

@ -9,25 +9,55 @@ import projectzombie.util.math.vec.Vec2d;
public class Button implements GUIComponent
{
private Vec2d pos;
private String text;
private Vec2d pos = new Vec2d(0, 0);
private String text = "";
private Alignment alignment = Alignment.CENTRE;
private static Vec2d textSize = new Vec2d(0.5, 0.5);
public static final Vec2d textSize = new Vec2d(0.5, 0.5);
public Button(Vec2d pos, String text) {
public void setPos(Vec2d pos) {
this.pos = pos;
}
public void setText(String text) {
this.text = text;
}
public void setAlign(Alignment alignment) {
this.alignment = alignment;
}
@Override
public void render(Vec2d mousePos) {
GlHelpers.pushMatrix();
GlHelpers.translate2(pos.x, pos.y);
double m = 2.5;
double w = textSize.x * m * 8 / 2;
double w = textSize.x * m * 8;
double h = textSize.x * m / 2;
double w1 = 0;
double w2 = 0;
double wt = 0;
if(alignment == Alignment.LEFT) {
w1 = 0;
w2 = w;
wt = w/2;
}
if(alignment == Alignment.CENTRE) {
w1 = -w/2;
w2 = w/2;
wt = 0;
}
if(alignment == Alignment.RIGHT) {
w1 = -w;
w2 = 0;
wt = -w/2;
}
TextureReference tex;
boolean mouseHover = this.checkMouseHover(mousePos);
if(mouseHover) {
@ -39,15 +69,15 @@ public class Button implements GUIComponent
GlHelpers.color4(1, 1, 1, 1);
GlHelpers.begin();
{
tex.texCoord(0, 0); GlHelpers.vertex2(-w, -h);
tex.texCoord(1, 0); GlHelpers.vertex2( w, -h);
tex.texCoord(1, 1); GlHelpers.vertex2( w, h);
tex.texCoord(0, 1); GlHelpers.vertex2(-w, h);
tex.texCoord(0, 0); GlHelpers.vertex2(w1, -h);
tex.texCoord(1, 0); GlHelpers.vertex2(w2, -h);
tex.texCoord(1, 1); GlHelpers.vertex2(w2, h);
tex.texCoord(0, 1); GlHelpers.vertex2(w1, h);
}
GlHelpers.end();
GlHelpers.translate2(
(-textSize.x * text.length() / 2)/GlHelpers.getAspectRatio(),
(-textSize.x * text.length() / 2 + wt)/GlHelpers.getAspectRatio(),
-textSize.y / 2
);
@ -65,20 +95,42 @@ public class Button implements GUIComponent
public boolean checkMouseHover(Vec2d pos) {
double m = 2.5;
double w = textSize.x * m * 8 / 2 / GlHelpers.getAspectRatio();
double w = textSize.x * m * 8 / GlHelpers.getAspectRatio();
double h = textSize.x * m / 2;
double mx = pos.x / Main.window.getWidth() * 20 - 10;
double my = pos.y / Main.window.getHeight() * 20 - 10;
if(alignment == Alignment.LEFT) {
return (
mx > -w - this.pos.x &&
mx > -this.pos.x &&
mx < w - this.pos.x &&
my > -h - this.pos.y &&
my < h - this.pos.y
);
}
if(alignment == Alignment.CENTRE) {
return (
mx > -w/2 - this.pos.x &&
mx < w/2 - this.pos.x &&
my > -h - this.pos.y &&
my < h - this.pos.y
);
}
if(alignment == Alignment.RIGHT) {
return (
mx > -w - this.pos.x &&
mx < -this.pos.x &&
my > -h - this.pos.y &&
my < h - this.pos.y
);
}
return false;
}
@Override
public void onMouseClick(Vec2d pos) {
}

View File

@ -0,0 +1,56 @@
package projectzombie.menu.gui;
import java.util.ArrayList;
import projectzombie.util.math.vec.Vec2d;
public class ButtonGroup implements GUIComponent, GUIContainer
{
private ArrayList<Button> buttons = new ArrayList<Button>();
private Vec2d pos = new Vec2d(0, 0);
public void setPos(Vec2d pos) {
this.pos = pos;
for(int i=0;i<buttons.size();i++) {
Button b = buttons.get(i);
b.setPos(new Vec2d(pos.x, pos.y + Button.textSize.y * -i * 3));
}
}
@Override
public void add(GUIComponent c) {
if(c instanceof Button) {
Button b = (Button) c;
buttons.add(b);
setPos(pos);
}
}
@Override
public void render(Vec2d mousePos) {
for(Button b : buttons) {
b.render(mousePos);
}
}
@Override
public boolean checkMouseHover(Vec2d pos) {
for(Button b : buttons) {
if(b.checkMouseHover(pos)) {
return true;
}
}
return false;
}
@Override
public void onMouseClick(Vec2d pos) {
for(Button b : buttons) {
if(b.checkMouseHover(pos)) {
b.onMouseClick(pos);
}
}
}
}

View File

@ -0,0 +1,61 @@
package projectzombie.menu.gui;
import projectzombie.text.Text;
import projectzombie.util.gl.GlHelpers;
import projectzombie.util.math.vec.Vec2d;
import projectzombie.util.math.vec.Vec3d;
public class Label implements GUIComponent
{
private Vec2d pos = new Vec2d(0, 0);
private Vec2d size = new Vec2d(0.5, 0.5);
private Vec3d color = new Vec3d(1, 1, 1);
private String text = "";
private Alignment alignment = Alignment.CENTRE;
public void setText(String text) {
this.text = text;
}
public void setPos(Vec2d pos) {
this.pos = pos;
}
public void setSize(Vec2d size) {
this.size = size;
}
public void setColor(Vec3d color) {
this.color = color;
}
public void setAlign(Alignment alignment) {
this.alignment = alignment;
}
@Override
public void render(Vec2d mousePos) {
double a = GlHelpers.getAspectRatio();
GlHelpers.pushMatrix();
GlHelpers.color3(color.x, color.y, color.z);
GlHelpers.translate2(pos.x / a, pos.y - size.y / 2);
if(this.alignment == Alignment.CENTRE) {
GlHelpers.translate2(-size.y * text.length() / 2 / a, 0);
}
if(this.alignment == Alignment.RIGHT) {
GlHelpers.translate2(-size.y * text.length() / a, 0);
}
Text.render(text, size);
GlHelpers.popMatrix();
}
@Override
public boolean checkMouseHover(Vec2d pos) {
return false;
}
@Override
public void onMouseClick(Vec2d pos) {
}
}

View File

@ -0,0 +1,43 @@
package projectzombie.menu.gui;
import projectzombie.util.gl.GlHelpers;
import projectzombie.util.math.vec.Vec2d;
import projectzombie.util.math.vec.Vec3d;
import projectzombie.util.math.vec.Vec4d;
public class Overlay implements GUIComponent
{
Vec4d color = new Vec4d(0, 0, 0, 0);
public void setColor(Vec3d color) {
this.color = new Vec4d(color.x, color.y, color.z, this.color.m);
}
public void setColor(Vec4d color) {
this.color = color;
}
@Override
public void render(Vec2d mousePos) {
GlHelpers.disableTexture2d();
GlHelpers.color4(color.x, color.y, color.z, color.m);
GlHelpers.begin();
GlHelpers.vertex3(-10, -10, 0);
GlHelpers.vertex3(-10, 10, 0);
GlHelpers.vertex3(10, 10, 0);
GlHelpers.vertex3(10, -10, 0);
GlHelpers.end();
GlHelpers.color4(1, 1, 1, 1);
GlHelpers.enableTexture2d();
}
@Override
public boolean checkMouseHover(Vec2d pos) {
return false;
}
@Override
public void onMouseClick(Vec2d pos) {
}
}

View File

@ -0,0 +1,27 @@
package projectzombie.menu.gui.components;
import projectzombie.menu.gui.Button;
import projectzombie.util.math.vec.Vec2d;
public class ButtonBasic extends Button {
private ButtonCallback callback;
public ButtonBasic(String text, ButtonCallback callback) {
setText(text);
this.callback = callback;
}
public ButtonBasic(String text, Vec2d pos, ButtonCallback callback) {
this(text, callback);
this.setPos(pos);
}
@Override
public void onMouseClick(Vec2d pos) {
super.onMouseClick(pos);
callback.onClick(this);
}
}

View File

@ -0,0 +1,7 @@
package projectzombie.menu.gui.components;
import projectzombie.menu.gui.Button;
public interface ButtonCallback {
public void onClick(Button button);
}

View File

@ -1,35 +0,0 @@
package projectzombie.menu.gui.components;
import java.util.Random;
import projectzombie.Main;
import projectzombie.display.bossbar.BossBars;
import projectzombie.entity.player.EntityPlayer;
import projectzombie.init.Layers;
import projectzombie.menu.MenuGame;
import projectzombie.menu.gui.Button;
import projectzombie.time.GameTimer;
import projectzombie.util.math.vec.Vec2d;
public class ButtonRespawn extends Button
{
private static final Random rand = new Random();
public ButtonRespawn() {
super(new Vec2d(0, -2), "Respawn");
}
@Override
public void onMouseClick(Vec2d pos) {
super.onMouseClick(pos);
// Reset the world and the player
Layers.init(rand.nextLong());
Main.player = new EntityPlayer();
Main.menu = new MenuGame();
Main.game_paused = false;
GameTimer.resetTime();
BossBars.clear();
}
}

View File

@ -0,0 +1,16 @@
package projectzombie.menu.gui.components;
import projectzombie.menu.gui.Alignment;
public class ButtonSetting extends ButtonBasic
{
public ButtonSetting(String text, ButtonCallback callback) {
super(text, callback);
this.setAlign(Alignment.RIGHT);
}
}

View File

@ -0,0 +1,13 @@
package projectzombie.menu.gui.components;
import projectzombie.menu.gui.Label;
import projectzombie.util.math.vec.Vec2d;
public class LabelMain extends Label
{
public LabelMain() {
setText("Project Zombie");
setSize(new Vec2d(1.2, 1.2));
setPos(new Vec2d(0, 3.2));
}
}

View File

@ -0,0 +1,15 @@
package projectzombie.menu.gui.components;
import projectzombie.menu.gui.Label;
import projectzombie.util.math.vec.Vec2d;
import projectzombie.util.math.vec.Vec3d;
public class LabelRespawn extends Label
{
public LabelRespawn(String text) {
setText(text);
setPos(new Vec2d(0, 2.4));
setSize(new Vec2d(1, 1));
setColor(new Vec3d(0.92, 0.92, 0.92));
}
}

View File

@ -0,0 +1,11 @@
package projectzombie.menu.gui.components;
import projectzombie.menu.gui.Overlay;
import projectzombie.util.math.vec.Vec4d;
public class OverlayBackground extends Overlay
{
public OverlayBackground() {
setColor(new Vec4d(0, 0, 0, 0.5));
}
}

View File

@ -12,6 +12,10 @@ public class GameTimer implements IMainloopTask
return time;
}
public static void forceTimeUpdate(int amount) {
time += amount;
}
@Override
public boolean MainLoopDelay(long millis) {
return millis > Main.tickrate;

View File

@ -0,0 +1,17 @@
package projectzombie.util.math.range;
public class Range4i
{
public int mx;
public int my;
public int mz;
public int mm;
public Range4i(int mx, int my, int mz, int mm)
{
this.mx = mx;
this.my = my;
this.mz = mz;
this.mm = mm;
}
}

View File

@ -0,0 +1,68 @@
package projectzombie.util.math.vec;
import projectzombie.util.math.MathHelpers;
public class Vec4d
{
public double x;
public double y;
public double z;
public double m;
public Vec4d(double x, double y, double z, double m)
{
this.x = x;
this.y = y;
this.z = z;
this.m = m;
}
public double distance(Vec4d other) {
return Math.sqrt(
MathHelpers.squared(this.x - other.x) +
MathHelpers.squared(this.y - other.y) +
MathHelpers.squared(this.z - other.z) +
MathHelpers.squared(this.m - other.m));
}
public static double distance(Vec4d v1, Vec4d v2) {
return v1.distance(v2);
}
public boolean equal(Vec4d other) {
return x == other.x && y == other.y && z == other.z && m == other.m;
}
public Vec4d add(Vec4d other) {
return new Vec4d(this.x + other.x, this.y + other.y, this.z + other.z, this.m + other.m);
}
public Vec4d subtract(Vec4d other) {
return new Vec4d(this.x - other.x, this.y - other.y, this.z - other.z, this.m - other.m);
}
public Vec4d multiply(Vec4d other) {
return new Vec4d(this.x * other.x, this.y * other.y, this.z * other.z, this.m * other.m);
}
public Vec4d divide(Vec4d other) {
return new Vec4d(this.x / other.x, this.y / other.y, this.z / other.z, this.m / other.m);
}
public Vec4d copy() {
return new Vec4d(x, y, z, m);
}
public double squareDistance(Vec4d other)
{
double dx = MathHelpers.positive(other.x - x);
double dy = MathHelpers.positive(other.y - y);
double dz = MathHelpers.positive(other.z - z);
double dm = MathHelpers.positive(other.m - m);
if(dx > dy) return dx;
if(dy > dz) return dy;
if(dz > dm) return dz;
else return dm;
}
}

View File

@ -0,0 +1,106 @@
package projectzombie.util.math.vec;
import projectzombie.util.math.MathHelpers;
import projectzombie.util.math.range.Range3i;
import projectzombie.util.math.range.Range4i;
public class Vec4i {
public int x;
public int y;
public int z;
public int m;
public Vec4i(int x, int y, int z, int m)
{
this.x = x;
this.y = y;
this.z = z;
this.m = m;
}
public double distance(Vec4i other) {
return Math.sqrt(
MathHelpers.squared(this.x - other.x) +
MathHelpers.squared(this.y - other.y) +
MathHelpers.squared(this.z - other.z) +
MathHelpers.squared(this.m - other.m));
}
public static double distance(Vec4i v1, Vec4i v2) {
return v1.distance(v2);
}
public int getId(Range4i range)
{
int x = MathHelpers.mod(this.x, range.mx);
int y = MathHelpers.mod(this.y, range.my);
int z = MathHelpers.mod(this.z, range.mz);
int m = MathHelpers.mod(this.m, range.mm);
int id = 0;
int mu = 1;
id += x;
mu = range.mx;
id += y*mu;
mu *= range.my;
id += z*mu;
mu *= range.mz;
id += m*mu;
return id;
}
public static Vec4i fromId(Range4i range, int id)
{
int x = MathHelpers.mod(id, range.mx);
id -= x;
id /= range.mx;
int y = MathHelpers.mod(id, range.my);
id -= y;
id /= range.my;
int z = MathHelpers.mod(id, range.mz);
id -= z;
id /= range.mz;
int m = MathHelpers.mod(id, range.mm);
return new Vec4i(x, y, z, m);
}
public boolean equal(Vec4i other) {
return x == other.x && y == other.y && z == other.z;
}
public Vec4i add(Vec4i other) {
return new Vec4i(this.x + other.x, this.y + other.y, this.z + other.z, this.m + other.m);
}
public Vec4i subtract(Vec4i other) {
return new Vec4i(this.x - other.x, this.y - other.y, this.z - other.z, this.m - other.m);
}
public Vec4i multiply(Vec4i other) {
return new Vec4i(this.x * other.x, this.y * other.y, this.z * other.z, this.m * other.m);
}
public Vec4i divide(Vec4i other) {
return new Vec4i(this.x / other.x, this.y / other.y, this.z / other.z, this.m / other.m);
}
public Vec4i copy() {
return new Vec4i(x, y, z, m);
}
public int squareDistance(Vec4i other)
{
int dx = MathHelpers.positive(other.x - x);
int dy = MathHelpers.positive(other.y - y);
int dz = MathHelpers.positive(other.z - z);
int dm = MathHelpers.positive(other.m - m);
if(dx > dy) return dx;
if(dy > dz) return dy;
if(dz > dm) return dz;
else return dm;
}
}