-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapTest.js
More file actions
140 lines (129 loc) · 3.86 KB
/
mapTest.js
File metadata and controls
140 lines (129 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
let config = {
type: Phaser.AUTO,
width: 1000,
height: 1000,
scene: {
preload: preload,
create: create,
},
physics: {
default: 'arcade',
arcade: {
gravity: { y: 0 },
debug: true,
}
},
fps: {
target: 60,
forceSetTimeOut: true
},
};
let game = new Phaser.Game(config);
let updateInterval = 25000 / 1;
let cowGroup;
let cows = 0;
let gameOver = false;
let initialCows = 12;
let mapGroup;
function preload() {
this.load.image('water', 'assets/water.png');
this.load.image('grass', 'assets/grass.png');
this.load.image('thick_grass', 'assets/thick_grass.png');
this.load.spritesheet('cow', 'assets/1cow.png', { frameWidth: 32, frameHeight: 32 })
}
function create() {
this.physics.world.gravity.y = 0;
this.physics.world.setBounds(0, 0, 1000, 1000);
cowGroup = this.physics.add.group();
mapGroup = this.add.group();
for (let i = 0; i < 10; i++) {
for (let j = 0; j < 10; j++) {
let terrainType = Math.floor(Math.random()*10)
let name;
let type;
let quantity;
let color;
if (terrainType <= 5) {
name = 'light_grass'
type = 'grass'
quantity = 1000;
color = 0x339966;
} else if (terrainType <= 8) {
name = 'thick_grass'
type = 'grass'
quantity = 10000;
color = 0x004d00;
} else {
name = 'water'
type = 'water'
quantity = 10000;
color = 0x6699ff;
}
const square = this.add.rectangle(i * 100, j * 100, 100, 100, color);
square.setOrigin(0, 0);
square.setName(name);
square.setData(type, quantity);
mapGroup.add(square);
}
}
for (let i = 1; i <= initialCows; i++) {
let t = 0
console.log(mapGroup)
let grassSquare = false;
while (!grassSquare) {
startingLocationX = Math.floor(Math.random()*1000);
startingLocationY = Math.floor(Math.random()*1000);
const squareAtLocation = getSquareAt(startingLocationX, startingLocationY)
if (squareAtLocation.name === 'light_grass') {
grassSquare = true;
}
console.log('HERE: ', squareAtLocation.name)
t++
if (t > 300) {
console.log('something went wrong')
return
}
}
console.log('grass found!', startingLocationX, startingLocationY)
createCow(`${cows++}`, startingLocationX, startingLocationY);
}
}
function createCow(id, x, y) {
const cow = cowGroup.create(x, y, 'cow');
cow.setCollideWorldBounds(true);
cow.cowId = id;
cow.food = 8000;
cow.age = 0;
cow.water = 20000;
cow.drinking = false;
cow.movingToWater = false;
cow.knownWater = [];
cow.knownFood = [];
cow.movingToFood = false;
cow.eating = false;
cow.moving = false;
cow.hunger = 0;
cow.thirst = 0;
cow.satisfied = false;
cow.children = 0;
cow.wandering = false;
cow.wanderDistance = 0;
cow.wanderDirection = 0;
cow.waterHeading = [];
cow.waterPicked = false;
cow.foodHeading = [];
cow.foodPicked = false;
cow.closestWater = [];
cow.currentDistance = 0;
}
function getSquareAt(x, y) {
// Iterate through the map squares and find the one that corresponds to the given coordinates
for (let i = 0; i < mapGroup.getChildren().length; i++) {
const square = mapGroup.getChildren()[i];
if (square.getBounds().contains(x, y)) {
return square;
}
}
// Return null if no square is found at the given coordinates
return null;
}