Init with basic generation of first level a primitive movement

This commit is contained in:
Nikita Iryupin 2021-04-21 23:15:41 +02:00
parent 82dfbe15f4
commit e969cdd606
18 changed files with 347 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
.DS_Store
node_modules
package-lock.json
public/bundle*

7
LICENSE Normal file
View File

@ -0,0 +1,7 @@
Copyright (c) 2017 [these people](https://github.com/rollup/rollup-starter-app/graphs/contributors)
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.

20
package.json Normal file
View File

@ -0,0 +1,20 @@
{
"name": "rollup-starter-app",
"devDependencies": {
"@rollup/plugin-commonjs": "^17.0.0",
"@rollup/plugin-node-resolve": "^11.1.0",
"npm-run-all": "^4.1.5",
"rollup": "^2.36.2",
"rollup-plugin-terser": "^7.0.2",
"serve": "^11.3.2"
},
"dependencies": {
"date-fns": "^2.16.1"
},
"scripts": {
"build": "rollup -c",
"watch": "rollup -c -w",
"dev": "npm-run-all --parallel start watch",
"start": "serve public"
}
}

BIN
public/favicon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

12
public/index.html Normal file
View File

@ -0,0 +1,12 @@
<!doctype html>
<html>
<head lang='en'>
<meta charset='utf-8'>
<meta name='viewport' content='width=device-width'>
<title>BattleCity.js</title>
<script src='bundle.js'></script>
</head>
<body>
<canvas id="canvas" height="1000" width="1000"></canvas>
</body>
</html>

BIN
public/nesBattle.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 120 KiB

21
rollup.config.js Normal file
View File

@ -0,0 +1,21 @@
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import { terser } from 'rollup-plugin-terser';
// `npm run build` -> `production` is true
// `npm run dev` -> `production` is false
const production = !process.env.ROLLUP_WATCH;
export default {
input: 'src/main.js',
output: {
file: 'public/bundle.js',
format: 'iife', // immediately-invoked function expression — suitable for <script> tags
sourcemap: true
},
plugins: [
resolve(), // tells Rollup how to find date-fns in node_modules
commonjs(), // converts date-fns to ES modules
production && terser() // minify, but only in production
]
};

View File

@ -0,0 +1,16 @@
export class Tile {
constructor(x, y, width, height, id){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.id = id;
this.isTransperent = false;
this.img = null;
}
addImage(img){
this.img = img;
}
}

View File

@ -0,0 +1,47 @@
import {Tile} from "./tile"
export class Tileset {
tiles = [];
constructor(imageName) {
this.image = new Image();
this.image.src = `./${imageName}`;
}
addTile(x, y, width, height, id){
this.tiles.push(new Tile(x, y, width, height, id));
}
loadAll() {
return new Promise(resolve => {
this.image.onload = (() => {
for (let i = 0; i < this.tiles.length; i++) {
let tile = this.tiles[i];
tile.addImage(this.generateImg(tile.x, tile.y, tile.width, tile.height));
}
resolve("Resolved");
})
});
}
generateImg(x, y, width, height) {
var canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
var context = canvas.getContext('2d');
context.drawImage(this.image, x, y, width, height, 0, 0, width, height);
return canvas;
}
getImageOf(id){
for (let i = 0; i < this.tiles.length; i++){
if (this.tiles[i].id == id){
return this.tiles[i].img;
}
}
return null;
}
}

38
src/Graphics/map.js Normal file
View File

@ -0,0 +1,38 @@
export class Map {
//0 - empty
//1 - brick
//2 - brick right
//3 - brick down
//4 - brick left
//5 - brick up
//6 - steel
//7 - steel right
//8 - steel down
//9 - steel left
//10 - steel up
tiles = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
0, 1, 0, 1, 0, 1, 6, 1, 0, 1, 0, 1, 0,
0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0,
3, 0, 3, 3, 0, 0, 0, 0, 0, 3, 3, 0, 3,
10, 0, 5, 5, 0, 3, 0, 3, 0, 5, 5, 0, 10,
0, 3, 0, 3, 0, 1, 1, 1, 0, 3, 0, 3, 0,
0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
0, 1, 0, 1, 0, 5, 0, 5, 0, 1, 0, 1, 0,
0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0,
0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 2, 0, 4, 0, 0, 0, 0, 0
];
constructor() {
}
}

View File

@ -0,0 +1,3 @@
export class GameKeyAdapterInterface {
handleEvent(ev){}
}

View File

@ -0,0 +1,29 @@
import { GameKeyAdapterInterface } from "./gameKeyAdapterInterface";
import Direction from "./../directionEnum";
export class TankKeyAdapter extends GameKeyAdapterInterface{
constructor(tank){
super();
this.tank = tank;
}
handleEvent(event){
switch(event.key){
case "ArrowLeft":
this.tank.move(Direction().left);
break;
case "ArrowRight":
this.tank.move(Direction().right);
break;
case "ArrowUp":
this.tank.move(Direction().up);
break;
case "ArrowDown":
this.tank.move(Direction().down);
break;
}
console.log(event.key);
}
}

10
src/canvas.js Normal file
View File

@ -0,0 +1,10 @@
import {Game} from "./game";
export function init(){
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
if (ctx) {
const game = new Game(500, 500, ctx, canvas);
game.start();
}
}

6
src/directionEnum.js Normal file
View File

@ -0,0 +1,6 @@
const DirectionEnum = {"up":1, "left":2, "down": 3, "right": 4};
Object.freeze(DirectionEnum);
export default function Direction(){
return DirectionEnum;
}

14
src/game.js Normal file
View File

@ -0,0 +1,14 @@
import { GameField } from "./gameField";
import { TankKeyAdapter } from "./KeyAdapters/tankKeyAdapter";
export class Game {
constructor(width = 500, height = 500, ctx){
this.gameField = new GameField(width, height, ctx, canvas);
this.gameKeyAdapter = new TankKeyAdapter(this.gameField.tank);
}
start() {
addEventListener("keydown", (ev) => this.gameKeyAdapter.handleEvent(ev));
requestAnimationFrame(() => this.gameField.drawField());
}
}

75
src/gameField.js Normal file
View File

@ -0,0 +1,75 @@
import {Tileset} from "./Graphics/Tileset/tileset"
import {Map} from "./Graphics/map";
import { PlayerTank } from "./playerTank";
export class GameField {
TILE_SIZE = 32;
GAME_WIDTH = 13;
GAME_HEIGHT = 13;
constructor(width = 512, height = 512, ctx, canvas){
this.height = height;
this.width = width;
this.ctx = ctx;
this.canvas = canvas
this.tank = new PlayerTank(0,0,0);
this.tileset = null;
}
//add all tiles here!
async setupTileset(){
this.tileset = new Tileset("nesBattle.png");
//Bricks
this.tileset.addTile(512, 0, 32, 31, 1);
this.tileset.addTile(545, 0, 30, 31, 2);
this.tileset.addTile(577, 0, 30, 31, 3);
this.tileset.addTile(607, 0, 30, 31, 4);
this.tileset.addTile(640, 0, 32, 31, 5);
//Metal
this.tileset.addTile(512, 32, 32, 31, 6);
this.tileset.addTile(544, 32, 32, 31, 7);
this.tileset.addTile(576, 32, 32, 31, 8);
this.tileset.addTile(608, 32, 32, 31, 9);
this.tileset.addTile(640, 32, 32, 31, 10);
//Tank
this.tileset.addTile(0, 0, 32, 32, 101);
this.tileset.addTile(64, 0, 32, 32, 102);
this.tileset.addTile(128, 0, 32, 32, 103);
this.tileset.addTile(192, 0, 32, 32, 104);
await this.tileset.loadAll();
}
async drawField() {
if (!this.tileset){
await this.setupTileset();
this.canvas.height = this.height;
this.canvas.width = this.width;
}
let map = new Map().tiles;
for (let x = 0; x < this.GAME_WIDTH; x += 1){
for (let y = 0; y <= this.GAME_HEIGHT; y += 1) {
let id = map[x + y*this.GAME_WIDTH];
if (id != 0) {
this.ctx.drawImage(this.tileset.getImageOf(id), x*this.TILE_SIZE, y*this.TILE_SIZE, this.TILE_SIZE, this.TILE_SIZE);
} else {
this.ctx.fillStyle = "black";
this.ctx.fillRect(x*this.TILE_SIZE, y*this.TILE_SIZE, this.TILE_SIZE, this.TILE_SIZE);
}
}
}
this.tank.draw(this.tileset, this.ctx, this.TILE_SIZE);
requestAnimationFrame(() => this.drawField());
}
}

3
src/main.js Normal file
View File

@ -0,0 +1,3 @@
import * as canvas from "./canvas";
addEventListener("load", () => canvas.init());

42
src/playerTank.js Normal file
View File

@ -0,0 +1,42 @@
import Direction from "./directionEnum";
export class PlayerTank {
constructor(x, y){
this.x = x;
this.y = y;
this.speed = 2.0;
this.direction = Direction().up;
}
move(direction){
switch(direction){
case Direction().up:
this.y -= this.speed;
this.direction = Direction().up;
break;
case Direction().left:
this.x -= this.speed;
this.direction = Direction().left;
break;
case Direction().right:
this.x += this.speed;
this.direction = Direction().right;
break;
case Direction().down:
this.y += this.speed;
this.direction = Direction().down;
break;
}
console.log(this.x + " " + this.y);
}
draw(tileset, ctx, tilesize){
ctx.drawImage(tileset.getImageOf(100 + this.direction), this.x, this.y, tilesize, tilesize);
}
}