Trade-Winds/prefabs/islandPrefab.js

86 lines
2.1 KiB
JavaScript
Raw Normal View History

var ani_island_1 = OS.A.Add("Island 1", 256, 256, {});
2016-04-07 20:26:58 +02:00
function islandPrefab() {}
var pr_island = OS.P.Add("Island", {
solid: true,
imageSrc: "images/island.png",
2016-04-08 08:19:55 +02:00
animations: [ani_island_1],
mapX: 0,
mapY: 0,
mapWidth: 1,
mapHeight: 1,
mapColor: "#00AB00",
2016-04-12 18:02:27 +02:00
canTrade: true,
2016-04-12 06:47:01 +02:00
inventory: [],
2016-04-08 08:19:55 +02:00
priceDifferences: [],
itemsSold: [0, 0, 0, 0, // The more you sell, the lower the price gets
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0],
itemsBought: [0, 0, 0, 0, // The more you buy, the higher the price gets
0, 0, 0, 0,
0, 0, 0, 0,
2016-04-12 06:47:01 +02:00
0, 0, 0, 0]
2016-04-07 20:26:58 +02:00
});
2016-04-08 08:19:55 +02:00
pr_island.DoFirst = function () {
this.GetMapPosition();
2016-04-12 18:02:27 +02:00
this.SetUp();
2016-04-08 08:19:55 +02:00
}
pr_island.GetMapPosition = function () {
this.mapX = (this.x / OS.S.pixelScale) / (OS.camera.width / OS.S.pixelScale);
this.mapY = (this.y / OS.S.pixelScale) / (OS.camera.height / OS.S.pixelScale);
}
2016-04-12 06:47:01 +02:00
pr_island.SetUp = function () {
2016-04-08 08:19:55 +02:00
for (var i = 0; i < 15; i++) {
2016-04-12 06:47:01 +02:00
this.inventory[i] = Math.round(Math.randomRange(0, 20));
2016-04-12 18:02:27 +02:00
this.priceDifferences[i] = Math.round(Math.randomRange(-50, 50));
if (G.economy.cargoItemWorth[i] + this.priceDifferences[i]) {
this.priceDifferences[i] = -G.economy.cargoItemWorth[i] + 1;
}
2016-04-08 08:19:55 +02:00
}
}
pr_island.AdjustPrices = function () {
for (var i = 0; i < 15; i++) {
var saleDifference = this.itemsSold[i] - this.itemsBought[i];
if (saleDifference = 0) {
this.priceDifferences[i] += Math.round(Math.randomRange(-1, 1)) * 5;
} else {
this.priceDifferences[i] += saleDifference * 5;
}
}
}
2016-04-12 18:02:27 +02:00
pr_island.CheckInventory = function () { // Returns an array of indices that have cargo
var indicesWithCargo = [];
for (var i = 0; i < G.inventory.cargo.length; i++) {
if (this.inventory[i] > 0) {
indicesWithCargo.push(i);
}
}
return indicesWithCargo;
}
pr_island.TradeWith = function () {
guiControl.trade.island = this.self;
guiControl.trade.activateDelay = 5;
guiControl.trade.show = true;
}
2016-04-08 08:19:55 +02:00
pr_island.SellTo = function (itemIndex, amount) {
2016-04-12 18:02:27 +02:00
this.inventory[itemIndex] += amount;
2016-04-08 08:19:55 +02:00
this.itemsBought[itemIndex] += amount;
}
pr_island.BuyFrom = function (itemIndex, amount) {
2016-04-12 18:02:27 +02:00
this.inventory[itemIndex] += amount;
2016-04-08 08:19:55 +02:00
this.itemsSold[itemIndex] += amount;
}