-
Notifications
You must be signed in to change notification settings - Fork 8
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
149 additions
and
33 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,50 @@ | ||
import { World, Image, Keys, KeyEvent } from '../mod.ts'; | ||
|
||
|
||
class Game extends World { | ||
public test = new Image(this, "assets/caviar.png", 200, 100, 414, 197); | ||
|
||
public setup() { | ||
this.addChild(this.test); | ||
} | ||
public draw() { | ||
|
||
} | ||
public keyDown(key: KeyEvent) { | ||
switch (key.keycode) { | ||
case Keys.ARROWUP: { | ||
this.test.setY(this.test.y - 10); | ||
break; | ||
} | ||
case Keys.ARROWDOWN: { | ||
this.test.setY(this.test.y + 10); | ||
break; | ||
} | ||
case Keys.ARROWLEFT: { | ||
this.test.setX(this.test.x - 10); | ||
break; | ||
} | ||
case Keys.ARROWRIGHT: { | ||
this.test.setX(this.test.x + 10); | ||
break; | ||
} | ||
|
||
} | ||
} | ||
|
||
} | ||
|
||
const test = new Game({ | ||
title: "test", | ||
width: 800, | ||
height: 600, | ||
centered: true, | ||
fullscreen: false, | ||
hidden: false, | ||
resizable: true, | ||
minimized: false, | ||
maximized: false, | ||
flags: null, | ||
}); | ||
|
||
await test.start(); |
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,4 +1,4 @@ | ||
export { World } from './src/World.ts'; | ||
export { Entity, Rectangle, Line, Point, Sprite, Text } from './src/entities/mod.ts'; | ||
export { Entity, Rectangle, Line, Point, Image, Sprite, Text } from './src/entities/mod.ts'; | ||
export { Keys } from './src/utils/Keycodes.ts'; | ||
export type { KeyEvent } from "./src/types.ts" |
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,4 +1,4 @@ | ||
export { Entity } from './Entity.ts'; | ||
export { Rectangle, Point, Line } from './geometry/mod.ts'; | ||
export { Sprite } from './sprites/mod.ts'; | ||
export { Sprite, Image } from './sprites/mod.ts'; | ||
export { Text } from "./text/mod.ts"; |
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,15 @@ | ||
import { Entity } from '../mod.ts'; | ||
import { World } from '../../../mod.ts'; | ||
export class Image extends Entity { | ||
public width: number; | ||
public height: number | ||
public surface: any; | ||
public texture: any; | ||
constructor(world: World, texture: string, x: number, y: number, width: number, height: number) { | ||
super(x, y); | ||
this.width = width; | ||
this.height = height; | ||
this.surface = world.loadSurface(texture); | ||
this.texture = world.createTextureFromSurface(this.surface); | ||
} | ||
} |
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,15 +1,44 @@ | ||
import { Entity } from '../mod.ts'; | ||
import { World } from '../../../mod.ts'; | ||
import { spriteConfig, Frame } from '../../types.ts'; | ||
import { generateFrames } from "../../utils/mod.ts"; | ||
export class Sprite extends Entity { | ||
public width: number; | ||
public height: number | ||
public surface: any; | ||
public texture: any; | ||
constructor(world: World, texture: string, x: number, y: number, width: number, height: number) { | ||
public frames: Array<Frame>; | ||
public frame: Frame; | ||
constructor( | ||
world: World, | ||
texture: string, | ||
x: number, | ||
y: number, | ||
width: number, | ||
height: number, | ||
{ rows, cols, }: spriteConfig, | ||
frame: number = 0, | ||
) { | ||
super(x, y); | ||
this.width = width; | ||
this.height = height; | ||
this.surface = world.loadSurface(texture); | ||
this.texture = world.createTextureFromSurface(this.surface); | ||
this.frames = generateFrames(this.width, this.height, rows, cols); | ||
this.frame = this.frames[frame]; | ||
|
||
} | ||
} | ||
|
||
public nextFrame(): void { | ||
this.frame = this.frames[(this.frames.indexOf(this.frame) + 1) % this.frames.length]; | ||
} | ||
public previousFrame(): void { | ||
this.frame = this.frames[(this.frames.indexOf(this.frame) - 1 + this.frames.length) % this.frames.length]; | ||
} | ||
public setFrame(frame: number): void { | ||
this.frame = this.frames[frame]; | ||
} | ||
|
||
} | ||
|
||
|
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,2 @@ | ||
export { Sprite } from './Sprite.ts'; | ||
export { Image } from './Image.ts'; | ||
export { Sprite } from './Sprite.ts'; |
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,9 +1,23 @@ | ||
import type { RGBA } from "../types.ts"; | ||
import type { RGBA, Frame } from "../types.ts"; | ||
export { Keys } from "./Keycodes.ts"; | ||
export const hexToRGBA = (hex: string): RGBA => | ||
[ | ||
parseInt(hex.slice(1, 3), 16), | ||
parseInt(hex.slice(3, 5), 16), | ||
parseInt(hex.slice(5, 7), 16), | ||
1 | ||
] | ||
export { Keys } from "./Keycodes.ts"; | ||
export const generateFrames = (width: number, height: number, rows: number, cols: number): Array<Frame> => { | ||
const frames = []; | ||
for (let i = 0; i < cols; i++) { | ||
for (let j = 0; j < rows; j++) { | ||
frames.push({ | ||
x: i * (width/cols), | ||
y: j * (height/rows), | ||
width: width / cols, | ||
height: height/rows | ||
}); | ||
} | ||
} | ||
return frames; | ||
} |