new Inventory
This commit is contained in:
parent
9d9a70a12b
commit
5d66d4147b
|
|
@ -148,34 +148,35 @@ class FactoryInventory {
|
||||||
}
|
}
|
||||||
|
|
||||||
countOf(id) {
|
countOf(id) {
|
||||||
var n = 0
|
if (this.items.indexOf(id) != -1)
|
||||||
for (var i = 0; i < this.items.length; i++) {
|
return this.itemcount[this.items.indexOf(id)]
|
||||||
if (this.items[i].id == id)
|
else
|
||||||
n++
|
return 0
|
||||||
}
|
|
||||||
return n
|
|
||||||
}
|
}
|
||||||
|
|
||||||
addItem(item) {
|
addItem(item) {
|
||||||
|
var id = item
|
||||||
if (item instanceof 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) {
|
take(id, count) {
|
||||||
if (this.countOf(id) >= count) {
|
if (this.countOf(id) > count) {
|
||||||
for (var j = 0; j < count; j++) {
|
this.itemcount[this.items.indexOf(id)] = this.itemcount[this.items.indexOf(id)] - count
|
||||||
for (var i = 0; i < this.items.length; i++) {
|
return true;
|
||||||
if (this.items[i].id == id) {
|
} else if (this.countOf(id) == count) {
|
||||||
if (factory != null)
|
this.itemcount.splice(this.items.indexOf(id), 1)
|
||||||
factory.deleteItem(this.items[i])
|
this.items.splice(this.items.indexOf(id), 1)
|
||||||
this.items.splice(i, 1)
|
return true;
|
||||||
break;
|
} else {
|
||||||
}
|
return false;
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
18
js/game.js
18
js/game.js
|
|
@ -65,10 +65,9 @@ function save() {
|
||||||
game.factorysToBuy = factorysToBuy
|
game.factorysToBuy = factorysToBuy
|
||||||
game.factoryRerollPrice = factoryRerollPrice
|
game.factoryRerollPrice = factoryRerollPrice
|
||||||
game.factoryPrice = factoryPrice
|
game.factoryPrice = factoryPrice
|
||||||
game.inventory = []
|
game.inventory = {}
|
||||||
for (var i = 0; i < inventory.items.length; i++) {
|
game.inventory.itemcount = inventory.itemcount
|
||||||
game.inventory.push(inventory.items[i].id)
|
game.inventory.items = inventory.items
|
||||||
}
|
|
||||||
game.factorys = []
|
game.factorys = []
|
||||||
for (var i = 0; i < factorys.length; i++) {
|
for (var i = 0; i < factorys.length; i++) {
|
||||||
game.factorys.push({})
|
game.factorys.push({})
|
||||||
|
|
@ -119,7 +118,7 @@ function loadGameData() {
|
||||||
lastsave = new Date().getTime()
|
lastsave = new Date().getTime()
|
||||||
//Keine Save Vorhanden
|
//Keine Save Vorhanden
|
||||||
factorys.push(new Factory())
|
factorys.push(new Factory())
|
||||||
inventory = new Inventory()
|
inventory = new FactoryInventory()
|
||||||
//game = Cookies.get("game")
|
//game = Cookies.get("game")
|
||||||
$.get("php/playerdata.php", function(data) {
|
$.get("php/playerdata.php", function(data) {
|
||||||
if (JSON.parse(data).money != undefined) {
|
if (JSON.parse(data).money != undefined) {
|
||||||
|
|
@ -135,11 +134,10 @@ function loadGameData() {
|
||||||
factorysToBuy = game.factorysToBuy
|
factorysToBuy = game.factorysToBuy
|
||||||
factoryRerollPrice = game.factoryRerollPrice
|
factoryRerollPrice = game.factoryRerollPrice
|
||||||
factoryPrice = game.factoryPrice
|
factoryPrice = game.factoryPrice
|
||||||
inventory = new Inventory()
|
inventory = new FactoryInventory()
|
||||||
factorys = []
|
factorys = []
|
||||||
for (var i = 0; i < game.inventory.length; i++) {
|
inventory.itemcount = game.inventory.itemcount
|
||||||
inventory.addItem(new Item(game.inventory[i]))
|
inventory.items = game.inventory.items
|
||||||
}
|
|
||||||
for (var i = 0; i < game.factorys.length; i++) {
|
for (var i = 0; i < game.factorys.length; i++) {
|
||||||
factorys.push(new Factory(game.factorys[i].tier))
|
factorys.push(new Factory(game.factorys[i].tier))
|
||||||
for (var x = 0; x < 25; x++) {
|
for (var x = 0; x < 25; x++) {
|
||||||
|
|
@ -329,6 +327,8 @@ function render() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//DRAW INFO BAR
|
//DRAW INFO BAR
|
||||||
|
if (selectedTile == undefined)
|
||||||
|
selectedTile = 0
|
||||||
drawInfoBar()
|
drawInfoBar()
|
||||||
if (selectedTile != 0 && mode != "selectbuilding" && mode != "build") {
|
if (selectedTile != 0 && mode != "selectbuilding" && mode != "build") {
|
||||||
if (selectedTile.hasNoInventory === undefined)
|
if (selectedTile.hasNoInventory === undefined)
|
||||||
|
|
|
||||||
|
|
@ -373,6 +373,8 @@ function buildEvents() {
|
||||||
if (selectedTile.options != undefined) {
|
if (selectedTile.options != undefined) {
|
||||||
//Has Options
|
//Has Options
|
||||||
options()
|
options()
|
||||||
|
} else {
|
||||||
|
$('#options').hide()
|
||||||
}
|
}
|
||||||
} else if (mode != "selectItem") {
|
} else if (mode != "selectItem") {
|
||||||
selectedTile = 0
|
selectedTile = 0
|
||||||
|
|
@ -416,35 +418,36 @@ function buildEvents() {
|
||||||
})
|
})
|
||||||
|
|
||||||
$('body').mouseup(function(e) {
|
$('body').mouseup(function(e) {
|
||||||
if (mode == "delete") {
|
if (isCursorInScreen)
|
||||||
var minX = 0
|
if (mode == "delete") {
|
||||||
var maxX = 0
|
var minX = 0
|
||||||
var minY = 0
|
var maxX = 0
|
||||||
var maxY = 0
|
var minY = 0
|
||||||
if (deleteFromX <= cursorScreenX) {
|
var maxY = 0
|
||||||
minX = deleteFromX
|
if (deleteFromX <= cursorScreenX) {
|
||||||
maxX = cursorScreenX
|
minX = deleteFromX
|
||||||
} else {
|
maxX = cursorScreenX
|
||||||
maxX = deleteFromX
|
} else {
|
||||||
minX = cursorScreenX
|
maxX = deleteFromX
|
||||||
}
|
minX = cursorScreenX
|
||||||
if (deleteFromY <= cursorScreenY) {
|
}
|
||||||
minY = deleteFromY
|
if (deleteFromY <= cursorScreenY) {
|
||||||
maxY = cursorScreenY
|
minY = deleteFromY
|
||||||
} else {
|
maxY = cursorScreenY
|
||||||
maxY = deleteFromY
|
} else {
|
||||||
minY = cursorScreenY
|
maxY = deleteFromY
|
||||||
}
|
minY = cursorScreenY
|
||||||
for (var x = minX; x <= maxX; x++) {
|
}
|
||||||
for (var y = minY; y <= maxY; y++) {
|
for (var x = minX; x <= maxX; x++) {
|
||||||
if (factorys[currentFactory].tiles[x][y] != 0) {
|
for (var y = minY; y <= maxY; y++) {
|
||||||
factorys[currentFactory].tiles[x][y] = 0
|
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)
|
if (e.which == 1)
|
||||||
mousedown = false
|
mousedown = false
|
||||||
if (mode == "move") {
|
if (mode == "move") {
|
||||||
|
|
@ -534,20 +537,23 @@ function sort() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function drawInventory(inventory, title) {
|
function drawInventory(inventory, title) {
|
||||||
if (!(inventory instanceof Inventory))
|
|
||||||
return false;
|
|
||||||
itemId = []
|
itemId = []
|
||||||
itemCount = []
|
itemCount = []
|
||||||
|
|
||||||
for (let item of inventory.items) {
|
if ((inventory instanceof Inventory))
|
||||||
var id = item.id
|
for (let item of inventory.items) {
|
||||||
var index = itemId.indexOf(item.id)
|
var id = item.id
|
||||||
if (index == -1) {
|
var index = itemId.indexOf(item.id)
|
||||||
itemId.push(id)
|
if (index == -1) {
|
||||||
itemCount.push(1)
|
itemId.push(id)
|
||||||
} else {
|
itemCount.push(1)
|
||||||
itemCount[index]++
|
} else {
|
||||||
|
itemCount[index]++
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
if ((inventory instanceof FactoryInventory)) {
|
||||||
|
itemId = inventory.items
|
||||||
|
itemCount = inventory.itemcount
|
||||||
}
|
}
|
||||||
sort()
|
sort()
|
||||||
|
|
||||||
|
|
@ -571,7 +577,7 @@ function drawInventory(inventory, title) {
|
||||||
img.src = "images/items/" + items[itemId[currentIndex]].name + ".png"
|
img.src = "images/items/" + items[itemId[currentIndex]].name + ".png"
|
||||||
inventoryCtx.drawImage(img, 12 + x * 72, 36 + y * 72, 48, 48)
|
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.strokeStyle = "black"
|
||||||
inventoryCtx.lineWidth = 2
|
inventoryCtx.lineWidth = 2
|
||||||
inventoryCtx.strokeText("x" + formattedCount, 4 + x * 72, 91 + y * 72)
|
inventoryCtx.strokeText("x" + formattedCount, 4 + x * 72, 91 + y * 72)
|
||||||
|
|
@ -589,20 +595,14 @@ var itembg = new Image
|
||||||
itembg.src = "images/inventorybg.png"
|
itembg.src = "images/inventorybg.png"
|
||||||
|
|
||||||
function drawBigInventory(inventory) {
|
function drawBigInventory(inventory) {
|
||||||
if (!(inventory instanceof Inventory))
|
if (!(inventory instanceof FactoryInventory))
|
||||||
return false;
|
return false;
|
||||||
itemId = []
|
itemId = []
|
||||||
itemCount = []
|
itemCount = []
|
||||||
|
|
||||||
for (let item of inventory.items) {
|
if ((inventory instanceof FactoryInventory)) {
|
||||||
var id = item.id
|
itemId = inventory.items
|
||||||
var index = itemId.indexOf(item.id)
|
itemCount = inventory.itemcount
|
||||||
if (index == -1) {
|
|
||||||
itemId.push(id)
|
|
||||||
itemCount.push(1)
|
|
||||||
} else {
|
|
||||||
itemCount[index]++
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
sort()
|
sort()
|
||||||
for (var i = 0; i < items.length; i++) {
|
for (var i = 0; i < items.length; i++) {
|
||||||
|
|
@ -618,7 +618,7 @@ function drawBigInventory(inventory) {
|
||||||
itemCtx.drawImage(itembg, 0, 0, 72, 72)
|
itemCtx.drawImage(itembg, 0, 0, 72, 72)
|
||||||
itemCtx.drawImage(img, 12, 12, 48, 48)
|
itemCtx.drawImage(img, 12, 12, 48, 48)
|
||||||
|
|
||||||
var formattedCount = formatCount(itemCount[currentIndex])
|
var formattedCount = formatItemCount(itemCount[currentIndex])
|
||||||
itemCtx.strokeStyle = "black"
|
itemCtx.strokeStyle = "black"
|
||||||
itemCtx.lineWidth = 2
|
itemCtx.lineWidth = 2
|
||||||
itemCtx.strokeText("x" + formattedCount, 4, 67)
|
itemCtx.strokeText("x" + formattedCount, 4, 67)
|
||||||
|
|
|
||||||
|
|
@ -46,6 +46,12 @@ function infoDrawRotatedImage(image, x, y, angle) {
|
||||||
infoCtx.restore();
|
infoCtx.restore();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function formatItemCount(c) {
|
||||||
|
if (c > 1000000)
|
||||||
|
return 1000000
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
function formatCount(c) {
|
function formatCount(c) {
|
||||||
if (c == 0)
|
if (c == 0)
|
||||||
return 0
|
return 0
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue