Added level class and player movement

This commit is contained in:
Arne van Iterson 2020-03-02 22:06:16 +01:00
parent 33ab4a4624
commit 1bf8fb648b
7 changed files with 85 additions and 9 deletions

5
css/index.css Normal file
View File

@ -0,0 +1,5 @@
html, body {
margin: 0;
padding: 0;
overflow: hidden;
}

View File

@ -3,6 +3,7 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/index.css">
<script type="module" src="src/index.js"></script>
<title>Game</title>
</head>

14
main.js
View File

@ -1,4 +1,4 @@
const { app, BrowserWindow } = require('electron')
const { app, BrowserWindow, ipcMain } = require('electron')
const path = require('path')
function createWindow () {
@ -6,10 +6,20 @@ function createWindow () {
const win = new BrowserWindow({
width: 800,
height: 600,
backgroundColor: '#111',
resizable: false,
frame: true,
webPreferences: {
nodeIntegration: true
}
})
});
ipcMain.on('resize', function (event, window) {
win.setSize( window.w, window.h);
});
// Remove alt menu
win.removeMenu();
// and load the index.html of the app.
win.loadFile(path.join('index.html'))

BIN
res/placeholder.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 243 B

View File

@ -4,9 +4,11 @@ const { math, KeyControls, Texture, Sprite, TileSprite } = asdf;
var texture = new Texture('./res/player.png');
class Player extends TileSprite {
constructor(keys) {
constructor(keys, window) {
super(texture, 24, 24);
this.scale = { x: 3, y: 3 };
this.scale = { x: 2, y: 2 };
this.window = window;
// Rate walking = 0.4
// Rate running = 0.08
@ -23,11 +25,12 @@ class Player extends TileSprite {
this.curFrame = 0;
this.frame = this.frames[this.curFrame];
this.pos.x = (640 / 2) - (24 * this.scale.x / 2);
this.pos.y = (320 / 2) - (24 * this.scale.y / 2);
this.pos.x = (this.window.w / 2) - (24 * this.scale.x / 2);
this.pos.y = (this.window.h / 2) - (24 * this.scale.y / 2);
this.keys = keys;
}
update(dt, t) {
const { rate, frames } = this;
this.curTime += dt;

View File

@ -1,19 +1,32 @@
const { ipcRenderer } = require('electron')
var asdf = require('asdf-games');
const { Game, Container, math, KeyControls, MouseControls, Text, Texture, TileMap, Sprite, TileSprite } = asdf;
const game = new Game(640, 320, true);
const window = { w: 640, h: 320 };
const game = new Game(window.w, window.h, true);
ipcRenderer.send('resize', window);
const { scene, w, h } = game;
var Player = require("./src/entities/player.js")
var Level = require("./src/levels/level.js")
const mouseAim = new MouseControls();
const keys = new KeyControls();
var Player = require("./src/entities/player.js")
var player = new Player(keys);
var player = new Player(keys, window);
var level = new Level(window, keys, player);
scene.add(level);
scene.add(player);
game.run((dt ,t) => {
if (mouseAim.isDown) {
console.log('cliccccccccccc');
}
// Check gamestate
// TODO
});

44
src/levels/level.js Normal file
View File

@ -0,0 +1,44 @@
var asdf = require("asdf-games");
const { math, KeyControls, Texture, Sprite, TileSprite, TileMap } = asdf;
const texture = new Texture('./res/placeholder.png');
const levelSize = { w: 640, h: 320 };
var levelData = [];
for (let index = 0; index < ((levelSize.w / 32) * (levelSize.h / 32)); index++) {
levelData.push({ x: 0, y: 0 });
}
class Level extends TileMap {
constructor (window, keys, player) {
super(levelData, levelSize.w / 32, levelSize.h / 32, 32, 32, texture);
this.pos.x = (window.w / 2) - (levelSize.w / 2);
this.pos.y = (window.h / 2) - (levelSize.h / 2);
this.keys = keys;
this.player = player
}
update(dt, t) {
// Change walking direction
if (this.keys.x == -1) {
// Left
this.pos.x = this.pos.x + dt * (1 / this.player.rate) * 10;
}
if (this.keys.x == 1) {
// Right
this.pos.x = this.pos.x - dt * (1 / this.player.rate) * 10;
}
if (this.keys.y == -1) {
// Down
this.pos.y = this.pos.y + dt * (1 / this.player.rate) * 10;
}
if (this.keys.y == 1) {
// Up
this.pos.y = this.pos.y - dt * (1 / this.player.rate) * 10;
}
}
}
module.exports = Level;