Skip to content

Commit

Permalink
Add the ability to pickup/add to inventory icons
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan01252 committed Mar 31, 2017
1 parent e0fee57 commit 3a0af24
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 161 deletions.
27 changes: 14 additions & 13 deletions client/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import {drawChanging} from "./src/draw/draw-changing"

import BuildingFactory from "./src/factories/BuildingFactory";
import BulletFactory from "./src/factories/BulletFactory"
import ItemFactory from "./src/factories/ItemFactory";
import IconFactory from "./src/factories/IconFactory";


import SocketListener from "./src/SocketListener"
import {setupBuildingMenu} from "./src/draw/draw-building-interface";
import {drawBuilding} from "./src/draw/draw-building-interface";
import {CAN_BUILD} from "./src/constants";
import {CANT_BUILD} from "./src/constants";
import {drawItems} from "./src/draw/draw-items";
import {drawItems} from "./src/draw/draw-icons";
import {ITEM_TYPE_TURRET} from "./src/constants";
import {drawPanelInterface} from "./src/draw/draw-panel-interface";

Expand All @@ -45,7 +45,7 @@ var objectContainer = new PIXI.Container();
var panelContainer = new PIXI.Container();
var groundTiles = null;
var backgroundTiles = null;
var itemTiles = null;
var iconTiles = null;

var menuContainer = null;

Expand Down Expand Up @@ -98,7 +98,7 @@ const game = {
game.bulletFactory = new BulletFactory(game);
game.buildingFactory = new BuildingFactory(game);
game.socketListener = new SocketListener(game);
game.itemFactory = new ItemFactory(game);
game.iconFactory = new IconFactory(game);

PIXI.loader
.add([
Expand All @@ -112,8 +112,8 @@ PIXI.loader
"data/imgHealth.png",
"data/imgBuildings.png",
"data/imgBuildIcons.png",
"data/imgIcons.png",
"data/imgitems.png",
"data/imgItems.png",
"data/imgInventorySelection.png",
{url: "data/map.dat", loadType: 1, xhrType: "arraybuffer"}
])
.on("progress", loadProgressHandler)
Expand Down Expand Up @@ -144,8 +144,9 @@ function setup() {
game.textures['health'] = PIXI.utils.TextureCache["data/imgHealth.png"];
game.textures['buildings'] = PIXI.utils.TextureCache["data/imgBuildings.png"];
game.textures['buildingIcons'] = PIXI.utils.TextureCache["data/imgBuildIcons.png"];
game.textures['imageIcons'] = PIXI.utils.TextureCache["data/imgIcons.png"];
game.textures['imageItems'] = PIXI.utils.TextureCache["data/imgitems.png"];
game.textures['imageIcons'] = PIXI.utils.TextureCache["data/imgItems.png"];
game.textures['imageItems'] = PIXI.utils.TextureCache["data/imgItems.png"];
game.textures['imageInventorySelection'] = PIXI.utils.TextureCache["data/imgInventorySelection.png"];


setupKeyboardInputs(game);
Expand All @@ -160,24 +161,24 @@ function setup() {

groundTiles = new PIXI.tilemap.CompositeRectTileLayer(0, game.textures['groundTexture'], true);
backgroundTiles = new PIXI.tilemap.CompositeRectTileLayer(0, null, true);
itemTiles = new PIXI.tilemap.CompositeRectTileLayer(0, game.textures['imageItems'], true);
iconTiles = new PIXI.tilemap.CompositeRectTileLayer(0, game.textures['imageItems'], true);


app.stage.addChild(groundTiles);
app.stage.addChild(backgroundTiles);
app.stage.addChild(itemTiles);
app.stage.addChild(iconTiles);
app.stage.addChild(objectContainer);
app.stage.addChild(panelContainer);


game.itemFactory.newItem(null, 1600, 1800, ITEM_TYPE_TURRET);
game.iconFactory.newIcon(null, 1600, 1800, ITEM_TYPE_TURRET);

setupBuildingMenu(game);

game.forceDraw = true;
drawGround(game, groundTiles);
drawTiles(game, backgroundTiles);
drawItems(game, itemTiles);
drawItems(game, iconTiles);
drawPanelInterface(game, panelContainer);


Expand All @@ -203,7 +204,7 @@ function gameLoop() {
setupBuildingMenu(game);
drawGround(game, groundTiles);
drawTiles(game, backgroundTiles);
drawItems(game, itemTiles);
drawItems(game, iconTiles);
drawChanging(game);
drawBuilding(game);
drawPanelInterface(game, panelContainer);
Expand Down
File renamed without changes
47 changes: 47 additions & 0 deletions client/src/draw/draw-icons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
var getItemsWithingRange = function (iconFactory, player) {

var icon = iconFactory.getHead();
var range = 40 * 48;
var foundItems = [];
while (icon) {

//no one is holding it
if (icon.owner == null) {
if (icon.x > (player.offset.x - range)
&& icon.x < (player.offset.x + range)
&& icon.y > (player.offset.y - range)
&& icon.y < (player.offset.y + range)
) {
foundItems.push(icon)
}
}
icon = icon.next;
}
return foundItems
};

export const drawItems = (game, iconTiles) => {


var offTileX = Math.floor(game.player.offset.x % 32);
var offTileY = Math.floor(game.player.offset.y % 32);


if (game.forceDraw) {
iconTiles.clear();

var foundItems = getItemsWithingRange(game.iconFactory, game.player);
foundItems.forEach((icon, index) => {
console.log(game.player.offset.x - icon.x);
var tmpText = new PIXI.Texture(
game.textures['imageItems'].baseTexture,
new PIXI.Rectangle(icon.type * 32, 0, 32, 32)
);
iconTiles.addFrame(tmpText, icon.x - game.player.offset.x + offTileX, icon.y - game.player.offset.y + offTileY);
});

iconTiles.position.set(game.player.defaultOffset.x + game.player.offset.x - offTileX, game.player.defaultOffset.y + game.player.offset.y - offTileY);
}

iconTiles.pivot.set(game.player.offset.x, game.player.offset.y);
};
47 changes: 0 additions & 47 deletions client/src/draw/draw-items.js

This file was deleted.

45 changes: 34 additions & 11 deletions client/src/draw/draw-panel-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,54 @@ var drawPanel = (game, stage) => {
};

var drawItems = (game, stage) => {
var item = game.itemFactory.getHead();
var icon = game.iconFactory.getHead();


while (item) {
if (item.owner == game.player.id) {
var x = 0;
var y = 0;
while (icon) {

if (icon.owner == game.player.id) {


var tmpText = new PIXI.Texture(
game.textures['imageItems'].baseTexture,
new PIXI.Rectangle(item.type * 32, 0, 32, 32)
new PIXI.Rectangle(icon.type * 32, 0, 32, 32)
);

console.log("item type" + item.type);
var icon = new PIXI.Sprite(tmpText);
var iconSprite = new PIXI.Sprite(tmpText);


switch (item.type) {
switch (icon.type) {
case 9:
icon.x = (game.maxMapX + 7);
icon.y = 372;
x = game.maxMapX + 7;
y = 372;
break;
}
stage.addChild(icon);

if (icon.selected) {
var selected = new PIXI.Sprite(game.textures['imageInventorySelection']);
selected.x = x;
selected.y = y;
stage.addChild(selected);
}

iconSprite.x = x;
iconSprite.y = y;

iconSprite.interactive = true;

const iconClosure = icon;
iconSprite.on('mousedown', (event) => {
event.stopPropagation();
console.log("selecting item")
game.iconFactory.toggleSelected(iconClosure);
game.forceDraw = true;
});
stage.addChild(iconSprite);

}
item = item.next;
icon = icon.next;
}
};

Expand Down
104 changes: 104 additions & 0 deletions client/src/factories/IconFactory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
class IconFactory {

constructor(game) {
this.game = game;
this.iconListHead = null;
}

cycle() {
}

newIcon(owner, x, y, type) {

var icon = {
"owner": owner,
"x": x,
"y": y,
"type": type,
"next": null,
"previous": null

};


if (this.iconListHead) {
this.iconListHead.previous = building;
icon.next = this.iconListHead
}


this.iconListHead = icon;
return icon;
}


pickupIcon() {
var icon = this.findIconByLocation();
if (icon) {
icon.owner = this.game.player.id;
this.game.forceDraw = true;
}
}

/**
*
* @param selectedIcon
*/
toggleSelected(selectedIcon) {
selectedIcon.selected = !selectedIcon.selected;

var icon = this.getHead();
while (icon) {
if (icon.owner == selectedIcon.owner && icon != selectedIcon) {
console.log("setting other icons selected to false");
icon.selected = false;
}
icon = icon.next;
}
}

findIconByLocation() {

var icon = this.getHead();
var range = 24;

while (icon) {

if (icon.x >= (this.game.player.offset.x - range)
&& icon.x <= (this.game.player.offset.x + range)
&& icon.y >= (this.game.player.offset.y - range)
&& icon.y <= (this.game.player.offset.y + range)
) {
return icon;
}

icon = icon.next;
}
return null;
}


deleteIcon(icon) {
var returnIcon = icon.next;

if (icon.next) {
icon.next.previous = icon.previous;
}

if (icon.previous) {
icon.previous.next = icon.next
} else {
this.iconListHead = icon.next;
}

return returnIcon;
}


getHead() {
return this.iconListHead;
}
}


export default IconFactory;
Loading

0 comments on commit 3a0af24

Please sign in to comment.