Started city builder like game

Signed-off-by: Avior <github@avior.me>
This commit is contained in:
2022-09-05 00:43:12 +02:00
parent 79b74d16d5
commit 2143f9887e
15 changed files with 296 additions and 21 deletions

View File

@ -6,13 +6,13 @@ import Renderer from '.'
interface Params {
material?: string | Asset
stroke?: string
stroke?: string | {color: string, width: number}
}
export default class RectRenderer extends Renderer implements Params {
public material?: string | Asset
public stroke?: string
public stroke?: string | {color: string, width: number}
public constructor(component: Component2D, params?: Params) {
super(component)
@ -45,7 +45,12 @@ export default class RectRenderer extends Renderer implements Params {
ctx.fillRect(...item)
}
if (this.stroke) {
ctx.strokeStyle = this.stroke
if (typeof this.stroke === 'string') {
ctx.strokeStyle = this.stroke
} else {
ctx.strokeStyle = this.stroke.color
ctx.lineWidth = this.stroke.width
}
ctx.strokeRect(...item)
}
}

View File

@ -11,6 +11,8 @@ export default class TextRenderer extends Renderer {
public text?: string
public size?: number
public weight?: 'bold'
public color?: string
public constructor(component: Component2D, params?: Params) {
super(component)
@ -31,10 +33,11 @@ export default class TextRenderer extends Renderer {
// console.log
if (this.text) {
ctx.fillStyle = 'black'
ctx.textBaseline = 'top'
ctx.fillStyle = this.color ?? 'black'
ctx.textBaseline = 'middle'
ctx.textAlign = 'center'
ctx.font = `${size}px sans-serif`
ctx.font = `${this.weight ? `${this.weight} ` : ''}${size + (this.size ?? 0)}px sans-serif`
ctx.fillText(this.text, ...item)
}
}

View File

@ -10,7 +10,11 @@ export default abstract class Renderer {
protected getPosition(): Vector2D {
const ge = GameEngine.getGameEngine()
const realPosition = ge.currentScene!.camera.topLeft.sum(this.component.position)
const realPosition = ge.currentScene?.camera.topLeft.sum(this.component.position)
if (!realPosition) {
console.error('no camera?!?')
return this.component.position
}
return new Vector2D(
realPosition.x - this.component.scale.x / 2 - this.component.origin.x,
realPosition.y - this.component.scale.y / 2 - this.component.origin.y