commit c256015b8c954ee21803e7624882dcf14c981965 Author: Alexander Popov Date: Thu Feb 16 00:04:40 2023 +0300 init diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..b470842 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,20 @@ +root = true + +[*] +indent_style = space +indent_size = 4 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.js] +indent_style = space +indent_size = 2 + +[{*.html,*.css,*.json}] +indent_style = tab +indent_size = 4 + +[*.md] +trim_trailing_whitespace = false diff --git a/src/index.html b/src/index.html new file mode 100644 index 0000000..9191c3e --- /dev/null +++ b/src/index.html @@ -0,0 +1,13 @@ + + + + + + gameEngine + + + + + + + diff --git a/src/js/main.js b/src/js/main.js new file mode 100644 index 0000000..19d4c9b --- /dev/null +++ b/src/js/main.js @@ -0,0 +1,28 @@ +import { Scene } from './scene.js'; +import { Settings } from './settings.js'; +import { Pointer } from './pointer.js'; + +class Game { + constructor() { + this.scene = new Scene(); + this.prevTime = Date.now(); + + Pointer.init(); + + this.run(); + } + + run = () => { + let newTime = Date.now(); + Settings.delta = (newTime - this.prevTime) / 1000; + this.prevTime = newTime; + + this.scene.run(); + + requestAnimationFrame(this.run); + } +} + +window.addEventListener('DOMContentLoaded', () => { + new Game(); +}); diff --git a/src/js/pointer.js b/src/js/pointer.js new file mode 100644 index 0000000..ba6c9bc --- /dev/null +++ b/src/js/pointer.js @@ -0,0 +1,30 @@ +export class Pointer { + static #x; + static #y; + + static init() { + this.#x = 0; + this.#y = 0; + + window.addEventListener('mousemove', this.mouseMove); + window.addEventListener('mousedown', this.mouseDown); + window.addEventListener('mouseup', this.mouseUp); + } + + static set pos({ x, y }) { this.#x = x; this.#y = y; } + static get pos() { + return { x: this.#x, y: this.#y }; + } + + static mouseMove = (e) => { + this.pos = { x: e.pageX, y: e.pageY }; + } + + static mouseDown = (e) => { + this.pos = { x: e.pageX, y: e.pageY }; + } + + static mouseUp = (e) => { + this.pos = { x: e.pageX, y: e.pageY }; + } +} diff --git a/src/js/scene.js b/src/js/scene.js new file mode 100644 index 0000000..949d1f4 --- /dev/null +++ b/src/js/scene.js @@ -0,0 +1,26 @@ +import { Pointer } from './pointer.js'; + +export class Scene { + constructor() { + this.init(); + } + + init = async () => { + this.canvas = document.querySelector('canvas'); + this.context = this.canvas.getContext('2d'); + + this.setScreenSize(); + } + + run() { + console.log(Pointer.pos); + } + + setScreenSize() { + let w = 640; + let h = 480; + + this.canvas.width = w; + this.canvas.height = h; + } +} diff --git a/src/js/settings.js b/src/js/settings.js new file mode 100644 index 0000000..5a74836 --- /dev/null +++ b/src/js/settings.js @@ -0,0 +1,24 @@ +export class Settings { + static #props = {}; + + static get(property) { + return this.#props[property]; + } + + static add(property, value) { + if (!Object.getOwnPropertyDescriptors(this)[property]) { + Object.defineProperty(this, property, { + configurable: true, + enumerable: true, + get: () => this.#props[property], + set: (setValue) => { this.#props[property] = setValue; }, + }); + } + + this.#props[property] = value; + } + + static remove(property) { + delete this.#props[property]; + } +} diff --git a/src/styles.css b/src/styles.css new file mode 100644 index 0000000..e69de29