Skip to content

Commit

Permalink
Merge pull request #16 from ArthurLobopro/update-and-refactor
Browse files Browse the repository at this point in the history
Update and refactor
  • Loading branch information
ArthurLobopro authored Oct 19, 2023
2 parents 33da7f1 + 7cad730 commit 88c818c
Show file tree
Hide file tree
Showing 31 changed files with 1,763 additions and 1,410 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "tetris",
"productName": "tetris",
"version": "1.4.0",
"version": "1.5.0",
"description": "A simple tetris game",
"main": "./dist/main/main.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/main/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ window.addEventListener("DOMContentLoaded", () => {
NunitoFont.inject()
PressStart2PFont.inject()

require("../render/Game")
require("../render/main")
})
96 changes: 0 additions & 96 deletions src/render/Controllers.ts

This file was deleted.

86 changes: 78 additions & 8 deletions src/render/Figures.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { colors } from "./Colors"
import { randint } from "./Util"
import { randint, range } from "./Util"

type Block = {
type: 'block'
}

type NullBlock = {
export type NullBlock = {
type: 'null'
}

Expand All @@ -14,12 +14,14 @@ const block: Block = { type: 'block' }

export type figureName = "square" | "stick" | "z" | "reverse-z" | "reverse-L" | "L" | "T"

export type blocks = (Block | NullBlock)[][]

type figure = {
name: figureName,
blocks: (Block | NullBlock)[][]
blocks: blocks
}

const types: figure[] = [
const FIGURES_TYPES: figure[] = [
{
name: "square",
blocks: [
Expand All @@ -45,7 +47,6 @@ const types: figure[] = [
blocks: [
[null_block, block, block],
[block, block, null_block]

]
},
{
Expand Down Expand Up @@ -73,7 +74,11 @@ const types: figure[] = [

export class Figures {
static get types() {
return types
return FIGURES_TYPES
}

static get figuresNames() {
return this.types.map(figure => figure.name)
}

static random() {
Expand All @@ -87,8 +92,73 @@ export class Figures {
static getByName(name: figureName) {
return (this.types.find((figure) => figure.name === name) as figure).blocks
}
}

static getFigureNames() {
return this.types.map(figure => figure.name)
export interface coords {
x: number
y: number
}

export class Figure {
declare x: number
declare y: number
declare figure: ReturnType<typeof Figures.random>

constructor() {
this.x = 0
this.y = 0
this.figure = Figures.random()
}

get width() {
return this.figure.blocks[0].length
}

get height() {
return this.figure.blocks.length
}

get blocks() {
return this.figure.blocks
}

getRotated() {
const blocks = []

for (const block of range(0, this.width)) {
const newLine = []
for (const line of range(0, this.height)) {
newLine.unshift(this.figure.blocks[line][block])
}
blocks.push(newLine)
}

return new Figure().turnInto({
...this.figure,
blocks
})
}

turnInto(figure: ReturnType<typeof Figures.random>) {
this.figure = figure

return this
}

turnIntoRandom() {
return this.turnInto(Figures.random())
}

setCoords({ x, y }: Partial<coords>) {
this.x = x ?? this.x
this.y = y ?? this.y

return this
}

resetCoords() {
this.setCoords({ x: 0, y: 0 })

return this
}
}
Loading

0 comments on commit 88c818c

Please sign in to comment.