Skip to content

Commit

Permalink
Add support for in-app notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
BenCheung0422 committed Nov 15, 2023
1 parent 9df4a4f commit 36ab19c
Show file tree
Hide file tree
Showing 22 changed files with 249 additions and 175 deletions.
5 changes: 3 additions & 2 deletions src/client/java/minicraft/core/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import minicraft.core.io.Settings;
import minicraft.core.io.Sound;
import minicraft.entity.mob.Player;
import minicraft.gfx.Screen;
import minicraft.level.Level;
import minicraft.level.tile.Tiles;
import minicraft.network.Analytics;
import minicraft.saveload.Load;
import minicraft.saveload.Version;
import minicraft.screen.Display;
import minicraft.screen.Notification;
import minicraft.screen.ResourcePackDisplay;
import minicraft.screen.TitleDisplay;
import minicraft.util.Logging;
Expand All @@ -30,7 +30,8 @@ protected Game() {} // Can't instantiate the Game class.
public static InputHandler input; // Input used in Game, Player, and just about all the *Menu classes.
public static Player player;

public static List<String> notifications = new ArrayList<>();
public static List<String> inGameNotifications = new ArrayList<>();
public static ArrayDeque<Notification> inAppNotifications = new ArrayDeque<>();

public static int MAX_FPS;

Expand Down
16 changes: 11 additions & 5 deletions src/client/java/minicraft/core/Renderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import minicraft.level.Level;
import minicraft.screen.LoadingDisplay;
import minicraft.screen.Menu;
import minicraft.screen.Notification;
import minicraft.screen.QuestsDisplay;
import minicraft.screen.RelPos;
import minicraft.screen.TutorialDisplayHandler;
Expand Down Expand Up @@ -123,6 +124,11 @@ public static void render() {
if (currentDisplay != null) // Renders menu, if present.
currentDisplay.render(screen);

Notification notification;
if ((notification = inAppNotifications.peek()) != null) {
notification.render(screen);
}

if (!canvas.hasFocus())
renderFocusNagger(); // Calls the renderFocusNagger() method, which creates the "Click to Focus" message.

Expand Down Expand Up @@ -268,18 +274,18 @@ private static void renderGui() {
// NOTIFICATIONS

Updater.updateNoteTick = false;
if (permStatus.size() == 0 && notifications.size() > 0) {
if (permStatus.size() == 0 && inGameNotifications.size() > 0) {
Updater.updateNoteTick = true;
if (notifications.size() > 3) { // Only show 3 notifs max at one time; erase old notifs.
notifications = notifications.subList(notifications.size() - 3, notifications.size());
if (inGameNotifications.size() > 3) { // Only show 3 notifs max at one time; erase old notifs.
inGameNotifications = inGameNotifications.subList(inGameNotifications.size() - 3, inGameNotifications.size());
}

if (Updater.notetick > 180) { // Display time per notification.
notifications.remove(0);
inGameNotifications.remove(0);
Updater.notetick = 0;
}
List<String> print = new ArrayList<>();
for (String n : notifications) {
for (String n : inGameNotifications) {
for (String l : Font.getLines(n, Screen.w, Screen.h, 0))
print.add(l);
}
Expand Down
13 changes: 12 additions & 1 deletion src/client/java/minicraft/core/Updater.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import minicraft.screen.Display;
import minicraft.screen.EndGameDisplay;
import minicraft.screen.LevelTransitionDisplay;
import minicraft.screen.Notification;
import minicraft.screen.PlayerDeathDisplay;
import minicraft.screen.TutorialDisplayHandler;
import minicraft.screen.WorldSelectDisplay;
Expand Down Expand Up @@ -177,6 +178,16 @@ public static void tick() {
}

if (updateNoteTick) notetick++;
Notification notification;
if ((notification = inAppNotifications.peek()) != null) {
boolean refresh = true;
if (notification.isExpired()) {
inAppNotifications.pop(); // Removes
refresh = (notification = inAppNotifications.peek()) != null; // Tries getting new
}

if (refresh) notification.tick();
}

// This is the general action statement thing! Regulates menus, mostly.
if (!Renderer.canvas.hasFocus()) {
Expand Down Expand Up @@ -331,7 +342,7 @@ public static void notifyAll(String msg) {
}
public static void notifyAll(String msg, int notetick) {
msg = Localization.getLocalized(msg);
notifications.add(msg);
inGameNotifications.add(msg);
Updater.notetick = notetick;
}
}
2 changes: 1 addition & 1 deletion src/client/java/minicraft/core/World.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static void resetGame(boolean keepPlayer) {
playerDeadTime = 0;
currentLevel = 3;
Updater.asTick = 0;
Updater.notifications.clear();
Updater.inGameNotifications.clear();

// Adds a new player
if (keepPlayer) {
Expand Down
2 changes: 1 addition & 1 deletion src/client/java/minicraft/entity/furniture/Bed.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static boolean checkCanSleep(Player player) {
// It is too early to sleep; display how much time is remaining.
int sec = (int)Math.ceil((Updater.sleepStartTime - Updater.tickCount)*1.0 / Updater.normSpeed); // gets the seconds until sleeping is allowed. // normSpeed is in tiks/sec.
String note = Localization.getLocalized("minicraft.notification.cannot_sleep", sec / 60, sec % 60);
Game.notifications.add(note); // Add the notification displaying the time remaining in minutes and seconds.
Game.inGameNotifications.add(note); // Add the notification displaying the time remaining in minutes and seconds.

return false;
}
Expand Down
4 changes: 2 additions & 2 deletions src/client/java/minicraft/entity/furniture/DeathChest.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ public void touchedBy(Entity other) {

int returned = playerInv.add(i);
if (returned < total) {
Game.notifications.add("Your inventory is full!");
Game.inGameNotifications.add("Your inventory is full!");
return;
}

inventory.removeItem(i);
}

remove();
Game.notifications.add(Localization.getLocalized("minicraft.notification.death_chest_retrieved"));
Game.inGameNotifications.add(Localization.getLocalized("minicraft.notification.death_chest_retrieved"));
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/client/java/minicraft/entity/furniture/KnightStatue.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ public KnightStatue(int health) {
public boolean interact(Player player, Item heldItem, Direction attackDir) {
if (!ObsidianKnight.active) {
if (touches == 0) { // Touched the first time.
Game.notifications.add(Localization.getLocalized("minicraft.notifications.statue_tapped"));
Game.inGameNotifications.add(Localization.getLocalized("minicraft.notifications.statue_tapped"));
touches++;
} else if (touches == 1) { // Touched the second time.
Game.notifications.add(Localization.getLocalized("minicraft.notifications.statue_touched"));
Game.inGameNotifications.add(Localization.getLocalized("minicraft.notifications.statue_touched"));
touches++;
} else { // Touched the third time.
// Awoken notifications is in Boss class
Expand All @@ -37,7 +37,7 @@ public boolean interact(Player player, Item heldItem, Direction attackDir) {

return true;
} else { // The boss is active.
Game.notifications.add(Localization.getLocalized("minicraft.notification.boss_limit"));
Game.inGameNotifications.add(Localization.getLocalized("minicraft.notification.boss_limit"));
return false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/client/java/minicraft/entity/mob/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ private void goFishing() {
}
if (itemData.startsWith(";")) {
// For secret messages :=)
Game.notifications.add(itemData.substring(1));
Game.inGameNotifications.add(itemData.substring(1));
} else {
if (Items.get(itemData).equals(Items.get("Raw Fish"))) {
AchievementsDisplay.setAchievement("minicraft.achievement.fish",true);
Expand Down
2 changes: 1 addition & 1 deletion src/client/java/minicraft/item/FishingRodItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public boolean interactOn(Tile tile, Level level, int xt, int yt, Player player,
@Override
public boolean isDepleted() {
if (random.nextInt(100) > 120 - uses + level * 6) { // Breaking is random, the lower the level, and the more times you use it, the higher the chance
Game.notifications.add("Your Fishing rod broke.");
Game.inGameNotifications.add("Your Fishing rod broke.");
return true;
}
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/client/java/minicraft/item/PotionType.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public boolean toggleEffect(Player player, boolean addEffect) {

if (playerDepth == 0) {
// player is in overworld
Game.notifications.add("You can't escape from here!");
Game.inGameNotifications.add("You can't escape from here!");
return false;
}

Expand Down
Loading

0 comments on commit 36ab19c

Please sign in to comment.