-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
277 additions
and
194 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
33 changes: 33 additions & 0 deletions
33
org.sheepy.vsand/src/main/java/org/sheepy/vsand/audio/MaterialSoundManager.java
This file contains 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
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; | ||
} | ||
} | ||
} |
78 changes: 23 additions & 55 deletions
78
org.sheepy.vsand/src/main/java/org/sheepy/vsand/draw/DrawManager.java
This file contains 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
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; | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
org.sheepy.vsand/src/main/java/org/sheepy/vsand/input/AbstractDrawer.java
This file contains 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
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); | ||
|
||
} |
Oops, something went wrong.