new Inventory

This commit is contained in:
MasterGordon 2018-04-29 12:52:17 +02:00
parent 9d9a70a12b
commit 5d66d4147b
4 changed files with 85 additions and 78 deletions

View File

@ -148,34 +148,35 @@ class FactoryInventory {
}
countOf(id) {
var n = 0
for (var i = 0; i < this.items.length; i++) {
if (this.items[i].id == id)
n++
}
return n
if (this.items.indexOf(id) != -1)
return this.itemcount[this.items.indexOf(id)]
else
return 0
}
addItem(item) {
var id = item
if (item instanceof Item)
this.items.push(item)
id = item.id
if (this.items.indexOf(id) == -1) {
this.items.push(id)
this.itemcount.push(1)
} else {
this.itemcount[this.items.indexOf(id)]++
}
}
take(id, count, factory) {
if (this.countOf(id) >= count) {
for (var j = 0; j < count; j++) {
for (var i = 0; i < this.items.length; i++) {
if (this.items[i].id == id) {
if (factory != null)
factory.deleteItem(this.items[i])
this.items.splice(i, 1)
break;
}
}
}
return true
take(id, count) {
if (this.countOf(id) > count) {
this.itemcount[this.items.indexOf(id)] = this.itemcount[this.items.indexOf(id)] - count
return true;
} else if (this.countOf(id) == count) {
this.itemcount.splice(this.items.indexOf(id), 1)
this.items.splice(this.items.indexOf(id), 1)
return true;
} else {
return false;
}
return false
}
}

View File

@ -65,10 +65,9 @@ function save() {
game.factorysToBuy = factorysToBuy
game.factoryRerollPrice = factoryRerollPrice
game.factoryPrice = factoryPrice
game.inventory = []
for (var i = 0; i < inventory.items.length; i++) {
game.inventory.push(inventory.items[i].id)
}
game.inventory = {}
game.inventory.itemcount = inventory.itemcount
game.inventory.items = inventory.items
game.factorys = []
for (var i = 0; i < factorys.length; i++) {
game.factorys.push({})
@ -119,7 +118,7 @@ function loadGameData() {
lastsave = new Date().getTime()
//Keine Save Vorhanden
factorys.push(new Factory())
inventory = new Inventory()
inventory = new FactoryInventory()
//game = Cookies.get("game")
$.get("php/playerdata.php", function(data) {
if (JSON.parse(data).money != undefined) {
@ -135,11 +134,10 @@ function loadGameData() {
factorysToBuy = game.factorysToBuy
factoryRerollPrice = game.factoryRerollPrice
factoryPrice = game.factoryPrice
inventory = new Inventory()
inventory = new FactoryInventory()
factorys = []
for (var i = 0; i < game.inventory.length; i++) {
inventory.addItem(new Item(game.inventory[i]))
}
inventory.itemcount = game.inventory.itemcount
inventory.items = game.inventory.items
for (var i = 0; i < game.factorys.length; i++) {
factorys.push(new Factory(game.factorys[i].tier))
for (var x = 0; x < 25; x++) {
@ -329,6 +327,8 @@ function render() {
}
}
//DRAW INFO BAR
if (selectedTile == undefined)
selectedTile = 0
drawInfoBar()
if (selectedTile != 0 && mode != "selectbuilding" && mode != "build") {
if (selectedTile.hasNoInventory === undefined)

View File

@ -373,6 +373,8 @@ function buildEvents() {
if (selectedTile.options != undefined) {
//Has Options
options()
} else {
$('#options').hide()
}
} else if (mode != "selectItem") {
selectedTile = 0
@ -416,35 +418,36 @@ function buildEvents() {
})
$('body').mouseup(function(e) {
if (mode == "delete") {
var minX = 0
var maxX = 0
var minY = 0
var maxY = 0
if (deleteFromX <= cursorScreenX) {
minX = deleteFromX
maxX = cursorScreenX
} else {
maxX = deleteFromX
minX = cursorScreenX
}
if (deleteFromY <= cursorScreenY) {
minY = deleteFromY
maxY = cursorScreenY
} else {
maxY = deleteFromY
minY = cursorScreenY
}
for (var x = minX; x <= maxX; x++) {
for (var y = minY; y <= maxY; y++) {
if (factorys[currentFactory].tiles[x][y] != 0) {
factorys[currentFactory].tiles[x][y] = 0
if (isCursorInScreen)
if (mode == "delete") {
var minX = 0
var maxX = 0
var minY = 0
var maxY = 0
if (deleteFromX <= cursorScreenX) {
minX = deleteFromX
maxX = cursorScreenX
} else {
maxX = deleteFromX
minX = cursorScreenX
}
if (deleteFromY <= cursorScreenY) {
minY = deleteFromY
maxY = cursorScreenY
} else {
maxY = deleteFromY
minY = cursorScreenY
}
for (var x = minX; x <= maxX; x++) {
for (var y = minY; y <= maxY; y++) {
if (factorys[currentFactory].tiles[x][y] != 0) {
factorys[currentFactory].tiles[x][y] = 0
}
}
}
deleteFromX = -1
deleteFromY = -1
}
deleteFromX = -1
deleteFromY = -1
}
if (e.which == 1)
mousedown = false
if (mode == "move") {
@ -534,20 +537,23 @@ function sort() {
}
function drawInventory(inventory, title) {
if (!(inventory instanceof Inventory))
return false;
itemId = []
itemCount = []
for (let item of inventory.items) {
var id = item.id
var index = itemId.indexOf(item.id)
if (index == -1) {
itemId.push(id)
itemCount.push(1)
} else {
itemCount[index]++
if ((inventory instanceof Inventory))
for (let item of inventory.items) {
var id = item.id
var index = itemId.indexOf(item.id)
if (index == -1) {
itemId.push(id)
itemCount.push(1)
} else {
itemCount[index]++
}
}
if ((inventory instanceof FactoryInventory)) {
itemId = inventory.items
itemCount = inventory.itemcount
}
sort()
@ -571,7 +577,7 @@ function drawInventory(inventory, title) {
img.src = "images/items/" + items[itemId[currentIndex]].name + ".png"
inventoryCtx.drawImage(img, 12 + x * 72, 36 + y * 72, 48, 48)
var formattedCount = formatCount(itemCount[currentIndex])
var formattedCount = formatItemCount(itemCount[currentIndex])
inventoryCtx.strokeStyle = "black"
inventoryCtx.lineWidth = 2
inventoryCtx.strokeText("x" + formattedCount, 4 + x * 72, 91 + y * 72)
@ -589,20 +595,14 @@ var itembg = new Image
itembg.src = "images/inventorybg.png"
function drawBigInventory(inventory) {
if (!(inventory instanceof Inventory))
if (!(inventory instanceof FactoryInventory))
return false;
itemId = []
itemCount = []
for (let item of inventory.items) {
var id = item.id
var index = itemId.indexOf(item.id)
if (index == -1) {
itemId.push(id)
itemCount.push(1)
} else {
itemCount[index]++
}
if ((inventory instanceof FactoryInventory)) {
itemId = inventory.items
itemCount = inventory.itemcount
}
sort()
for (var i = 0; i < items.length; i++) {
@ -618,7 +618,7 @@ function drawBigInventory(inventory) {
itemCtx.drawImage(itembg, 0, 0, 72, 72)
itemCtx.drawImage(img, 12, 12, 48, 48)
var formattedCount = formatCount(itemCount[currentIndex])
var formattedCount = formatItemCount(itemCount[currentIndex])
itemCtx.strokeStyle = "black"
itemCtx.lineWidth = 2
itemCtx.strokeText("x" + formattedCount, 4, 67)

View File

@ -46,6 +46,12 @@ function infoDrawRotatedImage(image, x, y, angle) {
infoCtx.restore();
}
function formatItemCount(c) {
if (c > 1000000)
return 1000000
return c;
}
function formatCount(c) {
if (c == 0)
return 0