-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
246 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"sprites":[{"name":"palette","packs":[{"frame":{"x":0,"y":0,"w":8,"h":8},"packed":{"x":0,"y":0,"w":8,"h":8}},{"frame":{"x":0,"y":0,"w":8,"h":8},"packed":{"x":8,"y":0,"w":8,"h":8}},{"frame":{"x":0,"y":0,"w":8,"h":8},"packed":{"x":0,"y":8,"w":8,"h":8}},{"frame":{"x":0,"y":0,"w":8,"h":8},"packed":{"x":16,"y":0,"w":8,"h":8}},{"frame":{"x":0,"y":0,"w":8,"h":8},"packed":{"x":0,"y":16,"w":8,"h":8}}]}]} | ||
{"sprites":[{"name":"palette","tags":[{"from":0,"to":0,"name":"1"},{"from":1,"to":1,"name":"2"},{"from":2,"to":2,"name":"3"},{"from":3,"to":3,"name":"4"},{"from":4,"to":4,"name":"5"}],"packs":[{"frame":{"x":0,"y":0,"w":8,"h":8},"packed":{"x":0,"y":0,"w":8,"h":8}},{"frame":{"x":0,"y":0,"w":8,"h":8},"packed":{"x":8,"y":0,"w":8,"h":8}},{"frame":{"x":0,"y":0,"w":8,"h":8},"packed":{"x":0,"y":8,"w":8,"h":8}},{"frame":{"x":0,"y":0,"w":8,"h":8},"packed":{"x":16,"y":0,"w":8,"h":8}},{"frame":{"x":0,"y":0,"w":8,"h":8},"packed":{"x":0,"y":16,"w":8,"h":8}}]}]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
import { Vec2, Subtexture } from 'blah' | ||
|
||
export class Frame { | ||
constructor(readonly image: Subtexture, | ||
readonly duration: number) {} | ||
} | ||
|
||
export class Animation { | ||
|
||
constructor(readonly name: string, | ||
readonly frames: Array<Frame>) {} | ||
|
||
} | ||
|
||
|
||
export class Sprite { | ||
|
||
|
||
constructor(readonly name: string, | ||
readonly origin: Vec2, | ||
readonly animations: Array<Animation>) {} | ||
|
||
get(name: string) { | ||
return this.animations.find(_ => _.name === name) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { Component } from '../world' | ||
import { Time, Batch, Vec2, Mat3x2, Color } from 'blah' | ||
import { Sprite } from '../assets/sprite' | ||
import Content from '../content' | ||
|
||
|
||
export class Animator extends Component { | ||
|
||
static make = (name: string) => new Animator(name) | ||
|
||
constructor(readonly name: string) { | ||
super() | ||
this._sprite = Content.find_sprite(name) | ||
this._animation_index = 0 | ||
this._frame_index = 0 | ||
this._frame_counter = 0 | ||
} | ||
|
||
_sprite: Sprite | ||
_animation_index: number | ||
_frame_index: number | ||
_frame_counter: number | ||
|
||
|
||
scale = Vec2.one | ||
offset = Vec2.zero | ||
|
||
get sprite() { | ||
return this._sprite | ||
} | ||
|
||
get animation() { | ||
|
||
if (this._sprite && this._animation_index >= 0 && this._animation_index < this._sprite.animations.length) { | ||
return this._sprite.animations[this._animation_index] | ||
} | ||
return undefined | ||
} | ||
|
||
|
||
play(animation: string, restart: boolean = false) { | ||
for (let i = 0; i < this._sprite.animations.length; i++) { | ||
if (this._sprite.animations[i].name === animation) { | ||
if (this._animation_index !== i || restart) { | ||
this._animation_index = i | ||
this._frame_index = 0 | ||
this._frame_counter = 0 | ||
} | ||
break | ||
} | ||
} | ||
} | ||
|
||
|
||
update() { | ||
if (this._in_valid_state) { | ||
let anim = this._sprite.animations[this._animation_index] | ||
let frame = anim.frames[this._frame_index] | ||
|
||
this._frame_counter += Time.delta | ||
|
||
while (this._frame_counter >= frame.duration) { | ||
this._frame_counter -= frame.duration | ||
|
||
this._frame_index++; | ||
|
||
if (this._frame_index >= anim.frames.length) { | ||
this._frame_index = 0 | ||
} | ||
} | ||
} | ||
} | ||
|
||
render(batch: Batch) { | ||
if (this._in_valid_state) { | ||
batch.push_matrix( | ||
Mat3x2.create_transform(this.entity.position.add(this.offset), | ||
this._sprite.origin, this.scale, 0)) | ||
|
||
let anim = this._sprite.animations[this._animation_index] | ||
let frame = anim.frames[this._frame_index] | ||
batch.stex(frame.image, Vec2.zero, Color.white) | ||
batch.pop_matrix() | ||
} | ||
} | ||
|
||
|
||
get _in_valid_state() { | ||
return this._sprite && | ||
this._animation_index >= 0 && | ||
this._animation_index < this._sprite.animations.length && | ||
this._frame_index >= 0 && | ||
this._frame_index < this._sprite.animations[this._animation_index].frames.length | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { World, Component } from '../world' | ||
import { Vec2, Batch } from 'blah' | ||
import { Animator } from './animator' | ||
import Game from '../game' | ||
|
||
|
||
export class Background extends Component { | ||
|
||
static make = (world: World, position: Vec2) => { | ||
|
||
let en = world.add_entity(position) | ||
|
||
let anim = en.add(Animator.make('palette')) | ||
anim.play('4') | ||
|
||
anim.scale = Vec2.make(Game.width / 8, Game.height / 8) | ||
|
||
en.add(new Background()) | ||
|
||
return en | ||
} | ||
|
||
|
||
render(batch: Batch) { | ||
|
||
|
||
|
||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { World, Component } from '../world' | ||
import { Vec2, Mat3x2 } from 'blah' | ||
|
||
import { Background } from './background' | ||
|
||
export class MainMenu extends Component { | ||
|
||
|
||
static make = (world: World, position: Vec2) => { | ||
let en = world.add_entity(position) | ||
|
||
en.add(new MainMenu()) | ||
|
||
Background.make(world, position) | ||
|
||
return en | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/// <reference types="vite/client" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters