fix some js stuff

This commit is contained in:
RandomityGuy 2021-07-08 22:47:57 +05:30
parent b224b1c262
commit 144260b22c
9 changed files with 199 additions and 33 deletions

Binary file not shown.

View file

@ -1,6 +1,5 @@
package src; package src;
import js.Browser;
import h3d.col.Plane; import h3d.col.Plane;
import h3d.mat.Material; import h3d.mat.Material;
import h3d.prim.Cube; import h3d.prim.Cube;
@ -81,6 +80,10 @@ class CameraController extends Object {
} }
public function lockCursor() { public function lockCursor() {
#if js
var jsCanvas = @:privateAccess Window.getInstance().canvas;
jsCanvas.focus();
#end
Window.getInstance().lockPointer((x, y) -> orbit(x, y)); Window.getInstance().lockPointer((x, y) -> orbit(x, y));
#if hl #if hl
Cursor.show(false); Cursor.show(false);
@ -92,6 +95,10 @@ class CameraController extends Object {
#if hl #if hl
Cursor.show(true); Cursor.show(true);
#end #end
#if js
var jsCanvas = @:privateAccess Window.getInstance().canvas;
@:privateAccess Window.getInstance().lockCallback = null; // Fix cursorlock position shit
#end
} }
function orbit(mouseX:Float, mouseY:Float) { function orbit(mouseX:Float, mouseY:Float) {

View file

@ -348,6 +348,8 @@ class DtsObject extends GameObject {
var material = Material.create(); var material = Material.create();
var iflMaterial = false;
if (fullName == null || (this.isTSStatic && ((flags & (1 << 31) > 0)))) { if (fullName == null || (this.isTSStatic && ((flags & (1 << 31) > 0)))) {
if (this.isTSStatic) { if (this.isTSStatic) {
// TODO USE PBR??? // TODO USE PBR???
@ -355,6 +357,7 @@ class DtsObject extends GameObject {
} else if (Path.extension(fullName) == "ifl") { } else if (Path.extension(fullName) == "ifl") {
var keyframes = parseIfl(fullName); var keyframes = parseIfl(fullName);
this.materialInfos.set(material, keyframes); this.materialInfos.set(material, keyframes);
iflMaterial = true;
// TODO IFL SHIT // TODO IFL SHIT
} else { } else {
var texture:Texture = ResourceLoader.getTexture(fullName); var texture:Texture = ResourceLoader.getTexture(fullName);
@ -370,12 +373,13 @@ class DtsObject extends GameObject {
// TODO TRANSLUENCY SHIT // TODO TRANSLUENCY SHIT
} }
material.shadows = false; material.shadows = false;
if (material.texture == null) { if (material.texture == null && !iflMaterial) {
// var dtsshader = new DtsTexture(); // var dtsshader = new DtsTexture();
// dtsshader.currentOpacity = 1; // dtsshader.currentOpacity = 1;
// Make a 1x1 white texture // Make a 1x1 white texture
var bitmap = new hxd.BitmapData(1, 1); var bitmap = new hxd.BitmapData(1, 1);
bitmap.setPixel(0, 0, 0xFFFFFF); bitmap.setPixel(0, 0, 0xFFFFFF);
bitmap.setPixel(1, 1, 0xFFFFFF);
var texture = new Texture(1, 1); var texture = new Texture(1, 1);
texture.uploadBitmap(bitmap); texture.uploadBitmap(bitmap);
material.texture = texture; material.texture = texture;

View file

@ -26,7 +26,7 @@ class Main extends hxd.App {
#if hl #if hl
hl.UI.closeConsole(); hl.UI.closeConsole();
#end #end
ResourceLoader.init(() -> { ResourceLoader.init(s2d, () -> {
Settings.init(); Settings.init();
AudioManager.init(); AudioManager.init();
AudioManager.playShell(); AudioManager.playShell();

View file

@ -29,6 +29,24 @@ class MarbleGame {
canvas = new Canvas(scene2d, cast this); canvas = new Canvas(scene2d, cast this);
this.scene = scene; this.scene = scene;
this.scene2d = scene2d; this.scene2d = scene2d;
#if js
js.Browser.document.addEventListener('pointerlockchange', () -> {
if (!paused && world != null) {
if (world.finishTime == null) {
if (js.Browser.document.pointerLockElement != @:privateAccess Window.getInstance().canvas) {
paused = true;
handlePauseGame();
// Focus the shit again
var jsCanvas = @:privateAccess Window.getInstance().canvas;
@:privateAccess Window.getInstance().lockCallback = null; // Fix cursorlock position shit
jsCanvas.focus();
// js.Browser.document.exitPointerLock();
}
}
}
});
#end
} }
public function update(dt:Float) { public function update(dt:Float) {
@ -41,30 +59,15 @@ class MarbleGame {
world.update(dt); world.update(dt);
} }
if (Key.isPressed(Key.ESCAPE) && world.finishTime == null) { if (Key.isPressed(Key.ESCAPE) && world.finishTime == null) {
#if hl
paused = !paused; paused = !paused;
if (paused) { handlePauseGame();
world.setCursorLock(false); #end
exitGameDlg = new ExitGameDlg((sender) -> { #if js
canvas.popDialog(exitGameDlg); if (paused)
paused = !paused; paused = false;
world.dispose(); handlePauseGame();
world = null; #end
canvas.setContent(new PlayMissionGui());
}, (sender) -> {
canvas.popDialog(exitGameDlg);
paused = !paused;
world.setCursorLock(true);
}, (sender) -> {
canvas.popDialog(exitGameDlg);
world.restart();
world.setCursorLock(true);
paused = !paused;
});
canvas.pushDialog(exitGameDlg);
} else {
canvas.popDialog(exitGameDlg);
world.setCursorLock(true);
}
} }
} }
if (canvas != null) { if (canvas != null) {
@ -76,6 +79,32 @@ class MarbleGame {
} }
} }
public function handlePauseGame() {
if (paused) {
world.setCursorLock(false);
exitGameDlg = new ExitGameDlg((sender) -> {
canvas.popDialog(exitGameDlg);
paused = !paused;
world.dispose();
world = null;
canvas.setContent(new PlayMissionGui());
}, (sender) -> {
canvas.popDialog(exitGameDlg);
paused = !paused;
world.setCursorLock(true);
}, (sender) -> {
canvas.popDialog(exitGameDlg);
world.restart();
world.setCursorLock(true);
paused = !paused;
});
canvas.pushDialog(exitGameDlg);
} else {
canvas.popDialog(exitGameDlg);
world.setCursorLock(true);
}
}
public function playMission(mission:Mission) { public function playMission(mission:Mission) {
var musicFileName = [ var musicFileName = [
'data/sound/groovepolice.ogg', 'data/sound/groovepolice.ogg',

View file

@ -12,6 +12,7 @@ import dif.Dif;
import hxd.fs.LocalFileSystem; import hxd.fs.LocalFileSystem;
import hxd.fs.FileSystem; import hxd.fs.FileSystem;
import hxd.res.Loader; import hxd.res.Loader;
import fs.ManifestProgress;
class ResourceLoader { class ResourceLoader {
#if hl #if hl
@ -34,17 +35,17 @@ class ResourceLoader {
// static var threadPool:FixedThreadPool = new FixedThreadPool(4); // static var threadPool:FixedThreadPool = new FixedThreadPool(4);
public static function init(onLoadedFunc:Void->Void) { public static function init(scene2d:h2d.Scene, onLoadedFunc:Void->Void) {
#if js #if js
var mfileSystem = ManifestBuilder.create("data"); var mfileSystem = ManifestBuilder.create("data");
var mloader = new ManifestLoader(mfileSystem); var mloader = new ManifestLoader(mfileSystem);
mloader.onLoaded = () -> { var preloader = new ManifestProgress(mloader, () -> {
loader = mloader; loader = mloader;
fileSystem = mfileSystem; fileSystem = mfileSystem;
onLoadedFunc(); onLoadedFunc();
}; }, scene2d);
mloader.loadManifestFiles(); preloader.start();
#end #end
#if hl #if hl
onLoadedFunc(); onLoadedFunc();

View file

@ -77,7 +77,7 @@ class Settings {
invertYAxis: false invertYAxis: false
}; };
public static var progression = [0, 0, 0]; public static var progression = [24, 24, 52];
public static var highscoreName = ""; public static var highscoreName = "";
public static function applySettings() { public static function applySettings() {

View file

@ -43,9 +43,7 @@ class Sky extends Object {
env.compute(); env.compute();
// var renderer = cast(level.scene.renderer, h3d.scene.pbr.Renderer); // var renderer = cast(level.scene.renderer, h3d.scene.pbr.Renderer);
var shad = new Skybox(texture); var shad = new Skybox(texture);
#if js
skyMesh.material.mainPass.addShader(shad); skyMesh.material.mainPass.addShader(shad);
#end
// skyMesh.material.shadows = false; // skyMesh.material.shadows = false;
} }

127
src/fs/ManifestProgress.hx Normal file
View file

@ -0,0 +1,127 @@
// MIT License
// Copyright (c) 2018 Pavel Alexandrov
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package fs;
import h2d.Graphics;
import h2d.Text;
import hxd.Math;
import fs.ManifestLoader;
class ManifestProgress extends h2d.Object {
var g:h2d.Graphics;
var text:h2d.Text;
var loader:ManifestLoader;
var color:Int;
var onLoaded:Void->Void;
var totalBarHeight:Float;
var subBarHeight:Float;
var barWidth:Float;
public var removeSelf:Bool = true;
public function new(loader:ManifestLoader, color:Int = 0xffffff, onLoaded:Void->Void, ?parent:h2d.Object) {
super(parent);
this.loader = loader;
this.color = color;
this.onLoaded = onLoaded;
g = new Graphics(this);
text = new Text(hxd.res.DefaultFont.get(), this);
text.y = 50;
text.textAlign = Center;
text.textColor = 0xff000000 | color;
}
public function start():Void {
var s2d = getScene();
var x = s2d.width * .1;
barWidth = s2d.width * .8;
totalBarHeight = Math.max(s2d.height * .02, 4);
subBarHeight = Math.max(s2d.height * .01, 2);
var barH = totalBarHeight + (subBarHeight + 2) * ManifestLoader.concurrentFiles;
g.y = (s2d.height - (barH + 4 + (text.font.lineHeight + text.lineSpacing) * 2)) / 2;
g.x = x;
text.y = g.y + barH + 4;
text.x = x;
text.maxWidth = barWidth;
loader.onLoaded = finish;
loader.onFileLoadStarted = showFileName;
loader.onFileLoadStarted = fileLoaded;
loader.onFileProgress = fileProgress;
loader.loadManifestFiles();
}
function repaint() {
g.clear();
g.beginFill(color);
g.drawRect(0, 0, barWidth * (loader.loadedFiles / loader.totalFiles), totalBarHeight);
var txt = "Files: " + loader.loadedFiles + "/" + loader.totalFiles + "\n";
if (loader.tasks != null)
for (t in loader.tasks) {
if (t.busy) {
var ratio = t.loaded / t.total;
g.drawRect(0, totalBarHeight + 2 + t.slot * (2 + subBarHeight), barWidth * ratio, subBarHeight);
txt += t.entry.name + " " + Math.ceil(ratio * 100) + "% ";
} else {
g.drawRect(0, totalBarHeight + 2 + t.slot * (2 + subBarHeight), barWidth, subBarHeight);
}
}
text.text = txt;
}
function setProgress(b:Int, t:Int):Void {
g.clear();
var s = getScene();
var size = s != null ? s.width * .5 : 400;
g.beginFill(color);
g.drawRect(0, 0, loader.loadedFiles / loader.totalFiles * size, 20);
if (b != 0 && t != 0)
g.drawRect(0, 25, b / t * size, 20);
}
function finish():Void {
// setProgress(1, 1);
repaint();
text.text = "All done!";
if (removeSelf)
remove();
onLoaded();
}
function showFileName(task:LoaderTask) {
repaint();
// text.text = "Loading: " + task.entry.path;
}
function fileLoaded(task:LoaderTask) {
repaint();
// setProgress(1, 1);
// text.text = "Loaded : " + task.entry.path;
}
function fileProgress(task:LoaderTask) {
repaint();
// setProgress(task.loaded, task.total);
}
}