-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
149 lines (118 loc) · 2.98 KB
/
main.js
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
141
142
143
144
145
146
147
148
149
'use strict'
console.log('main.js loaded')
// System variables
let fps = 60
let oldDate = -new Date()
let fWidth = canvas.width
let fHeight = canvas.height
let hWidth = canvas.width/2
let hHeight = canvas.height/2
let data = []
let grid
const inputs = inputHandler()
var debugMode = 0 // 0 - off, 1 - on
// Game variables
let critterList = []
let foodList = []
let deleteList = []
let deleteListFood = []
let player = new Player(inputs,hWidth,hHeight)
let camera = new Camera(0,0,0.5,fWidth,fHeight)
let maxPopulation = 500
let maxFood = 2000
function screenSetting(){
// SPAWNING
if(debugMode === 0){
// default
for(let i=0;i<500;i++){// 35000, 3000
critterList[i] = new Critter(fWidth*Math.random()*5-fWidth*2,fWidth*Math.random()*5-fWidth*2,0,2)
}
for(let i=0;i<2000;i++){
foodList[i] = new Food(fWidth*Math.random()*5-fWidth*2,fWidth*Math.random()*5-fWidth*2,i)
}
}else{
// debugging
for(let i=0;i<10;i++){// 35000, 3000
critterList[i] = new Critter(fWidth*Math.random(),fHeight*Math.random(),0,1)
}
for(let i=0;i<100;i++){
foodList[i] = new Food(fWidth*Math.random(),fHeight*Math.random(),i)
}
}
}
setup()
function setup(){
screenSetting()
critterList.push(player)
loop()
}
function loop(){
// if(new Date()-oldDate>1000/fps){
if(debugMode === 0){
if(foodList.length < maxFood && Math.random()<.2){
foodList.push(new Food(fWidth*Math.random()*5-fWidth*2,fWidth*Math.random()*5-fWidth*2,foodList.length))
}
}
data.length = 0
if(debugMode === 0){
grid = new Quad(0-fWidth*2,0-fWidth*2,fWidth*5,fWidth*5,1)
}else{
grid = new Quad(0,0,fWidth,fHeight,1)
}
// data.length = 0
// SPATIAL PARTITIONING
// critter
for(let i=critterList.length-1;i>-1;i--){
grid.addInit(critterList[i])
}
// food
for(let i=foodList.length-1;i>-1;i--){
grid.addInit(foodList[i])
}
// UPDATE
// critter
for(let i=critterList.length-1;i>-1;i--){
const critter = critterList[i]
critter.update(i)
// critter.render()
}
// DELETE
if(deleteList.length > 0){
//!@#!@#!@#!@#!@# NEED TO ORDER THIS LIKE THE FOOD !#!@#!@#!@#!@#!@#!@#!@#!#!@#!@#!@#!@#!@#!@#!@#!@#!@#!@#!@#
for(let i=deleteList.length-1;i>-1;i--){ // IMPORTANT TO DO BACKWARDS
critterList.splice(deleteList[i],1)
}
deleteList.length=0
}
if(deleteListFood.length > 0){
for(let i=deleteListFood.length-1;i>-1;i--){ // IMPORTANT TO DO BACKWARDS
foodList.splice(deleteListFood[i],1)
}
deleteListFood.length=0
}
// RENDER
// food
for(let i=foodList.length-1;i>-1;i--){
const food = foodList[i]
food.update(i)
food.render()
}
camera.update(-player.x,player.y)
updateResolution(camera.w,camera.h)
updateOffset(camera.x,camera.y)
// render
clearCanvas()
render()
// oldDate = new Date()
// }
requestAnimationFrame(loop)
}
/*
length = 0
classes?
don't create tons of objects/reuse them
Properties on objects you don't use. REMOVE THEM
Webworkers
avoid global variables => prevents memory leaks
cache
*/