Skip to content

Commit

Permalink
Basic Implementation for Market (#1)
Browse files Browse the repository at this point in the history
* add market screen UI
* add dependency for Inventory to use item widget
* add interaction wrapper
* add test prefab
  • Loading branch information
darshan3 authored and skaldarnar committed Aug 13, 2019
1 parent cbe90ef commit a3c982e
Show file tree
Hide file tree
Showing 8 changed files with 568 additions and 1 deletion.
11 changes: 11 additions & 0 deletions assets/prefabs/test.prefab
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"parent": "engine:iconItem",
"DisplayName": {
"name": "Axe"
},
"Item": {
"icon": "engine:items#axe",
"damageType": "axeDamage"
},
"Purchasable":{}
}
176 changes: 176 additions & 0 deletions assets/ui/marketScreen.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
{
"type": "Market:marketScreen",
"contents": {
"type": "relativeLayout",
"contents": [
{
"type": "UIBox",
"id": "container",
"layoutInfo": {
"width": 1016,
"position-horizontal-center": {},
"height": 500,
"position-vertical-center": {}
}
},
{
"type": "ColumnLayout",
"columns": 2,
"column-widths": [
0.7,
0.3
],
"verticalSpacing": 16,
"horizontalSpacing": 8,
"layoutInfo": {
"width": 1000,
"use-content-height": true,
"position-horizontal-center": {},
"position-top": {
"target": "TOP",
"widget": "container",
"offset": 8
},
"position-bottom": {
"target": "BOTTOM",
"offset": 16,
"widget": "container"
}
},
"contents": [
{
"type": "RelativeLayout",
"contents": [
{
"type": "UILabel",
"id": "marketLabel",
"text": "Available Items",
"layoutInfo": {
"use-content-height": true,
"use-content-width": true,
"position-top": {
"offset": 16,
"target": "TOP",
"widget": "columnsHolder"
},
"position-horizontal-center": {}
}
},
{
"type": "ScrollableArea",
"layoutInfo": {
"position-top": {
"target": "BOTTOM",
"widget": "marketLabel"
},
"position-bottom": {
"target": "BOTTOM"
},
"use-content-width": false,
"use-content-height": false,
"position-left": {
"target": "LEFT"
},
"position-right": {
"target": "RIGHT"
}
},
"contents": [
{
"type": "FlowLayout",
"id": "marketItemList",
"layoutInfo": {
"use-content-height": false,
"use-content-width": false
}
}
]
}
]
},
{
"type": "RelativeLayout",
"contents": [
{
"type": "UILabel",
"id": "itemName",
"layoutInfo": {
"use-content-height": true,
"use-content-width": true,
"position-top": {
"offset": 16,
"target": "TOP",
"widget": "columnsHolder"
},
"position-horizontal-center": {}
}
},
{
"type": "ItemIcon",
"id": "itemDisplayIcon",
"layoutInfo": {
"height": 50,
"weight": 50,
"use-content-width" : false,
"position-horizontal-center": {},
"position-top": {
"offset": 16,
"target": "BOTTOM",
"widget": "itemName"
}
}
},
{
"type": "ScrollableArea",
"id": "itemInfoPlaceholder",
"layoutInfo": {
"position-top": {
"target": "BOTTOM",
"widget": "itemDisplayIcon",
"offset": 16
},
"height": 200
},
"contents": [
{
"type": "UILabel",
"id": "itemDescription",
"layoutInfo": {
"use-content-height": true,
"use-content-width": true
}
}
]
},
{
"type": "RelativeLayout",
"id": "buttonPlaceholder",
"layoutInfo": {
"position-top": {
"widget": "itemInfoPlaceholder",
"target": "BOTTOM"
},
"position-bottom": {
"target": "BOTTOM"
}
},
"contents": [
{
"type": "UIButton",
"id": "buyButton",
"layoutInfo": {
"position-vertical-center": {},
"use-content-height": true
},
"text": "Buy"
}
]
}
]
}
],
"id": "columnsHolder"
}
]
}
}
4 changes: 3 additions & 1 deletion module.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"author" : "darshan3",
"displayName" : "Market",
"description" : "A module to add market where one can buy items in exchange for other items.",
"dependencies" : [],
"dependencies" : [
{"id" : "Inventory", "minVersion" : "1.0.0"}
],
"serverSideOnly" : false
}
26 changes: 26 additions & 0 deletions src/main/java/org/terasology/input/binds/market/MarketButton.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.input.binds.market;

import org.terasology.input.*;

/**
* Button to toggle Market Screen.
*/
@RegisterBindButton(id = "market", description = "Toggle Market", category = "market")
@DefaultBinding(type = InputType.KEY, id = Keyboard.KeyId.Z)
public class MarketButton extends BindButtonEvent {
}
100 changes: 100 additions & 0 deletions src/main/java/org/terasology/logic/market/MarketClientSystem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* 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.logic.market;

import org.terasology.assets.management.AssetManager;
import org.terasology.entitySystem.entity.EntityManager;
import org.terasology.entitySystem.entity.EntityRef;
import org.terasology.entitySystem.event.ReceiveEvent;
import org.terasology.entitySystem.prefab.Prefab;
import org.terasology.entitySystem.systems.BaseComponentSystem;
import org.terasology.entitySystem.systems.RegisterMode;
import org.terasology.entitySystem.systems.RegisterSystem;
import org.terasology.input.ButtonState;
import org.terasology.input.binds.market.MarketButton;
import org.terasology.logic.inventory.ItemComponent;
import org.terasology.network.ClientComponent;
import org.terasology.registry.In;
import org.terasology.registry.Share;
import org.terasology.rendering.nui.NUIManager;
import org.terasology.world.block.Block;
import org.terasology.world.block.BlockExplorer;
import org.terasology.world.block.BlockManager;
import org.terasology.world.block.BlockUri;
import org.terasology.world.block.family.BlockFamily;
import org.terasology.world.block.items.BlockItemFactory;

import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

@RegisterSystem(RegisterMode.CLIENT)
@Share(MarketClientSystem.class)
public class MarketClientSystem extends BaseComponentSystem {
private Set<Prefab> purchasablePrefabs = new HashSet<>();
private Set<Block> purchasableBlocks = new HashSet<>();
private BlockItemFactory blockItemFactory;

@In
private NUIManager nuiManager;
@In
private EntityManager entityManager;
@In
private AssetManager assetManager;
@In
private BlockManager blockManager;


@ReceiveEvent(components = ClientComponent.class)
public void onToggleMarket(MarketButton event, EntityRef entity) {
if (event.getState() == ButtonState.DOWN) {
nuiManager.toggleScreen("Market:marketScreen");
event.consume();
}
}

@Override
public void postBegin() {
blockItemFactory = new BlockItemFactory(entityManager);
BlockExplorer blockExplorer = new BlockExplorer(assetManager);

purchasablePrefabs = assetManager.getLoadedAssets(Prefab.class)
.stream()
.filter(prefab -> prefab.hasComponent(ItemComponent.class)
&& prefab.hasComponent(PurchasableComponent.class))
.collect(Collectors.toSet());

Set<BlockUri> blocks = new HashSet<>();
blocks.addAll(blockManager.listRegisteredBlockUris());
blocks.addAll(blockExplorer.getAvailableBlockFamilies());
blocks.addAll(blockExplorer.getFreeformBlockFamilies());

purchasableBlocks = blocks.stream()
.map(blockManager::getBlockFamily)
.map(BlockFamily::getArchetypeBlock)
.filter(block -> block.getPrefab().isPresent())
.filter(block -> block.getPrefab().get().hasComponent(PurchasableComponent.class))
.collect(Collectors.toSet());
}

public Set<Prefab> getPurchasablePrefabs() {
return purchasablePrefabs;
}

public Set<Block> getPurchasableBlocks() {
return purchasableBlocks;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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.logic.market;

import org.terasology.entitySystem.Component;
import org.terasology.world.block.items.AddToBlockBasedItem;

@AddToBlockBasedItem
public class PurchasableComponent implements Component {
}
Loading

0 comments on commit a3c982e

Please sign in to comment.