Skip to content

Commit

Permalink
#14 Item rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
plajdo authored May 12, 2022
2 parents 2d2cdeb + 51ab79e commit b5433dd
Show file tree
Hide file tree
Showing 15 changed files with 168 additions and 27 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,4 @@ fabric.properties
.idea/misc.xml
.idea/modules.xml
.idea/vcs.xml
img
.idea/.gitignore
11 changes: 9 additions & 2 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added aseprite/portalgun.aseprite
Binary file not shown.
27 changes: 27 additions & 0 deletions bludisko_rt.iml
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,32 @@
<SOURCES />
</library>
</orderEntry>
<orderEntry type="library" name="jetbrains.annotations" level="project" />
<orderEntry type="module-library" scope="TEST">
<library name="JUnit4">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.13.1/junit-4.13.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library" scope="TEST">
<library name="JUnit5.8.1">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.8.1/junit-jupiter-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.8.1/junit-jupiter-api-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.8.1/junit-platform-commons-1.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.8.1/junit-jupiter-params-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.8.1/junit-jupiter-engine-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.8.1/junit-platform-engine-1.8.1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
Binary file added res/textures/items/portalGun.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 48 additions & 3 deletions src/sk/bytecode/bludisko/rt/game/entities/Player.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
package sk.bytecode.bludisko.rt.game.entities;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import sk.bytecode.bludisko.rt.game.graphics.Camera;
import sk.bytecode.bludisko.rt.game.graphics.DistanceRay;
import sk.bytecode.bludisko.rt.game.input.GameInputManagerDelegate;
import sk.bytecode.bludisko.rt.game.items.Item;
import sk.bytecode.bludisko.rt.game.map.Map;
import sk.bytecode.bludisko.rt.game.map.World;
import sk.bytecode.bludisko.rt.game.math.MathUtils;
import sk.bytecode.bludisko.rt.game.math.Vector2;
import sk.bytecode.bludisko.rt.game.util.NullSafe;

import java.awt.*;
import java.lang.ref.WeakReference;

/**
* A Player object representing the real player. Handles input and moves the camera accordingly.
*/
public class Player extends Entity implements GameInputManagerDelegate {

private Map worldWallMap;
private WeakReference<Camera> camera;
private Rectangle screenSize;

private Camera camera;
private Vector2 movementVector;
private Item heldItem;

private float walkingSpeed = 1.75f;

Expand Down Expand Up @@ -56,15 +65,51 @@ public void setWorld(World world) {
* @param camera Camera to control
*/
public void setCamera(Camera camera) {
this.camera = camera;
this.camera = new WeakReference<>(camera);
}

/**
* Equips an item
* @param item Item to equip
*/
public void equip(@Nullable Item item) {
this.heldItem = item;
}

/**
* @return Currently held item or null
*/
public Item getHeldItem() {
return heldItem;
}

// MARK: - Game loop

@Override
public void tick(float dt) {
move(dt);
camera.bind(this);
NullSafe.acceptWeak(camera, camera -> camera.bind(this));
}

/**
* Draws player overlay - currently held item to current graphics content.
* @param graphics Graphics content to draw on
*/
public void drawItemOverlay(@NotNull Graphics graphics) {
NullSafe.accept(heldItem, heldItem -> {
var texture = heldItem.getOverlay();
var image = texture.asImage();

float resizingRatio = screenSize.height / (float)texture.getHeight();
int scaledWidth = (int) (texture.getWidth() * resizingRatio);
int offset = (screenSize.width - scaledWidth) / 2;

graphics.drawImage(image, offset, 0, scaledWidth, screenSize.height, null);
});
}

public void setItemOverlayScreenSizeInformation(@NotNull Rectangle bounds) {
screenSize = bounds;
}

// MARK: - Input
Expand Down
12 changes: 4 additions & 8 deletions src/sk/bytecode/bludisko/rt/game/graphics/Camera.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package sk.bytecode.bludisko.rt.game.graphics;

import org.jetbrains.annotations.NotNull;
import sk.bytecode.bludisko.rt.game.blocks.Block;
import sk.bytecode.bludisko.rt.game.entities.Entity;
import sk.bytecode.bludisko.rt.game.map.GameMap;
Expand Down Expand Up @@ -62,7 +63,7 @@ public Camera() {
* at the same time, messing up the image.
* @param graphics Graphics object to draw into. Must not be finalized in any way.
*/
public synchronized void draw(Graphics graphics) {
public synchronized void draw(@NotNull Graphics graphics) {
BufferedImage bufferedImage = new BufferedImage(
(int) viewportSize.x,
(int) viewportSize.y,
Expand All @@ -74,13 +75,8 @@ public synchronized void draw(Graphics graphics) {
drawFloor(screenBuffer);
drawWalls(screenBuffer);

int width = (int) (viewportSize.x / Config.Display.DRAWING_QUALITY);
int height = (int) (viewportSize.y / Config.Display.DRAWING_QUALITY);
int imgTopLeftX = (screenSize.width / 2) - (width / 2);
int imgTopLeftY = (screenSize.height / 2) - (height / 2);

graphics.setColor(java.awt.Color.green);
graphics.drawImage(bufferedImage, imgTopLeftX, imgTopLeftY, width, height, null);
graphics.drawImage(bufferedImage, 0, 0, screenSize.width, screenSize.height, null);
graphics.drawString(this.position.toString(), 0, 50);
}

Expand Down Expand Up @@ -233,7 +229,7 @@ public synchronized void rotate(float angleDeg, float pitch) {
}

/**
* Sets the camera to render from an entity's perspective.
* Moves the camera to an entity's perspective.
* @param entity Entity to copy the position data from.
*/
public void bind(Entity entity) {
Expand Down
10 changes: 10 additions & 0 deletions src/sk/bytecode/bludisko/rt/game/graphics/Texture.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,14 @@ public int getHeight() {
return image.getHeight();
}

/**
* Returns BufferedImage from this texture. Texture is stored as
* a BufferedImage internally, thus method has no performance
* overhead.
* @return BufferedImage from this texture.
*/
public BufferedImage asImage() {
return image;
}

}
2 changes: 2 additions & 0 deletions src/sk/bytecode/bludisko/rt/game/graphics/TextureManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ private static void loadTextures() {
loadedTextures[10] = new Texture("wall/wallWindow");
loadedTextures[11] = new Texture("wall/wallWindowMirrored");
loadedTextures[12] = new Texture("wall/doors");

loadedTextures[13] = new Texture("items/portalGun");
}

private static void generateTextures() {
Expand Down
17 changes: 17 additions & 0 deletions src/sk/bytecode/bludisko/rt/game/items/Item.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package sk.bytecode.bludisko.rt.game.items;

import sk.bytecode.bludisko.rt.game.graphics.Texture;

public abstract class Item {

// MARK: - Attributes

protected Texture overlay;

// MARK: - Public

public Texture getOverlay() {
return overlay;
}

}
11 changes: 11 additions & 0 deletions src/sk/bytecode/bludisko/rt/game/items/PortalGun.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package sk.bytecode.bludisko.rt.game.items;

import sk.bytecode.bludisko.rt.game.graphics.TextureManager;

public class PortalGun extends Item {

public PortalGun() {
this.overlay = TextureManager.getTexture(13);
}

}
1 change: 1 addition & 0 deletions src/sk/bytecode/bludisko/rt/game/map/Chamber1.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import sk.bytecode.bludisko.rt.game.blocks.game.Portal;
import sk.bytecode.bludisko.rt.game.entities.Player;
import sk.bytecode.bludisko.rt.game.items.PortalGun;

import javax.swing.JOptionPane;

Expand Down
3 changes: 1 addition & 2 deletions src/sk/bytecode/bludisko/rt/game/map/World.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ public GameMap getMap() {
}

/**
* Sets the reference to a player created by a Screen.
* Reference is weak, so it does not count towards GC.
* Set a weak reference to a player created by a Screen.
* @param playerRef Reference to a player.
*/
public void setPlayer(@Nullable Player playerRef) {
Expand Down
25 changes: 25 additions & 0 deletions src/sk/bytecode/bludisko/rt/game/util/NullSafe.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package sk.bytecode.bludisko.rt.game.util;

import org.jetbrains.annotations.Nullable;

import java.lang.ref.WeakReference;
import java.util.function.Consumer;

public final class NullSafe {

public static <T> void accept(@Nullable T nullable, Consumer<T> consumer) {
if(nullable != null) {
consumer.accept(nullable);
}
}

public static <T> void acceptWeak(@Nullable WeakReference<T> weakNullable, Consumer<T> consumer) {
if(weakNullable != null) {
var nullable = weakNullable.get();
if(nullable != null) {
consumer.accept(nullable);
}
}
}

}
24 changes: 13 additions & 11 deletions src/sk/bytecode/bludisko/rt/game/window/screens/GameScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import sk.bytecode.bludisko.rt.game.graphics.Camera;
import sk.bytecode.bludisko.rt.game.input.GameInputManager;
import sk.bytecode.bludisko.rt.game.input.InputManager;
import sk.bytecode.bludisko.rt.game.items.PortalGun;
import sk.bytecode.bludisko.rt.game.map.Chamber1;
import sk.bytecode.bludisko.rt.game.map.World;
import sk.bytecode.bludisko.rt.game.util.NullSafe;
import sk.bytecode.bludisko.rt.game.window.Window;

import java.awt.Graphics;
Expand Down Expand Up @@ -42,11 +44,13 @@ public GameScreen() {
}

private void setupPlayer() {
this.player = new Player(currentWorld);
this.camera = new Camera();
player = new Player(currentWorld);
camera = new Camera();

this.player.setCamera(this.camera);
this.currentWorld.setPlayer(player);
player.setCamera(camera);
currentWorld.setPlayer(player);

player.equip(new PortalGun());
}

private void setupInput() {
Expand All @@ -65,20 +69,17 @@ public InputManager getInputManager() {
public void screenDidAppear() {
super.screenDidAppear();

Window window = this.window.get();
if(window != null) {
window.setCursorVisible(false);
}
NullSafe.acceptWeak(window, window -> window.setCursorVisible(false));
}

@Override
public void screenDidChangeBounds(Rectangle bounds) {
super.screenDidChangeBounds(bounds);

var window = this.window.get();
if(window != null) {
NullSafe.acceptWeak(window, window -> {
camera.setScreenSize(window.canvasBounds());
}
player.setItemOverlayScreenSizeInformation(window.canvasBounds());
});
}

// MARK: - Game loop
Expand All @@ -93,6 +94,7 @@ public void tick(float dt) {
@Override
public void draw(Graphics graphics) {
camera.draw(graphics);
player.drawItemOverlay(graphics);
}

// MARK: - Public
Expand Down

0 comments on commit b5433dd

Please sign in to comment.