Skip to content

Commit

Permalink
Clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
Ealrann committed May 4, 2020
1 parent 5e1b235 commit 4fa8fab
Show file tree
Hide file tree
Showing 13 changed files with 277 additions and 194 deletions.
2 changes: 2 additions & 0 deletions org.sheepy.vsand/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
requires org.sheepy.lily.vulkan.extra.api;

exports org.sheepy.vsand;
exports org.sheepy.vsand.logic;

exports org.sheepy.vsand.model;
exports org.sheepy.vsand.model.impl;
Expand All @@ -19,6 +20,7 @@
opens org.sheepy.vsand;
opens org.sheepy.vsand.constants;
opens org.sheepy.vsand.draw;
opens org.sheepy.vsand.input;
opens org.sheepy.vsand.loader;
opens org.sheepy.vsand.logic;
opens org.sheepy.vsand.ui;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import org.sheepy.vsand.draw.DrawCircleAdapter;
import org.sheepy.vsand.draw.DrawLineAdapter;
import org.sheepy.vsand.draw.DrawSquareAdapter;
import org.sheepy.vsand.input.InputManager;
import org.sheepy.vsand.input.PrimaryDrawer;
import org.sheepy.vsand.input.SecondaryDrawer;
import org.sheepy.vsand.loader.*;
import org.sheepy.vsand.logic.ApplicationBehaviour;
import org.sheepy.vsand.ui.MaterialSelectorInputProviderAdapter;
Expand All @@ -33,6 +36,9 @@ public List<Class<? extends IExtender>> classifiers()
DrawLineAdapter.class,
ApplicationBehaviour.class,
MaterialSelectorInputProviderAdapter.class,
MaterialCountAdapter.class);
MaterialCountAdapter.class,
InputManager.class,
PrimaryDrawer.class,
SecondaryDrawer.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.sheepy.lily.core.api.LilyLauncher;
import org.sheepy.lily.core.api.resource.IResourceLoader;
import org.sheepy.lily.core.api.util.DebugUtil;
import org.sheepy.vsand.logic.VSandMainLoop;
import org.sheepy.vsand.model.VSandApplication;

import java.io.IOException;
Expand All @@ -14,7 +15,7 @@ public class VSandApplicationLauncher

private static final IResourceLoader resourceLoader = IResourceLoader.INSTANCE;

public static void main(String[] args)
public static void main(String[] args) throws IOException
{
DebugUtil.parseMainArgs(args);
final var application = loadApplication();
Expand All @@ -23,21 +24,13 @@ public static void main(String[] args)
LilyLauncher.launch(application, mainLoop);
}

public static VSandApplication loadApplication()
public static VSandApplication loadApplication() throws IOException
{
final var module = VSandApplicationLauncher.class.getModule();
try
{
final var inputStream = module.getResourceAsStream(APPLICATION_PATH);
final var resource = resourceLoader.loadResource(inputStream);
final var res = (VSandApplication) resource.getContents().get(0);
res.setVersion(APPLICATION_VERSION);
return res;
}
catch (final IOException e)
{
e.printStackTrace();
return null;
}
final var inputStream = module.getResourceAsStream(APPLICATION_PATH);
final var resource = resourceLoader.loadResource(inputStream);
final var res = (VSandApplication) resource.getContents().get(0);
res.setVersion(APPLICATION_VERSION);
return res;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,33 @@
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.sheepy.lily.core.api.LilyLauncher;
import org.sheepy.vsand.logic.VSandMainLoop;
import org.sheepy.vsand.model.DrawCommand;
import org.sheepy.vsand.model.Material;
import org.sheepy.vsand.model.VSandApplication;
import org.sheepy.vsand.model.VSandFactory;

import java.io.IOException;
import java.util.List;

public class VSandBenchmarkLauncher
{
public static final int DEFAULT_ITERATION_COUNT = 3000;

public static void main(String[] args)
public static void main(String[] args) throws IOException
{
final var application = createTestApplication();
final var mainLoop = VSandMainLoop.createBenchmark(application, DEFAULT_ITERATION_COUNT);

LilyLauncher.launch(application, mainLoop);
}

public static VSandApplication createTestApplication()
public static VSandApplication createTestApplication() throws IOException
{
final var application = VSandApplicationLauncher.loadApplication();

// remove UI
EcoreUtil.delete(application.getScene()
.getCompositors()
.get(1));
EcoreUtil.delete(application.getScene().getCompositors().get(1));

final DemoDrawer drawer = new DemoDrawer(application);
drawer.drawLavaPot(200, 400);
Expand Down Expand Up @@ -98,12 +98,7 @@ private static DrawCommand drawCircle(final Material sand, int x, int y, int siz
return draw;
}

private static DrawCommand drawLine(final Material sand,
int x1,
int y1,
int x2,
int y2,
int size)
private static DrawCommand drawLine(final Material sand, int x1, int y1, int x2, int y2, int size)
{
final var draw = VSandFactory.eINSTANCE.createDrawLine();
draw.setMaterial(sand);
Expand All @@ -117,11 +112,7 @@ private static DrawCommand drawLine(final Material sand,

private static Material findMaterial(List<Material> materials, String name)
{
return materials.stream()
.filter(m -> m.getName()
.equals(name))
.findFirst()
.orElse(null);
return materials.stream().filter(m -> m.getName().equals(name)).findFirst().orElse(null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.sheepy.lily.core.api.LilyLauncher;
import org.sheepy.lily.vulkan.model.VulkanEngine;
import org.sheepy.vsand.logic.VSandMainLoop;

import java.io.IOException;

public class VSandHeadlessBenchmarkLauncher
{
public static void main(String[] args)
public static void main(String[] args) throws IOException
{
final var application = VSandBenchmarkLauncher.createTestApplication();
application.setScene(null);
final var vulkanEngine = (VulkanEngine) application.getEngines().get(0);
EcoreUtil.delete(vulkanEngine.getProcesses().get(1));

final var mainLoop = VSandMainLoop.createBenchmark( application,
VSandBenchmarkLauncher.DEFAULT_ITERATION_COUNT);
final var mainLoop = VSandMainLoop.createBenchmark(application, VSandBenchmarkLauncher.DEFAULT_ITERATION_COUNT);

LilyLauncher.launch(application, mainLoop);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.sheepy.vsand.audio;

import org.sheepy.lily.game.api.audio.AudioConfiguration;
import org.sheepy.lily.game.api.audio.IAudioAllocation;
import org.sheepy.lily.game.api.audio.IAudioHandle;
import org.sheepy.vsand.model.Material;

public final class MaterialSoundManager
{
private static final AudioConfiguration audioConfig = new AudioConfiguration.Builder().gain(0.85f).build();

private IAudioHandle audioHandle;

public void start(Material material)
{
final var paintSound = material.getPaintSound();
if (paintSound != null)
{
final var pitch = material.getPitch();
final var soundAdapter = paintSound.allocationHandle(IAudioAllocation.class).get();
audioHandle = soundAdapter.play(audioConfig.builder().pitch(pitch).build());
}
}

public void stop()
{
if (audioHandle != null)
{
audioHandle.end();
audioHandle = null;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,95 +1,63 @@
package org.sheepy.vsand.draw;

import org.joml.Vector2ic;
import org.sheepy.lily.core.api.input.IInputManager;
import org.sheepy.lily.game.api.audio.AudioConfiguration;
import org.sheepy.lily.game.api.audio.IAudioAllocation;
import org.sheepy.lily.game.api.audio.IAudioHandle;
import org.sheepy.vsand.model.DrawCommand;
import org.sheepy.vsand.model.Material;
import org.sheepy.vsand.model.VSandApplication;
import org.sheepy.vsand.model.VSandFactory;
import org.sheepy.vsand.util.BoardUtil;
import org.sheepy.vsand.util.EShapeSize;

public final class DrawManager
{
private static final AudioConfiguration audioConfig = new AudioConfiguration.Builder().gain(0.85f).build();
private Vector2ic previousPosition;
private Material material;
private boolean firstDraw = false;

private final VSandApplication application;
private final IInputManager inputManager;
private boolean wasUsed = false;
private Vector2ic previousCursor;
private IAudioHandle audioHandle;

public DrawManager(VSandApplication application, IInputManager inputManager)
public void start(Material material)
{
this.application = application;
this.inputManager = inputManager;
this.material = material;
firstDraw = true;
}

public void manage(Material material, boolean needDraw)
{
if (needDraw)
{
final var firstDraw = wasUsed == false;
newcreateDrawCommand(material, firstDraw);

if (firstDraw)
{
final var paintSound = material.getPaintSound();
if (paintSound != null)
{
final var pitch = material.getPitch();
final var soundAdapter = paintSound.allocationHandle(IAudioAllocation.class).get();
audioHandle = soundAdapter.play(audioConfig.builder().pitch(pitch).build());
}
}
wasUsed = true;
}
else
{
wasUsed = false;
if (audioHandle != null)
{
audioHandle.end();
audioHandle = null;
}
}
}

private void newcreateDrawCommand(Material material, boolean firstDraw)
public DrawCommand draw(final int brushSize, final Vector2ic position)
{
final DrawCommand newCommand;

final var size = EShapeSize.values()[application.getBrushSize() - 1];
final var cursor = inputManager.getCursorPosition();
final var cursorBoard = BoardUtil.toBoardPosition(cursor, application);
final var size = EShapeSize.values()[brushSize - 1];

if (firstDraw || (previousCursor.x() == cursorBoard.x() && previousCursor.y() == cursorBoard.y()))
if (firstDraw || (previousPosition.x() == position.x() && previousPosition.y() == position.y()))
{
final var command = VSandFactory.eINSTANCE.createDrawCircle();
command.setMaterial(material);
command.setSize(size.getSize());
command.setX(cursorBoard.x());
command.setY(cursorBoard.y());
command.setX(position.x());
command.setY(position.y());

newCommand = command;
firstDraw = false;
}
else
{
final var command = VSandFactory.eINSTANCE.createDrawLine();
command.setMaterial(material);
command.setSize(size.getSize());
command.setX1(previousCursor.x());
command.setY1(previousCursor.y());
command.setX2(cursorBoard.x());
command.setY2(cursorBoard.y());
command.setX1(previousPosition.x());
command.setY1(previousPosition.y());
command.setX2(position.x());
command.setY2(position.y());

newCommand = command;
}

previousCursor = cursorBoard;
application.getDrawQueue().add(newCommand);
previousPosition = position;
return newCommand;
}

public Material getMaterial()
{
return material;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.sheepy.vsand.input;

import org.sheepy.lily.core.api.adapter.IAdapter;
import org.sheepy.lily.core.api.input.IInputManager;
import org.sheepy.vsand.audio.MaterialSoundManager;
import org.sheepy.vsand.draw.DrawManager;
import org.sheepy.vsand.model.Material;
import org.sheepy.vsand.model.VSandApplication;
import org.sheepy.vsand.util.BoardUtil;

abstract class AbstractDrawer implements IAdapter
{
private final MaterialSoundManager soundManager = new MaterialSoundManager();
private final DrawManager drawManager = new DrawManager();
private final VSandApplication application;
private final IInputManager inputManager;

private boolean isDrawing = false;
private boolean drawRequested = false;

public AbstractDrawer(VSandApplication application)
{
this.application = application;
inputManager = IInputManager.get(application).orElseThrow();
inputManager.listen(this::afterPollInputs, IInputManager.Features.AfterPollInputs);
}

private void afterPollInputs()
{
if (drawRequested)
{
final var material = getMaterial(application);
final boolean materialHasChanged = drawManager.getMaterial() != material;
if (isDrawing && materialHasChanged)
{
soundManager.stop();
isDrawing = false;
}

if (!isDrawing)
{
soundManager.start(material);
drawManager.start(material);
isDrawing = true;
}

newDrawCommand();
}
else if (isDrawing)
{
soundManager.stop();
isDrawing = false;
}
}

private void newDrawCommand()
{
final var cursor = inputManager.getCursorPosition();
final var cursorBoard = BoardUtil.toBoardPosition(cursor, application);
final var brushSize = application.getBrushSize();
final var drawCommand = drawManager.draw(brushSize, cursorBoard);
application.getDrawQueue().add(drawCommand);
}

protected void setDrawRequested(boolean drawRequested)
{
this.drawRequested = drawRequested;
}

protected abstract Material getMaterial(VSandApplication application);

}
Loading

0 comments on commit 4fa8fab

Please sign in to comment.