webglmusti 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitattributes +2 -0
- package/idea_list.txt +13 -0
- package/index.html +17 -0
- package/package.json +20 -0
- package/public/apple.png +0 -0
- package/public/base.png +0 -0
- package/public/car.png +0 -0
- package/public/drop.png +0 -0
- package/public/font16-Sheet.png +0 -0
- package/public/font16-Sheet.txt +52 -0
- package/public/letters-Sheet.png +0 -0
- package/public/letters-Sheet.txt +27 -0
- package/public/letters_Sheet.png +0 -0
- package/public/letters_Sheet.txt +4 -0
- package/public/vite.svg +1 -0
- package/src/App/ResourceSim/car.ts +172 -0
- package/src/App/ResourceSim/grid.ts +993 -0
- package/src/App/ResourceSim/grid_app.ts +1326 -0
- package/src/App/ResourceSim/grid_test.test.ts +18 -0
- package/src/App/ResourceSim/node_graph.ts +293 -0
- package/src/App/ResourceSim/nodes.ts +151 -0
- package/src/App/ResourceSim/resource.ts +8 -0
- package/src/App/ResourceSim/texts.ts +6 -0
- package/src/App/card/card.test.ts +22 -0
- package/src/App/card/card.ts +763 -0
- package/src/App/puzzle_box/app.ts +10 -0
- package/src/App/puzzle_box/engine.ts +374 -0
- package/src/App/puzzle_box/renderer.ts +102 -0
- package/src/App/puzzle_box/test.test.ts +49 -0
- package/src/App/water/water.ts +206 -0
- package/src/Interface/button.ts +345 -0
- package/src/Interface/interface_element.ts +30 -0
- package/src/Interface/internal_window.ts +100 -0
- package/src/Interface/options.ts +332 -0
- package/src/Interface/text_input.ts +183 -0
- package/src/WebGL/Matrix/matrix.test.ts +30 -0
- package/src/WebGL/Matrix/matrix.ts +293 -0
- package/src/WebGL/Shaders/Fragment/Program/circle.ts +49 -0
- package/src/WebGL/Shaders/Fragment/Program/circle_only.ts +47 -0
- package/src/WebGL/Shaders/Fragment/Program/circle_outline.ts +54 -0
- package/src/WebGL/Shaders/Fragment/Program/colour.ts +39 -0
- package/src/WebGL/Shaders/Fragment/Program/colour_alpha.ts +37 -0
- package/src/WebGL/Shaders/Fragment/Program/line.ts +39 -0
- package/src/WebGL/Shaders/Fragment/Program/multi_colour_centre_circle_path.ts +69 -0
- package/src/WebGL/Shaders/Fragment/Program/multi_colour_path.ts +69 -0
- package/src/WebGL/Shaders/Fragment/Program/path_centre_circle.ts +69 -0
- package/src/WebGL/Shaders/Fragment/Program/rect_outline.ts +39 -0
- package/src/WebGL/Shaders/Fragment/Program/solid_path.ts +64 -0
- package/src/WebGL/Shaders/Fragment/Program/sprite_sheet.ts +54 -0
- package/src/WebGL/Shaders/Fragment/Program/sprite_sheet_colour.ts +62 -0
- package/src/WebGL/Shaders/Fragment/Program/texture.ts +34 -0
- package/src/WebGL/Shaders/Fragment/Source/circle.frag +19 -0
- package/src/WebGL/Shaders/Fragment/Source/circle_only.frag +15 -0
- package/src/WebGL/Shaders/Fragment/Source/circle_outline.frag +23 -0
- package/src/WebGL/Shaders/Fragment/Source/colour.frag +9 -0
- package/src/WebGL/Shaders/Fragment/Source/colour_alpha.frag +7 -0
- package/src/WebGL/Shaders/Fragment/Source/fragment.frag +5 -0
- package/src/WebGL/Shaders/Fragment/Source/fragment_source.ts +15 -0
- package/src/WebGL/Shaders/Fragment/Source/line.frag +12 -0
- package/src/WebGL/Shaders/Fragment/Source/multi_colour_centre_circle_path.frag +36 -0
- package/src/WebGL/Shaders/Fragment/Source/multi_colour_path.frag +34 -0
- package/src/WebGL/Shaders/Fragment/Source/path_centre_circle.frag +29 -0
- package/src/WebGL/Shaders/Fragment/Source/rect_outline.frag +20 -0
- package/src/WebGL/Shaders/Fragment/Source/solid_path.frag +24 -0
- package/src/WebGL/Shaders/Fragment/Source/sprite_sheet.frag +17 -0
- package/src/WebGL/Shaders/Fragment/Source/sprite_sheet_colour.frag +19 -0
- package/src/WebGL/Shaders/Fragment/Source/texture.frag +11 -0
- package/src/WebGL/Shaders/Fragment/fragment.ts +43 -0
- package/src/WebGL/Shaders/Fragment/fragment_old.ts +261 -0
- package/src/WebGL/Shaders/Vertex/Program/mvp2d.ts +41 -0
- package/src/WebGL/Shaders/Vertex/Program/transform2d.ts +39 -0
- package/src/WebGL/Shaders/Vertex/Source/mvp2d.vert +17 -0
- package/src/WebGL/Shaders/Vertex/Source/mvp_i_2d.vert +18 -0
- package/src/WebGL/Shaders/Vertex/Source/simple.vert +9 -0
- package/src/WebGL/Shaders/Vertex/Source/transform2d.vert +13 -0
- package/src/WebGL/Shaders/Vertex/Source/transform2d_rel.vert +16 -0
- package/src/WebGL/Shaders/Vertex/Source/translate2d.vert +7 -0
- package/src/WebGL/Shaders/Vertex/Source/vertex_source.ts +13 -0
- package/src/WebGL/Shaders/Vertex/vertex.ts +10 -0
- package/src/WebGL/Shaders/Vertex/vertex_old.ts +289 -0
- package/src/WebGL/Shaders/custom.ts +129 -0
- package/src/WebGL/Shaders/shader.ts +197 -0
- package/src/WebGL/Shapes/Line.ts +29 -0
- package/src/WebGL/Shapes/Shapes.ts +189 -0
- package/src/WebGL/Texture/texture.ts +214 -0
- package/src/WebGL/Util/file.ts +24 -0
- package/src/WebGL/app.ts +150 -0
- package/src/WebGL/colour.ts +71 -0
- package/src/WebGL/globals.ts +353 -0
- package/src/WebGL/index.ts +3 -0
- package/src/WebGL/mixin.ts +2 -0
- package/src/global.d.ts +13 -0
- package/src/index.ts +4 -0
- package/src/main.ts +139 -0
- package/src/utils/array.ts +154 -0
- package/src/utils/assert.ts +8 -0
- package/src/utils/file.ts +0 -0
- package/src/utils/mixin.ts +22 -0
- package/src/utils/numbers.ts +11 -0
- package/src/utils/utils.test.ts +23 -0
- package/src/vite-env.d.ts +1 -0
- package/tsconfig.json +25 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import {test, expect} from "vitest";
|
|
2
|
+
import * as Grid from "./grid";
|
|
3
|
+
|
|
4
|
+
test("direction", () => {
|
|
5
|
+
//
|
|
6
|
+
const try_left = Grid.DirectionUtil.fromFloatsInGridDecimal(0.1, 0.5);
|
|
7
|
+
expect(try_left).toBe(Grid.DirectionEnum.Left);
|
|
8
|
+
|
|
9
|
+
const try_up = Grid.DirectionUtil.fromFloatsInGridDecimal(0.5, 0.1);
|
|
10
|
+
expect(try_up).toBe(Grid.DirectionEnum.Up);
|
|
11
|
+
|
|
12
|
+
const try_right = Grid.DirectionUtil.fromFloatsInGridDecimal(0.9, 0.5);
|
|
13
|
+
expect(try_right).toBe(Grid.DirectionEnum.Right);
|
|
14
|
+
|
|
15
|
+
const try_down = Grid.DirectionUtil.fromFloatsInGridDecimal(0.5, 0.9);
|
|
16
|
+
expect(try_down).toBe(Grid.DirectionEnum.Down);
|
|
17
|
+
|
|
18
|
+
});
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
import * as Grid from './grid';
|
|
2
|
+
import * as Node from './nodes';
|
|
3
|
+
import * as ArrayUtil from './../../utils/array';
|
|
4
|
+
import { PriorityQueue } from '@datastructures-js/priority-queue';
|
|
5
|
+
|
|
6
|
+
type Int32 = number;
|
|
7
|
+
|
|
8
|
+
class RoadConnection{
|
|
9
|
+
path: Grid.GridPosition[];
|
|
10
|
+
from: Int32;
|
|
11
|
+
to: Int32;
|
|
12
|
+
|
|
13
|
+
constructor(path: Grid.GridPosition[], from: Int32, to: Int32){
|
|
14
|
+
this.path = path;
|
|
15
|
+
this.from = from;
|
|
16
|
+
this.to = to;
|
|
17
|
+
}
|
|
18
|
+
length(): Int32{
|
|
19
|
+
return this.path.length;
|
|
20
|
+
}
|
|
21
|
+
//generateReverse(): RoadConnection{
|
|
22
|
+
// return new RoadConnection([...this.path].reverse());
|
|
23
|
+
//}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
class RoadNode{
|
|
27
|
+
//connections: Map<Int32, RoadConnection>;
|
|
28
|
+
connections: RoadConnection[];
|
|
29
|
+
position: Grid.GridPosition;
|
|
30
|
+
key_node_id: Int32 | undefined;
|
|
31
|
+
id: Int32;
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
constructor(pos: Grid.GridPosition, id: Int32, key_id: Int32 | undefined=undefined){
|
|
35
|
+
//this.connections = new Map();
|
|
36
|
+
this.connections = [];
|
|
37
|
+
this.position = pos;
|
|
38
|
+
this.id = id;
|
|
39
|
+
this.key_node_id = key_id;
|
|
40
|
+
}
|
|
41
|
+
addConnection(conn: RoadConnection){
|
|
42
|
+
this.connections.push(conn);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
static unpackGridPositionPath(conns: RoadConnection[]): Grid.GridPosition[]{
|
|
46
|
+
const positions: Grid.GridPosition[] = [];
|
|
47
|
+
for(const conn of conns){
|
|
48
|
+
for(const pos of conn.path){
|
|
49
|
+
positions.push(pos);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return positions;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export class RoadGraph{
|
|
57
|
+
//nodes in the graphs are either key nodes or road intersection
|
|
58
|
+
|
|
59
|
+
//connections are arrays of grid_positions;
|
|
60
|
+
nodes: RoadNode[];
|
|
61
|
+
key_map: Map<Int32, Int32>;
|
|
62
|
+
|
|
63
|
+
constructor(){
|
|
64
|
+
this.nodes = [];
|
|
65
|
+
this.key_map = new Map();
|
|
66
|
+
}
|
|
67
|
+
generate(grid: Grid.WallGrid, nodes: Map<Int32, Node.KeyNode>){
|
|
68
|
+
this.nodes = [];
|
|
69
|
+
if(nodes.size == 0) return;
|
|
70
|
+
function getKey(x: Int32, y: Int32){
|
|
71
|
+
return y*grid.width + x;
|
|
72
|
+
}
|
|
73
|
+
//this.nodes = Array.from({length: nodes.length}, () => new RoadNode());
|
|
74
|
+
this.nodes = [];
|
|
75
|
+
this.key_map.clear();
|
|
76
|
+
|
|
77
|
+
const node_map: (Int32 | undefined)[] = Array.from({length: grid.width*grid.height}, () => undefined);
|
|
78
|
+
|
|
79
|
+
const road_node_reference: (Int32 | undefined)[] = Array.from({length: grid.width*grid.height}, () => undefined);
|
|
80
|
+
const road_node_directions: Grid.ActiveDirections[] = Array.from(
|
|
81
|
+
{length: grid.width*grid.height}, () => Grid.DirectionUtil.blankActiveDirections()
|
|
82
|
+
);
|
|
83
|
+
//indexes road nodes (NOT key nodes)
|
|
84
|
+
for(const [id, node] of nodes){
|
|
85
|
+
//const node = nodes[i];
|
|
86
|
+
const key = getKey(node.x, node.y);
|
|
87
|
+
node_map[key] = id;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
console.log(node_map);
|
|
91
|
+
|
|
92
|
+
//const last_position: (GridPosition | undefined)
|
|
93
|
+
//start on node 0 and run dfs to either intersection or other nodes
|
|
94
|
+
const first_node = nodes.values().next().value!;
|
|
95
|
+
let road_node_index = 0;
|
|
96
|
+
|
|
97
|
+
const position_queue: Grid.GridPosition[] = [];
|
|
98
|
+
let current_road_node_index = 0;
|
|
99
|
+
position_queue.push(new Grid.GridPosition(first_node.x, first_node.y));
|
|
100
|
+
|
|
101
|
+
const starting_position = new Grid.GridPosition(first_node.x, first_node.y);
|
|
102
|
+
let next_directions = grid.getTileFromPosition(starting_position)!.getDirections();
|
|
103
|
+
const first_id = first_node.getId();
|
|
104
|
+
const start_node = new RoadNode(starting_position.copy(), current_road_node_index, first_id);
|
|
105
|
+
this.nodes.push(start_node);
|
|
106
|
+
this.key_map.set(first_id, road_node_index);
|
|
107
|
+
const start_key = getKey(starting_position.x, starting_position.y);
|
|
108
|
+
road_node_reference[start_key] = current_road_node_index;
|
|
109
|
+
|
|
110
|
+
while(current_road_node_index < position_queue.length && current_road_node_index < 10){
|
|
111
|
+
console.log(`starting n ${current_road_node_index}`);
|
|
112
|
+
const starting_position = position_queue[current_road_node_index];
|
|
113
|
+
const starting_key = getKey(starting_position.x, starting_position.y);
|
|
114
|
+
//let current_node = road_node_reference.get(node_map[getKey(starting_position.x, starting_position.y)]);
|
|
115
|
+
next_directions = grid.getTileFromPosition(starting_position)!.getDirections();
|
|
116
|
+
console.log(next_directions);
|
|
117
|
+
for(const dir of next_directions){
|
|
118
|
+
if(Grid.DirectionUtil.isActiveDirection(road_node_directions[starting_key], dir)){
|
|
119
|
+
//already used edge
|
|
120
|
+
console.log(`repeat edge ${Grid.DirectionUtil.toString(dir)}`);
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
console.log(`running dir: ${Grid.DirectionUtil.toString(dir)}`);
|
|
124
|
+
const position = starting_position.copy();
|
|
125
|
+
//const tile = grid.getTileFromPosition(position)!;
|
|
126
|
+
//const opp = Grid.DirectionUtil.opposite(dir);
|
|
127
|
+
let next_directions = [dir];
|
|
128
|
+
|
|
129
|
+
let is_node = false;
|
|
130
|
+
let times = 0;
|
|
131
|
+
let key = 0;
|
|
132
|
+
const connection: Grid.GridPosition[] = [];
|
|
133
|
+
let last_direction = dir;
|
|
134
|
+
do{
|
|
135
|
+
last_direction = next_directions[0];
|
|
136
|
+
Grid.DirectionUtil.movePosition(next_directions[0], position);
|
|
137
|
+
const tile = grid.getTileFromPosition(position)!;
|
|
138
|
+
const opp = Grid.DirectionUtil.opposite(next_directions[0]);
|
|
139
|
+
next_directions = tile.getDirectionsOtherThan(opp);
|
|
140
|
+
key = getKey(position.x, position.y);
|
|
141
|
+
is_node = node_map[key] != undefined || next_directions.length != 1;
|
|
142
|
+
times++;
|
|
143
|
+
console.log(position);
|
|
144
|
+
connection.push(position.copy());
|
|
145
|
+
}while(!is_node);
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
const current_road_node = this.nodes[current_road_node_index];
|
|
149
|
+
const last_opposite_direction = Grid.DirectionUtil.opposite(last_direction);
|
|
150
|
+
if(road_node_reference[key] == undefined){
|
|
151
|
+
road_node_index++;
|
|
152
|
+
const connected_node = road_node_reference[key] == undefined ? new RoadNode(position, road_node_index, node_map[key]) : this.nodes[road_node_reference[key]!];
|
|
153
|
+
const connected_index = road_node_reference[key] == undefined ? road_node_index : road_node_reference[key]!;
|
|
154
|
+
const road_connection_backwards = new RoadConnection(connection, current_road_node_index, connected_index);
|
|
155
|
+
|
|
156
|
+
const backwards = [...connection];
|
|
157
|
+
backwards.pop();
|
|
158
|
+
ArrayUtil.reverse(backwards);
|
|
159
|
+
backwards.push(current_road_node.position.copy());
|
|
160
|
+
const road_connection = new RoadConnection(backwards, connected_index, current_road_node_index);
|
|
161
|
+
|
|
162
|
+
connected_node.addConnection(road_connection);
|
|
163
|
+
current_road_node.addConnection(road_connection_backwards);
|
|
164
|
+
//connected_node.connections.set(current_road_node_index, road_connection);
|
|
165
|
+
//current_road_node.connections.set(road_node_index, road_connection_backwards);
|
|
166
|
+
|
|
167
|
+
road_node_reference[key] = road_node_index;
|
|
168
|
+
|
|
169
|
+
if(node_map[key] != undefined){
|
|
170
|
+
this.key_map.set(node_map[key]!, road_node_index);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
position_queue.push(position.copy());
|
|
174
|
+
this.nodes.push(connected_node);
|
|
175
|
+
|
|
176
|
+
//setting directions;
|
|
177
|
+
//const last_opposite_direction = Grid.DirectionUtil.opposite(last_direction);
|
|
178
|
+
//Grid.DirectionUtil.setActiveDirection(road_node_directions[key], true, Grid.DirectionUtil.opposite(last_direction));
|
|
179
|
+
//console.log(`added direction to new node ${Grid.DirectionUtil.toString(last_opposite_direction)}`);
|
|
180
|
+
|
|
181
|
+
//Grid.DirectionUtil.setActiveDirection(road_node_directions[starting_key], true, dir);
|
|
182
|
+
//console.log(`added direction to current node ${dir}`);
|
|
183
|
+
|
|
184
|
+
console.log(`position added as key node ${position.x}, ${position.y}`);
|
|
185
|
+
}else{
|
|
186
|
+
console.log(`dir from source added: ${Grid.DirectionUtil.toString(dir)}`);
|
|
187
|
+
const connected_index = road_node_reference[key]!;
|
|
188
|
+
const connected_node = this.nodes[connected_index];
|
|
189
|
+
|
|
190
|
+
console.log(connected_index);
|
|
191
|
+
console.log(connected_node);
|
|
192
|
+
const road_connection_backwards = new RoadConnection(connection, current_road_node_index, connected_index);
|
|
193
|
+
|
|
194
|
+
const backwards = [...connection];
|
|
195
|
+
backwards.pop();
|
|
196
|
+
ArrayUtil.reverse(backwards);
|
|
197
|
+
backwards.push(current_road_node.position.copy());
|
|
198
|
+
const road_connection = new RoadConnection(backwards, connected_index, current_road_node_index);
|
|
199
|
+
//connected_node.connections.set(current_road_node_index, road_connection);
|
|
200
|
+
//current_road_node.connections.set(road_node_index, road_connection_backwards);
|
|
201
|
+
connected_node.addConnection(road_connection);
|
|
202
|
+
current_road_node.addConnection(road_connection_backwards);
|
|
203
|
+
|
|
204
|
+
}
|
|
205
|
+
Grid.DirectionUtil.setActiveDirection(road_node_directions[key], true, Grid.DirectionUtil.opposite(last_direction));
|
|
206
|
+
console.log(`added direction to new node ${Grid.DirectionUtil.toString(last_opposite_direction)}`);
|
|
207
|
+
Grid.DirectionUtil.setActiveDirection(road_node_directions[starting_key], true, dir);
|
|
208
|
+
console.log(`added direction to current node ${dir}`);
|
|
209
|
+
|
|
210
|
+
}
|
|
211
|
+
current_road_node_index++;
|
|
212
|
+
}
|
|
213
|
+
console.log(position_queue);
|
|
214
|
+
console.log(this.nodes);
|
|
215
|
+
console.log(this.key_map);
|
|
216
|
+
}
|
|
217
|
+
shortestPath(from: Int32, to: Int32): RoadConnection[] | undefined{
|
|
218
|
+
type PathTo = {
|
|
219
|
+
node: RoadNode;
|
|
220
|
+
distance: Int32;
|
|
221
|
+
last_connection: RoadConnection | undefined;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const seen = new Set();
|
|
225
|
+
|
|
226
|
+
const from_id = this.key_map.get(from);
|
|
227
|
+
const to_id = this.key_map.get(to);
|
|
228
|
+
if(from_id == undefined || to_id == undefined) return;
|
|
229
|
+
|
|
230
|
+
const from_node = this.nodes[from_id];
|
|
231
|
+
const to_node = this.nodes[to_id];
|
|
232
|
+
|
|
233
|
+
console.log(from_node);
|
|
234
|
+
console.log(to_node);
|
|
235
|
+
|
|
236
|
+
//run bfs until to_node
|
|
237
|
+
|
|
238
|
+
const node_queue = new PriorityQueue<PathTo>((a, b) => {
|
|
239
|
+
return a.distance - b.distance;
|
|
240
|
+
})
|
|
241
|
+
|
|
242
|
+
node_queue.push({node: from_node, distance: 0, last_connection: undefined});
|
|
243
|
+
|
|
244
|
+
const backtrack_map: (RoadConnection | undefined)[] = Array.from({length: this.nodes.length}, () => undefined);
|
|
245
|
+
|
|
246
|
+
while(!node_queue.isEmpty()){
|
|
247
|
+
|
|
248
|
+
const curr = node_queue.pop()!;
|
|
249
|
+
if(seen.has(curr.node.id)){
|
|
250
|
+
//node_id++;
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
backtrack_map[curr.node.id] = curr.last_connection;
|
|
254
|
+
seen.add(curr.node.id);
|
|
255
|
+
if(curr.node.id === to_id){
|
|
256
|
+
const moves = curr.distance;
|
|
257
|
+
|
|
258
|
+
console.log(`found to in ${moves} moves`);
|
|
259
|
+
//run backtrack
|
|
260
|
+
const connections: RoadConnection[] = [];
|
|
261
|
+
let id = curr.node.id;
|
|
262
|
+
let t = 0; // remove after fully tested
|
|
263
|
+
while(backtrack_map[id] != undefined && t < 10){
|
|
264
|
+
console.log(id);
|
|
265
|
+
connections.push(backtrack_map[id]!)
|
|
266
|
+
id = backtrack_map[id]!.from;
|
|
267
|
+
t++;
|
|
268
|
+
}
|
|
269
|
+
ArrayUtil.reverse(connections);
|
|
270
|
+
console.log(connections);
|
|
271
|
+
return connections;
|
|
272
|
+
}
|
|
273
|
+
console.log(curr);
|
|
274
|
+
//console.log(curr.node.connections);
|
|
275
|
+
for(const conn of curr.node.connections){
|
|
276
|
+
if(!seen.has(conn.to)){
|
|
277
|
+
node_queue.push({node: this.nodes[conn.to], distance: curr.distance + conn.length(), last_connection: conn});
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
//node_id++;
|
|
281
|
+
}
|
|
282
|
+
//console.log(node_heap);
|
|
283
|
+
|
|
284
|
+
return undefined;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
class ClosestNodeGraph{
|
|
288
|
+
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
class NodeGraph{
|
|
292
|
+
|
|
293
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import * as WebGL from '../../WebGL/globals';
|
|
2
|
+
import * as Resource from './resource';
|
|
3
|
+
|
|
4
|
+
type Int32 = number;
|
|
5
|
+
type Float = number;
|
|
6
|
+
|
|
7
|
+
interface Point{
|
|
8
|
+
x: Int32;
|
|
9
|
+
y: Int32;
|
|
10
|
+
equals:(p:Point) => boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const NodeTypeEnum = {
|
|
14
|
+
Basic: 0,
|
|
15
|
+
Resource: 1,
|
|
16
|
+
Requirement: 2
|
|
17
|
+
} as const;
|
|
18
|
+
|
|
19
|
+
type NodeType = (typeof NodeTypeEnum)[keyof typeof NodeTypeEnum];
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
//no capacity
|
|
23
|
+
export class KeyNode{
|
|
24
|
+
x: Int32;
|
|
25
|
+
y: Int32;
|
|
26
|
+
inventory: Map<Resource.Resource, Int32>;
|
|
27
|
+
type: NodeType;
|
|
28
|
+
private id: Int32;
|
|
29
|
+
constructor(x: Int32, y: Int32, ty: NodeType=NodeTypeEnum.Basic){
|
|
30
|
+
this.x = x;
|
|
31
|
+
this.y = y;
|
|
32
|
+
this.inventory = new Map();
|
|
33
|
+
this.type = ty;
|
|
34
|
+
this.id = -1;
|
|
35
|
+
this.initialiseDefaultInventory();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
protected initialiseDefaultInventory(){
|
|
39
|
+
this.inventory.set(Resource.ResourceEnum.Water, 0);
|
|
40
|
+
this.inventory.set(Resource.ResourceEnum.Apple, 0);
|
|
41
|
+
}
|
|
42
|
+
typeToString(node_type: NodeType): string{
|
|
43
|
+
switch(node_type){
|
|
44
|
+
case NodeTypeEnum.Basic:
|
|
45
|
+
return 'Bsc';
|
|
46
|
+
case NodeTypeEnum.Requirement:
|
|
47
|
+
return 'Req';
|
|
48
|
+
case NodeTypeEnum.Resource:
|
|
49
|
+
return 'Res';
|
|
50
|
+
}
|
|
51
|
+
return "";
|
|
52
|
+
}
|
|
53
|
+
getId(): Int32{
|
|
54
|
+
return this.id;
|
|
55
|
+
}
|
|
56
|
+
setId(id: Int32){
|
|
57
|
+
this.id = id;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
//can override
|
|
61
|
+
update(t: Float){
|
|
62
|
+
|
|
63
|
+
}
|
|
64
|
+
distanceSq(p: Point){
|
|
65
|
+
const dx = p.x - this.x - 0.5;
|
|
66
|
+
const dy = p.y - this.y - 0.5;
|
|
67
|
+
return dx*dx + dy*dy;
|
|
68
|
+
}
|
|
69
|
+
drawNodeUI(perspective: WebGL.Matrix.TransformationMatrix3x3, solid_shader: WebGL.Shader.MVPColourProgram, text_drawer: WebGL.TextDrawer,
|
|
70
|
+
x: Float, y: Float, text_size:Int32=12): Float{
|
|
71
|
+
//const text_size = 10;
|
|
72
|
+
const white = WebGL.Colour.ColourUtils.white();
|
|
73
|
+
solid_shader.use();
|
|
74
|
+
const back_model = WebGL.WebGL.rectangleModel(x, y, text_size*15, text_size*15);
|
|
75
|
+
solid_shader.setColour(0, 0, 0);
|
|
76
|
+
solid_shader.setMvp(perspective.multiplyCopy(back_model));
|
|
77
|
+
WebGL.Shapes.Quad.draw();
|
|
78
|
+
|
|
79
|
+
//details
|
|
80
|
+
text_drawer.drawTextColour(perspective, x, y, `id ${this.id.toString()}`, text_size, white);
|
|
81
|
+
text_drawer.drawTextColour(perspective, x+(text_size*6), y, this.typeToString(this.type), text_size, white);
|
|
82
|
+
//
|
|
83
|
+
|
|
84
|
+
text_drawer.drawTextColour(perspective, x, y+text_size, this.x.toFixed(0), text_size, white);
|
|
85
|
+
text_drawer.drawTextColour(perspective, x, y+(text_size*2), this.y.toFixed(0), text_size, white);
|
|
86
|
+
|
|
87
|
+
return text_size*3;
|
|
88
|
+
}
|
|
89
|
+
getResourceInventory(res: Resource.Resource): Int32{
|
|
90
|
+
if(!this.inventory.has(res)) return 0;
|
|
91
|
+
return this.inventory.get(res)!;
|
|
92
|
+
}
|
|
93
|
+
deliverResource(res: Resource.Resource, amount: Int32){
|
|
94
|
+
const count = this.inventory.get(res)!
|
|
95
|
+
this.inventory.set(res, count+amount);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
//return number drawn from node
|
|
99
|
+
drawResource(res: Resource.Resource, amount: Int32): Int32{
|
|
100
|
+
const count = this.inventory.get(res)!;
|
|
101
|
+
if(count > 0){
|
|
102
|
+
if(count < amount){
|
|
103
|
+
this.inventory.set(res, 0);
|
|
104
|
+
return count;
|
|
105
|
+
}
|
|
106
|
+
this.inventory.set(res, count-amount);
|
|
107
|
+
return amount;
|
|
108
|
+
}
|
|
109
|
+
return 0;
|
|
110
|
+
}
|
|
111
|
+
serialise(): string{
|
|
112
|
+
//TODO
|
|
113
|
+
return "";
|
|
114
|
+
}
|
|
115
|
+
static deserialise(s: string): KeyNode{
|
|
116
|
+
//TODO
|
|
117
|
+
return new KeyNode(0, 0);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export class RequirementNode extends KeyNode{
|
|
122
|
+
require_resource: Resource.Resource;
|
|
123
|
+
require_amount: Int32;
|
|
124
|
+
constructor(x: Int32, y: Int32){
|
|
125
|
+
super(x, y);
|
|
126
|
+
this.require_resource = Resource.ResourceEnum.Water;
|
|
127
|
+
this.require_amount = 5;
|
|
128
|
+
this.type = NodeTypeEnum.Requirement;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export class ResourceGeneratorNode extends KeyNode{
|
|
133
|
+
current_time: Float;
|
|
134
|
+
gen_time: Float;
|
|
135
|
+
resource: Resource.Resource;
|
|
136
|
+
constructor(x: Int32, y: Int32){
|
|
137
|
+
super(x, y);
|
|
138
|
+
this.current_time = 0;
|
|
139
|
+
this.gen_time = 1000;
|
|
140
|
+
this.resource = Resource.ResourceEnum.Water;
|
|
141
|
+
this.type = NodeTypeEnum.Resource;
|
|
142
|
+
}
|
|
143
|
+
update(t: Float){
|
|
144
|
+
this.current_time += t;
|
|
145
|
+
if(this.current_time >= this.gen_time){
|
|
146
|
+
this.current_time -= this.gen_time;
|
|
147
|
+
const n_resources = this.inventory.get(this.resource)!;
|
|
148
|
+
this.inventory.set(this.resource, n_resources+1);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { expect, test } from 'vitest'
|
|
2
|
+
import * as Card from './card'
|
|
3
|
+
import * as Grid from '../ResourceSim/grid'
|
|
4
|
+
|
|
5
|
+
test('list tests', () => {
|
|
6
|
+
//const list = new Card.
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
test('deck tests', () => {
|
|
10
|
+
//Card.VirtualCard.
|
|
11
|
+
const deck = new Card.Deck();
|
|
12
|
+
deck.addRandomCard();
|
|
13
|
+
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test('rotation', () => {
|
|
17
|
+
const rot = 0;
|
|
18
|
+
const target = Grid.DirectionEnum.Up;
|
|
19
|
+
expect(Grid.DirectionUtil.getTurnDirection(target, rot)).toBe(Grid.TurnDirectionEnum.Straight);
|
|
20
|
+
const r2 = Math.PI*1.5;
|
|
21
|
+
expect(Grid.DirectionUtil.getTurnDirection(target, rot)).toBe(Grid.TurnDirectionEnum.Straight);
|
|
22
|
+
});
|