Created
January 26, 2021 17:24
-
-
Save apacheli/98572545eded62535794b29e6b3adc04 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| export default class Polygon { | |
| x = 0; | |
| y = 0; | |
| constructor(public width: number, public height: number) { | |
| } | |
| draw(context: CanvasRenderingContext2D, x: number, y: number) { | |
| } | |
| checkXPosition(context: CanvasRenderingContext2D, screenWidth: number) { | |
| if (this.x >= screenWidth) { | |
| this.x = 0; | |
| } | |
| else if (this.x <= -this.width) { | |
| this.x = screenWidth - this.width; | |
| } | |
| if (this.x + this.width > screenWidth) { | |
| this.draw(context, this.x - screenWidth, this.y); | |
| } | |
| else if (this.x + this.width < screenWidth - this.width) { | |
| this.draw(context, this.x + screenWidth, this.y); | |
| } | |
| } | |
| checkYPosition(context: CanvasRenderingContext2D, screenHeight: number) { | |
| if (this.y >= screenHeight) { | |
| this.y = 0; | |
| } | |
| else if (this.y <= -this.height) { | |
| this.y = screenHeight - this.height; | |
| } | |
| if (this.y + this.height > screenHeight) { | |
| this.draw(context, this.x, this.y - screenHeight); | |
| } | |
| else if (this.y + this.height < screenHeight - this.height) { | |
| this.draw(context, this.x, this.y + screenHeight); | |
| } | |
| } | |
| checkCorners(context: CanvasRenderingContext2D, screenWidth: number, screenHeight: number) { | |
| // Top left | |
| if (this.x < 0 && this.y < 0) { | |
| this.draw(context, this.x + screenWidth, this.y + screenHeight); | |
| } | |
| // Bottom left | |
| else if (this.x < 0 && this.y > screenHeight - this.height) { | |
| this.draw(context, this.x + screenWidth, this.y - screenHeight); | |
| } | |
| // Top right | |
| else if (this.x > screenWidth - this.width && this.y < 0) { | |
| this.draw(context, this.x - screenWidth, this.y + screenHeight); | |
| } | |
| // Bottom right | |
| else if (this.x > screenWidth - this.width && this.y > screenHeight - this.height) { | |
| this.draw(context, this.x - screenWidth, this.y - screenHeight); | |
| } | |
| } | |
| update(context: CanvasRenderingContext2D, screenWidth: number, screenHeight: number) { | |
| this.checkCorners(context, screenWidth, screenHeight); | |
| this.checkXPosition(context, screenWidth); | |
| this.checkYPosition(context, screenWidth); | |
| this.draw(context, this.x, this.y); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment