Skip to content

Commit

Permalink
added masking of ice
Browse files Browse the repository at this point in the history
  • Loading branch information
Slideshow776 committed Jan 23, 2023
1 parent 8551602 commit 4fe9886
Show file tree
Hide file tree
Showing 15 changed files with 195 additions and 29 deletions.
Binary file added source code/assets/images/included/iceTest.ase
Binary file not shown.
Binary file added source code/assets/images/included/iceTest.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 29 additions & 2 deletions source code/assets/images/included/packed/images.pack.atlas
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

images.pack.png
size: 256, 128
size: 256, 256
format: RGBA8888
filter: Nearest, Nearest
repeat: none
Expand All @@ -13,15 +13,42 @@ button
index: -1
button-pressed
rotate: false
xy: 82, 5
xy: 132, 135
size: 78, 78
orig: 78, 78
offset: 0, 0
index: -1
iceTest
rotate: false
xy: 2, 85
size: 128, 128
orig: 128, 128
offset: 0, 0
index: -1
whitePixel
rotate: false
xy: 2, 2
size: 1, 1
orig: 1, 1
offset: 0, 0
index: -1

images.pack2.png
size: 1024, 1024
format: RGBA8888
filter: Nearest, Nearest
repeat: none
water/waterTest0
rotate: false
xy: 2, 364
size: 720, 360
orig: 720, 360
offset: 0, 0
index: -1
water/waterTest1
rotate: false
xy: 2, 2
size: 720, 360
orig: 720, 360
offset: 0, 0
index: -1
Binary file modified source code/assets/images/included/packed/images.pack.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ public class Fish extends BaseActor {
public Fish(float x, float y, Stage stage) {
super(x, y, stage);
loadImage("whitePixel");
setColor(Color.LIGHT_GRAY);
setColor(Color.BLACK);
setSize(50f, 20f);
centerAtPosition(x, y);
setOrigin(Align.center);
setRotation(MathUtils.random(0f, 360f));

setBoundaryRectangle();

Expand Down
16 changes: 16 additions & 0 deletions source code/core/src/no/sandramoen/drawingGame/actors/Ice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package no.sandramoen.drawingGame.actors;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.scenes.scene2d.Stage;

import no.sandramoen.drawingGame.actors.utils.BaseActor;

public class Ice extends BaseActor {
public Ice(float x, float y, Stage stage) {
super(x, y, stage);
loadImage("iceTest");
setColor(Color.LIGHT_GRAY);
setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}
}
23 changes: 23 additions & 0 deletions source code/core/src/no/sandramoen/drawingGame/actors/Water.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package no.sandramoen.drawingGame.actors;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.Array;

import no.sandramoen.drawingGame.actors.utils.BaseActor;
import no.sandramoen.drawingGame.utils.BaseGame;

public class Water extends BaseActor {
private Array<TextureAtlas.AtlasRegion> animationImages = new Array();

public Water(float x, float y, Stage stage) {
super(x, y, stage);
setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

animationImages.add(BaseGame.textureAtlas.findRegion("water/waterTest0"));/*
animationImages.add(BaseGame.textureAtlas.findRegion("water/waterTest1"));*/
animation = new Animation(2f, animationImages, Animation.PlayMode.LOOP);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class BaseActor extends Group {

protected static Rectangle worldBounds;

private Animation<TextureRegion> animation;
public Animation<TextureRegion> animation;
private float animationTime = 0f;
private boolean animationPaused = false;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package no.sandramoen.drawingGame.utils;
package no.sandramoen.drawingGame.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputMultiplexer;
Expand All @@ -10,13 +10,15 @@
import com.badlogic.gdx.utils.viewport.ScreenViewport;

public abstract class BaseScreen implements Screen, InputProcessor {
protected Stage groundStage;
protected Stage mainStage;
protected Stage uiStage;
protected Table uiTable;

private boolean isDebug = false;

public BaseScreen() {
groundStage = new Stage();
groundStage.setViewport(new ScreenViewport());

mainStage = new Stage();
mainStage.setViewport(new ScreenViewport());

Expand All @@ -37,24 +39,33 @@ public BaseScreen() {
public void render(float delta) {
uiStage.act(delta);
mainStage.act(delta);
groundStage.act(delta);
update(delta);

Gdx.gl.glClearColor(0.035f, 0.039f, 0.078f, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

groundStage.getViewport().apply();
drawGroundStage(delta);

mainStage.getViewport().apply();
mainStage.draw();

uiStage.getViewport().apply();
uiStage.draw();
}

public void drawGroundStage(float delta) {
groundStage.draw();
}

@Override
public void show() {
InputMultiplexer im = (InputMultiplexer) Gdx.input.getInputProcessor();
im.addProcessor(this);
im.addProcessor(uiStage);
im.addProcessor(mainStage);
im.addProcessor(groundStage);
}

@Override
Expand All @@ -63,10 +74,12 @@ public void hide() {
im.removeProcessor(this);
im.removeProcessor(uiStage);
im.removeProcessor(mainStage);
im.removeProcessor(groundStage);
}

@Override
public void resize(int width, int height) {
groundStage.getViewport().update(width, height, true);
mainStage.getViewport().update(width, height, true);
uiStage.getViewport().update(width, height, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Polygon;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
Expand All @@ -14,11 +19,13 @@
import com.badlogic.gdx.utils.Array;

import no.sandramoen.drawingGame.actors.Fish;
import no.sandramoen.drawingGame.actors.Ice;
import no.sandramoen.drawingGame.actors.Player;
import no.sandramoen.drawingGame.actors.Water;
import no.sandramoen.drawingGame.actors.utils.BaseActor;
import no.sandramoen.drawingGame.ui.StaminaBar;
import no.sandramoen.drawingGame.utils.BaseGame;
import no.sandramoen.drawingGame.utils.BaseScreen;
import no.sandramoen.drawingGame.screens.BaseScreen;
import no.sandramoen.drawingGame.utils.GameUtils;
import no.sandramoen.drawingGame.utils.ShapeDrawer;

Expand All @@ -39,19 +46,38 @@ public class LevelScreen extends BaseScreen {
private TypingLabel turnLabel;
private StaminaBar staminaBar;

private ShapeRenderer shapeRenderer;
private SpriteBatch spriteBatch;

private BaseActor water;
private BaseActor ice;

@Override
public void initialize() {
touchDownPoint = new Vector2();

shapeDrawer = new ShapeDrawer(mainStage);

ice = new Ice(0, 0, groundStage);
water = new Water(0, 0, groundStage);

fishes = new Array();
for (int i = 0; i < 3; i++)
spawnRandomFish();

player = new Player(Gdx.graphics.getWidth() * .5f, Gdx.graphics.getHeight() * .5f, mainStage);

waterTriangles = new Array();

initializeGUI();

shapeRenderer = new ShapeRenderer();
shapeRenderer.setAutoShapeType(true);
Gdx.gl20.glLineWidth(2);

spriteBatch = new SpriteBatch();

// Gdx.input.setCursorCatched(true);
}

@Override
Expand All @@ -60,9 +86,62 @@ public void update(float delta) {
player.die();
}

@Override
public void drawGroundStage(float delta) {
super.drawGroundStage(delta);
drawMasks();
drawMasked(delta);
}

private void drawMasks() {
/* Clear our depth buffer info from previous frame. */
Gdx.gl.glClear(GL20.GL_DEPTH_BUFFER_BIT);

/* Set the depth function to LESS. */
Gdx.gl.glDepthFunc(GL20.GL_LESS);

/* Enable depth writing. */
Gdx.gl.glEnable(GL20.GL_DEPTH_TEST);

/* Disable RGBA color writing. */
Gdx.gl.glColorMask(false, false, false, false);

/* Render mask elements. */
shapeRenderer.begin();
shapeRenderer.set(ShapeType.Filled);

if (!shapeDrawer.triangles.isEmpty())
for (Polygon polygon : shapeDrawer.triangles)
shapeRenderer.triangle(
polygon.getVertices()[0],
polygon.getVertices()[1],
polygon.getVertices()[2],
polygon.getVertices()[3],
polygon.getVertices()[4],
polygon.getVertices()[5]
);

shapeRenderer.flush();
shapeRenderer.end();
}

private void drawMasked(float delta) {
/* Enable RGBA color writing. */
Gdx.gl.glColorMask(true, true, true, true);

/* Set the depth function to LESS. */
Gdx.gl.glDepthFunc(GL20.GL_LESS);

/* Render masked elements. */
spriteBatch.begin();
spriteBatch.draw(ice.animation.getKeyFrame(delta), 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
spriteBatch.end();
Gdx.gl.glDisable(GL20.GL_DEPTH_TEST);
}

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
Vector3 worldCoordinates = mainStage.getCamera().unproject(new Vector3(screenX, screenY, 0f));
Vector3 worldCoordinates = groundStage.getCamera().unproject(new Vector3(screenX, screenY, 0f));
if (GameUtils.isWithinDistance(
new Vector2(worldCoordinates.x, worldCoordinates.y),
new Vector2(player.getX() + player.getWidth() / 2, player.getY() + player.getHeight() / 2),
Expand All @@ -82,7 +161,7 @@ public boolean touchUp(int screenX, int screenY, int pointer, int button) {

@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
Vector3 worldCoordinates = mainStage.getCamera().unproject(new Vector3(screenX, screenY, 0f));
Vector3 worldCoordinates = groundStage.getCamera().unproject(new Vector3(screenX, screenY, 0f));
if (isPlaying && shapeDrawer.isItPossibleToDrawNewSegment(touchDownPoint, new Vector2(worldCoordinates.x, worldCoordinates.y))) {
boolean isClosedShape = shapeDrawer.drawNewLineSegment(touchDownPoint, new Vector2(worldCoordinates.x, worldCoordinates.y));

Expand Down Expand Up @@ -118,7 +197,7 @@ private void endTurn(boolean isClosedShape) {
shapeDrawer.addCollisionPolygon();
staminaBar.reset();
if (isClosedShape) {
shapeDrawer.drawClosedShape();
shapeDrawer.addTriangles();
collisionDetection();
}
});
Expand Down Expand Up @@ -146,7 +225,7 @@ private void spawnRandomFish() {
fishes.add(new Fish(
MathUtils.random(0, Gdx.graphics.getWidth()),
MathUtils.random(0, Gdx.graphics.getHeight()),
mainStage
groundStage
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;

import no.sandramoen.drawingGame.screens.BaseScreen;
import no.sandramoen.drawingGame.screens.gameplay.LevelScreen;

public abstract class BaseGame extends Game implements AssetErrorListener {
Expand Down
Loading

0 comments on commit 4fe9886

Please sign in to comment.