Skip to content

Commit

Permalink
Resizable item overlay
Browse files Browse the repository at this point in the history
  • Loading branch information
plajdo committed May 12, 2022
1 parent 4d87855 commit 51ab79e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 20 deletions.
6 changes: 2 additions & 4 deletions .idea/workspace.xml

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

28 changes: 20 additions & 8 deletions src/sk/bytecode/bludisko/rt/game/entities/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@
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 @@ -63,7 +65,7 @@ public void setWorld(World world) {
* @param camera Camera to control
*/
public void setCamera(Camera camera) {
this.camera = camera;
this.camera = new WeakReference<>(camera);
}

/**
Expand All @@ -86,18 +88,28 @@ public Item getHeldItem() {
@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) {
if(heldItem != null) {
var overlay = heldItem.getOverlay().toImage();
graphics.drawImage(overlay, 0, 0, 320, 240, null);
}
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
2 changes: 1 addition & 1 deletion src/sk/bytecode/bludisko/rt/game/graphics/Texture.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public int getHeight() {
* overhead.
* @return BufferedImage from this texture.
*/
public BufferedImage toImage() {
public BufferedImage asImage() {
return image;
}

Expand Down
12 changes: 5 additions & 7 deletions src/sk/bytecode/bludisko/rt/game/window/screens/GameScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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 @@ -68,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 Down

0 comments on commit 51ab79e

Please sign in to comment.