summaryrefslogtreecommitdiff
path: root/src/block.ts
blob: 0d5b97e258e6cbd3528ace4b5fb08524ea21dfd6 (plain) (blame)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
module Omni {

    export class Block {
        private state:number;

        private mesh:Physijs.BoxMesh;

        static states:number[];
        static mesh_file:string = "";

        //will take the correct state as params?
        constructor(private geometry:THREE.Geometry, private materials:THREE.Material[]) {
            this.mesh = new Physijs.BoxMesh(geometry, materials[0], 0);
        }

        getState():number {
            return this.state;
        }

        setState(state:number):void {
            this.state = state;
        }

        setPosition(pos:THREE.Vector3):void {
            this.mesh.position.copy(pos);
        }

        getPosition():THREE.Vector3 {
            return this.mesh.position.clone();
        }

        getObject():THREE.Object3D {
            return this.mesh;
        }

    }

    /**
     * Plain block without any puzzles.
     */
    export class PlainBlock extends Block {
        static mesh_file = "plainBlock.json";
        static states:number[] = [];

        constructor(loader:BlockLoader) {
            super(loader.getMesh(PlainBlock.mesh_file), loader.getMaterials(PlainBlock.mesh_file));
        }
    }

    /**
     * Button block with 2 states
     */
    export class ButtonBlock extends Block {
        //need to make good design choices here, with the handling of states..
        static mesh_file = "buttonBlock.json";
        static states:number[] = [0, 1];

        constructor(loader:BlockLoader) {
            super(loader.getMesh(ButtonBlock.mesh_file), loader.getMaterials(ButtonBlock.mesh_file));
        }
    }

    /**
     * Lever block with 4 positions/states
     */
    export class LeverBlock extends Block {
        static mesh_file = "leverBlock.json";
        static states:number[] = [0, 1, 2, 3];

        constructor(loader:BlockLoader) {
            super(loader.getMesh(LeverBlock.mesh_file), loader.getMaterials(LeverBlock.mesh_file));
        }
    }

    /**
     *
     */
    export class AzimuthBlock extends Block {
        static mesh_file = "azimuthBlock.json";
        static states:number[] = [0, 1, 2, 3];

    }

    /**
     *
     */
    export class PullBlock extends Block {
        static mesh_file = "pullBlock.json";
        static states:number[] = [];

    }

    export class BlockLoader {
        private manager:THREE.LoadingManager = new THREE.LoadingManager();
        private loader:THREE.JSONLoader = new THREE.JSONLoader(this.manager);

        private geometries:{[file_name:string]:THREE.Geometry} = {};
        private materials:{[file_name:string]:THREE.Material[]} = {};

        private loaded:boolean = false;

        static URL_PREFIX:string = "json/";
        static BLOCKS:string[] = [
            PlainBlock.mesh_file,
            LeverBlock.mesh_file,
            //ButtonBlock.mesh_file,
            //AzimuthBlock.mesh_file,
            //PullBlock.mesh_file,
        ];

        /**
         * @param files what files to load()
         */
        constructor(private files:string[]) {
            this.manager.onLoad = () => {
                this.loaded = true;
                console.debug("BlockLoader: loaded");
            };
        }

        /**
         * Load all the geometries and materials from mesh_files
         */
        load():void {
            console.debug("BlockLoader: loading...");
            this.files.forEach((file) => {
                this.loadOne(file);
            });
        }

        /**
         * Load additional geometries and mats
         * @param file what file to load. Will be prefixed with URL_PREFIX
         */
        loadFile(file:string):void {
            if (this.files.indexOf(file) == -1) {
                this.files.push(file);
                this.loadOne(file);
            }
        }

        private loadOne(file:string) {
            this.loader.load(BlockLoader.URL_PREFIX + file, (geometry, materials) => {
                this.geometries[file] = geometry;
                this.materials[file] = materials;
            });
        }

        /**
         * Dispose of all the geometries and mats
         */
        dispose():void {
            console.debug("BlockLoader: disposing...");
            if (this.loaded) {
                this.files.forEach((file) => {
                    this.geometries[file].dispose();
                    this.materials[file].forEach((material) => {
                        material.dispose();
                    });
                });
                this.loaded = false;
            }
        }

        isLoaded():boolean {
            return this.loaded;
        }

        meshLoaded(file:string) {
            return this.geometries[file] !== undefined;
        }

        matsLoaded(file:string) {
            return this.materials[file] !== undefined;
        }

        getMesh(file:string):THREE.Geometry {
            return this.geometries[file];
        }

        getMaterials(file:string):THREE.Material[] {
            return this.materials[file];
        }
    }
}