Skip to content

Commit

Permalink
Add TaskOverlay
Browse files Browse the repository at this point in the history
  • Loading branch information
mayant15 committed Aug 9, 2019
1 parent f9b336f commit da02a38
Show file tree
Hide file tree
Showing 7 changed files with 270 additions and 7 deletions.
Binary file added assets/textures/beaconIcon.png
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
@@ -0,0 +1,33 @@
/*
* 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;

/**
* Adds the beacon overlay to the minimap
*/
public class AddBeaconOverlayEvent implements Event {
public EntityRef beaconEntity;

public AddBeaconOverlayEvent(EntityRef beaconEntity) {
this.beaconEntity = beaconEntity;
}

public AddBeaconOverlayEvent() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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.event.Event;

/**
* Destroys the active quest entity
*/
public class DestroyActiveEntityEvent implements Event {
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
import org.terasology.logic.inventory.InventoryManager;
import org.terasology.logic.location.LocationComponent;
import org.terasology.logic.nameTags.NameTagComponent;
import org.terasology.logic.players.LocalPlayer;
import org.terasology.math.geom.Rect2i;
import org.terasology.math.geom.Vector3f;
import org.terasology.metalrenegades.economy.systems.CurrencyManagementSystem;
import org.terasology.network.ClientComponent;
import org.terasology.protobuf.EntityData;
import org.terasology.registry.In;
import org.terasology.rendering.nui.Color;
import org.terasology.tasks.CollectBlocksTask;
Expand All @@ -47,16 +47,17 @@
import org.terasology.tasks.events.StartTaskEvent;
import org.terasology.tasks.systems.QuestSystem;
import org.terasology.utilities.Assets;
import sun.text.resources.et.FormatData_et;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
* Manages the fetch meat quest
*/
@RegisterSystem(RegisterMode.AUTHORITY)
public class FetchQuestSystem extends BaseComponentSystem {

Expand All @@ -72,6 +73,9 @@ public class FetchQuestSystem extends BaseComponentSystem {
@In
private QuestSystem questSystem;

@In
private LocalPlayer localPlayer;

private EntityRef activeQuestEntity;
private Map<String, Integer> amounts = new HashMap<>();

Expand Down Expand Up @@ -150,6 +154,8 @@ public void onReturnTaskInitiated(StartTaskEvent event, EntityRef entityRef) {
activeQuestEntity.destroy();
activeQuestEntity = beacon;
}

localPlayer.getCharacterEntity().send(new AddBeaconOverlayEvent(activeQuestEntity));
}

@ReceiveEvent
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Texture> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit da02a38

Please sign in to comment.