From da02a38b6575a380d4607acb9a703db8efb04d2e Mon Sep 17 00:00:00 2001 From: Mayant Mukul Date: Fri, 9 Aug 2019 16:16:12 +0530 Subject: [PATCH] Add TaskOverlay --- assets/textures/beaconIcon.png | Bin 0 -> 624 bytes .../quests/AddBeaconOverlayEvent.java | 33 +++++++ .../quests/DestroyActiveEntityEvent.java | 24 +++++ .../quests/FetchQuestSystem.java | 25 +++-- .../quests/RemoveBeaconOverlayEvent.java | 30 ++++++ .../metalrenegades/quests/TaskOverlay.java | 76 ++++++++++++++- .../quests/TaskOverlaySystem.java | 89 ++++++++++++++++++ 7 files changed, 270 insertions(+), 7 deletions(-) create mode 100644 assets/textures/beaconIcon.png create mode 100644 src/main/java/org/terasology/metalrenegades/quests/AddBeaconOverlayEvent.java create mode 100644 src/main/java/org/terasology/metalrenegades/quests/DestroyActiveEntityEvent.java create mode 100644 src/main/java/org/terasology/metalrenegades/quests/RemoveBeaconOverlayEvent.java create mode 100644 src/main/java/org/terasology/metalrenegades/quests/TaskOverlaySystem.java diff --git a/assets/textures/beaconIcon.png b/assets/textures/beaconIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..f9071bc983dbd6fe622dece4ea361e47e6fe3f72 GIT binary patch literal 624 zcmV-$0+0QPP)EX>4Tx04R}tkv&MmKp2MKriw)?4t9{@kfAzR5ET(zq>4qbP}&NuI+$Gg2TdB1 z6cv5D_F)!biJ5vLy_kXL__~LW?{`t23_a=v{OB);h=O1CXI!E#CkK zhrno&ve!M{9q8=azcsD?{Q#e9a=G1EcLD$a00v@9M??Un0Hpw>IE!O-00009a7bBm z001r{001r{0eGc9b^rhX2XskIMF-;u2n+=Yq;VA|0000PbVXQnLvL+uWo~o;Lvm$d zbY)~9cWHEJAV*0}P*;Ht7XSbNMoC0LR5;6HV88>OJ$t6cKmafikiYrt*|YzY7{EZO z3q}nX83xc6{Qv*|%>V!Yx8PI5NFX!8@&S{Sl+=H+!vL3NssI3G7ZpK4H`MF^0000< KMNUMnLSTX amounts = new HashMap<>(); @@ -150,6 +154,8 @@ public void onReturnTaskInitiated(StartTaskEvent event, EntityRef entityRef) { activeQuestEntity.destroy(); activeQuestEntity = beacon; } + + localPlayer.getCharacterEntity().send(new AddBeaconOverlayEvent(activeQuestEntity)); } @ReceiveEvent @@ -169,14 +175,21 @@ public void onQuestComplete(QuestCompleteEvent event, EntityRef client) { } inventoryManager.removeItem(character, EntityRef.NULL, item, true, amounts.getOrDefault(ITEM_ID, 0)); - // Destroy the beacon - activeQuestEntity.destroy(); - // Pay the player currencyManagementSystem.changeWallet(REWARD); + // Remove the minmap overlay + localPlayer.getCharacterEntity().send(new RemoveBeaconOverlayEvent(activeQuestEntity)); + // remove the quest questSystem.removeQuest(event.getQuest(), true); + +// activeQuestEntity.destroy(); } } + + @ReceiveEvent + public void onDestroyActiveEntityEvent(DestroyActiveEntityEvent event, EntityRef character) { + activeQuestEntity.destroy(); + } } diff --git a/src/main/java/org/terasology/metalrenegades/quests/RemoveBeaconOverlayEvent.java b/src/main/java/org/terasology/metalrenegades/quests/RemoveBeaconOverlayEvent.java new file mode 100644 index 00000000..3a204641 --- /dev/null +++ b/src/main/java/org/terasology/metalrenegades/quests/RemoveBeaconOverlayEvent.java @@ -0,0 +1,30 @@ +/* + * Copyright 2019 MovingBlocks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.terasology.metalrenegades.quests; + +import org.terasology.entitySystem.entity.EntityRef; +import org.terasology.entitySystem.event.Event; + +/** + * Removes the beacon overlay from the minimap + */ +public class RemoveBeaconOverlayEvent implements Event { + public EntityRef beaconEntity; + + public RemoveBeaconOverlayEvent(EntityRef beaconEntity) { + this.beaconEntity = beaconEntity; + } +} diff --git a/src/main/java/org/terasology/metalrenegades/quests/TaskOverlay.java b/src/main/java/org/terasology/metalrenegades/quests/TaskOverlay.java index c30096fa..1ee3ff80 100644 --- a/src/main/java/org/terasology/metalrenegades/quests/TaskOverlay.java +++ b/src/main/java/org/terasology/metalrenegades/quests/TaskOverlay.java @@ -15,18 +15,92 @@ */ package org.terasology.metalrenegades.quests; +import org.terasology.entitySystem.entity.EntityRef; +import org.terasology.logic.location.LocationComponent; import org.terasology.math.geom.Rect2f; +import org.terasology.math.geom.Rect2fTransformer; +import org.terasology.math.geom.Rect2i; +import org.terasology.math.geom.Vector2f; +import org.terasology.math.geom.Vector2i; +import org.terasology.math.geom.Vector3f; import org.terasology.minimap.overlays.MinimapOverlay; +import org.terasology.rendering.assets.texture.Texture; import org.terasology.rendering.nui.Canvas; +import org.terasology.utilities.Assets; +import java.util.Optional; + +/** + * A minimap overlay to mark the home beacon for the meat quest + */ public class TaskOverlay implements MinimapOverlay { + + private final int ICON_SIZE = 24; + private EntityRef beaconEntity; + + public TaskOverlay(EntityRef beaconEntity) { + this.beaconEntity = beaconEntity; + } + @Override public void render(Canvas canvas, Rect2f worldRect) { - // TODO + Rect2f screenRect = Rect2f.createFromMinAndSize( + new Vector2f(canvas.getRegion().minX(), canvas.getRegion().minY()), + new Vector2f(canvas.getRegion().maxX(), canvas.getRegion().maxY()) + ); + + Rect2fTransformer transformer = new Rect2fTransformer(worldRect, screenRect); + + Vector3f localPosition = beaconEntity.getComponent(LocationComponent.class).getWorldPosition(); + Vector2f mapPoint = transformer.apply(localPosition.x, localPosition.y); + + Vector2i min = clamp(mapPoint, screenRect); + Rect2i region = Rect2i.createFromMinAndSize(min.x, min.y, ICON_SIZE, ICON_SIZE); + + Optional icon = Assets.getTexture("MetalRenegades:beaconIcon"); + icon.ifPresent(texture -> canvas.drawTexture(texture, region)); } @Override public int getZOrder() { return 0; } + + public EntityRef getBeaconEntity() { + return beaconEntity; + } + + /** + * Constrains a point to a specified region. Works like a vector clamp. + * + * @param point: the coordinates of the point to be clamped + * @param box: limits + * @return new clamped coordinates of point + */ + private Vector2i clamp(Vector2f point, Rect2f box) { + float x; + float y; + Rect2f iconRegion = Rect2f.createFromMinAndSize(point.x, point.y, ICON_SIZE, ICON_SIZE); + + if (box.contains(iconRegion)) { + return new Vector2i(point.x, point.y); + } else { + if (iconRegion.maxX() >= box.maxX()) { + x = (int) box.maxX() - ICON_SIZE; + } else if (iconRegion.minX() <= box.minX()) { + x = (int) box.minX(); + } else { + x = point.x; + } + + if (iconRegion.maxY() >= box.maxY()) { + y = (int) box.maxY() - ICON_SIZE; + } else if (iconRegion.minY() <= box.minY()) { + y = (int) box.minY(); + } else { + y = point.y; + } + } + return new Vector2i(x, y); + } } diff --git a/src/main/java/org/terasology/metalrenegades/quests/TaskOverlaySystem.java b/src/main/java/org/terasology/metalrenegades/quests/TaskOverlaySystem.java new file mode 100644 index 00000000..20c981e3 --- /dev/null +++ b/src/main/java/org/terasology/metalrenegades/quests/TaskOverlaySystem.java @@ -0,0 +1,89 @@ +/* + * Copyright 2019 MovingBlocks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.terasology.metalrenegades.quests; + +import org.terasology.entitySystem.entity.EntityManager; +import org.terasology.entitySystem.entity.EntityRef; +import org.terasology.entitySystem.event.ReceiveEvent; +import org.terasology.entitySystem.systems.BaseComponentSystem; +import org.terasology.entitySystem.systems.RegisterMode; +import org.terasology.entitySystem.systems.RegisterSystem; +import org.terasology.logic.players.LocalPlayer; +import org.terasology.logic.players.MinimapSystem; +import org.terasology.network.ClientComponent; +import org.terasology.network.NetworkMode; +import org.terasology.network.NetworkSystem; +import org.terasology.registry.In; + +/** + * Manages the beacon minimap overlay + */ +@RegisterSystem(RegisterMode.CLIENT) +public class TaskOverlaySystem extends BaseComponentSystem { + + @In + private NetworkSystem networkSystem; + + @In + private EntityManager entityManager; + + @In + private MinimapSystem minimapSystem; + + @In + private LocalPlayer localPlayer; + + private TaskOverlay overlay; + private EntityRef clientEntity; + private boolean isOverlayAdded = false; + + @Override + public void initialise() { + if (networkSystem.getMode() == NetworkMode.CLIENT) { + clientEntity = networkSystem.getServer().getClientEntity(); + } + } + + @ReceiveEvent + public void onAddBeaconOverlayEvent(AddBeaconOverlayEvent event, EntityRef character) { + overlay = new TaskOverlay(event.beaconEntity); + + if (networkSystem.getMode() == NetworkMode.NONE) { + minimapSystem.addOverlay(overlay); + isOverlayAdded = true; + } + + if (networkSystem.getMode() == NetworkMode.CLIENT) { + if (clientEntity.getComponent(ClientComponent.class).character.getId() == character.getId() && !isOverlayAdded) { + minimapSystem.addOverlay(overlay); + isOverlayAdded = true; + } + } + + if (networkSystem.getMode() == NetworkMode.DEDICATED_SERVER && !isOverlayAdded) { + if (localPlayer.getCharacterEntity() == character) { + minimapSystem.addOverlay(overlay); + } + } + } + + @ReceiveEvent + public void onRemoveBeaconOverlayEvent(RemoveBeaconOverlayEvent event, EntityRef character) { + minimapSystem.removeOverlay(overlay); + isOverlayAdded = false; + character.send(new DestroyActiveEntityEvent()); + } +}