-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add rectangle plugin and example
- Loading branch information
1 parent
e1005a6
commit d545424
Showing
11 changed files
with
250 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<link rel="icon" type="image/svg+xml" href="/vite.svg" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<link | ||
rel="stylesheet" | ||
href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/styles/default.min.css" | ||
/> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/highlight.min.js"></script> | ||
|
||
<!-- and it's easy to individually load additional languages --> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/languages/go.min.js"></script> | ||
<script src="https://unpkg.com/highlightjs-copy/dist/highlightjs-copy.min.js"></script> | ||
<link | ||
rel="stylesheet" | ||
href="https://unpkg.com/highlightjs-copy/dist/highlightjs-copy.min.css" | ||
/> | ||
<link rel="stylesheet" href="/styles.css" /> | ||
|
||
<script> | ||
hljs.addPlugin(new CopyButtonPlugin()); | ||
hljs.highlightAll(); | ||
</script> | ||
<title>MiniPoint Tennis</title> | ||
</head> | ||
<body> | ||
<div id="app">A Tennis of two | ||
<div class="main-area"> | ||
<canvas id="canvas"></canvas> | ||
</div> | ||
<script type="module" src="./script.ts"></script> | ||
</body> | ||
</html> |
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,86 @@ | ||
import { Engine, Point, Line, Rectangle } from '../../../lib'; | ||
|
||
const engine = new Engine( | ||
document.getElementById('canvas') as HTMLCanvasElement, | ||
{ | ||
engineOptions: { | ||
width: 800, | ||
height: 600, | ||
clearEachFrame: true, | ||
}, | ||
}, | ||
); | ||
|
||
// layout - line in the middle | ||
const middleLine = new Line({ | ||
x1: 400, | ||
y1: 0, | ||
x2: 400, | ||
y2: 600, | ||
color: 'blue', | ||
stroke: 2, | ||
}); | ||
|
||
const player = new Rectangle({ | ||
x: 5, | ||
y: 0, | ||
width: 10, | ||
height: 50, | ||
fill: true, | ||
fillColor: 'black', | ||
}); | ||
|
||
let yAxisInput = 0; | ||
const speed = 300; | ||
|
||
const ball = new Point({ | ||
x: 400, | ||
y: 300, | ||
radius: 10, | ||
color: 'red', | ||
}); | ||
|
||
// ball velocity | ||
let ballXVelocity = Math.random() - 100; | ||
let ballYVelocity = Math.random() + 100; | ||
|
||
engine.update = (_, deltaTime) => { | ||
if (engine.input.keyboard.currentDownKey === 'arrowup') { | ||
yAxisInput = -1; | ||
} else if (engine.input.keyboard.currentDownKey === 'arrowdown') { | ||
yAxisInput = 1; | ||
} else { | ||
yAxisInput = 0; | ||
} | ||
|
||
player.options.y += yAxisInput * speed * deltaTime; | ||
|
||
// stop the player on the top and bottom | ||
if (player.options.y < 0) { | ||
player.options.y = 0; | ||
} else if (player.options.y > 550) { | ||
player.options.y = 550; | ||
} | ||
|
||
// ball movement | ||
ball.options.x += ballXVelocity * deltaTime; | ||
ball.options.y += ballYVelocity * deltaTime; | ||
|
||
if (ball.options.y < 0 || ball.options.y > 600) { | ||
ballYVelocity = -ballYVelocity; | ||
} | ||
|
||
if (ball.options.x < 0 || ball.options.x > 800) { | ||
ballXVelocity = -ballXVelocity; | ||
} | ||
|
||
// ball collision with player | ||
if ( | ||
ball.options.x < player.options.x + player.options.width && | ||
ball.options.x > player.options.x && | ||
ball.options.y < player.options.y + player.options.height && | ||
ball.options.y > player.options.y | ||
) { | ||
ballXVelocity = -ballXVelocity; | ||
} | ||
}; |
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
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 |
---|---|---|
|
@@ -7,3 +7,4 @@ | |
*/ | ||
|
||
export * from './line'; | ||
export * from './rectangle'; |
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,47 @@ | ||
import { | ||
BaseObject, | ||
DefaultDrawableObjectOptions, | ||
DrawableObjectOptions, | ||
} from '../render'; | ||
import { Renderer } from '../render/renderer'; | ||
|
||
export type RectangleOptions = DrawableObjectOptions<{ | ||
x: number; | ||
y: number; | ||
width: number; | ||
height: number; | ||
color?: string; | ||
stroke?: number; | ||
fill: boolean; | ||
fillColor: string; | ||
}>; | ||
|
||
export class Rectangle extends BaseObject<RectangleOptions, Rectangle> { | ||
constructor(options: RectangleOptions, renderer?: Renderer) { | ||
super(options, renderer); | ||
this.options = { | ||
...DefaultDrawableObjectOptions, | ||
...options, | ||
}; | ||
} | ||
override draw() { | ||
this.context.beginPath(); | ||
this.context.rect( | ||
this.options.x, | ||
this.options.y, | ||
this.options.width, | ||
this.options.height, | ||
); | ||
|
||
if (this.options.fill) { | ||
this.context.fillStyle = this.options.fillColor; | ||
this.context.fill(); | ||
} | ||
|
||
if (this.options.color) this.context.strokeStyle = this.options.color; | ||
if (this.options.stroke) this.context.lineWidth = this.options.stroke; | ||
this.context.stroke(); | ||
|
||
return this; | ||
} | ||
} |
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