| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- import { coordinates } from "./coordinates.js";
- import { position } from "./position.js";
- import { moving_directions } from "./directions.js";
- import { rotating_directions } from "./directions.js";
- import { is_moving_direction } from "./directions.js";
- import { is_rotating_direction } from "./directions.js";
- class actor extends coordinates {
- #moving;
- #rotating;
- #speed;
- constructor() {
- super();
- this.#speed = 0.25;
- this.#moving = moving_directions.stop;
- this.#rotating = moving_directions.stop;
- }
- set speed(target) {
- if (typeof(target) !== "number") {
- throw new TypeError("Speed must be an number.");
- }
- this.#speed = target;
- }
- get speed() {
- return this.#speed;
- }
- rotate_cursor(cursor, size) {
- if (!(cursor instanceof position)) {
- throw new TypeError("Cursor compare must be an position object.");
- }
- if (!(size instanceof position)) {
- throw new TypeError("Size must be an position object.");
- }
- const rotate = cursor.x / size.x * 360;
- this.rotate_clockwise(rotate);
- }
- set moving(target) {
- if (!is_moving_direction(target)) {
- throw new TypeError("Direction must be in directions.");
- }
- this.#moving = target;
- }
- get moving() {
- return this.#moving;
- }
- set rotating(target) {
- if (!is_rotating_direction(target)) {
- throw new TypeError("Direction must be in directions.");
- }
- this.#rotating = target;
- }
- get rotating() {
- return this.#rotating;
- }
- update() {
- this.#update_position();
- this.#update_rotation();
- }
- #update_rotation() {
- switch (this.#rotating) {
- case rotating_directions.clockwise:
- this.rotate_clockwise(this.#speed * 10);
- return;
- case rotating_directions.counterclockwise:
- this.rotate_counterclockwise(this.#speed * 10);
- return;
- default:
- return;
- }
- }
- #update_position() {
- switch (this.#moving) {
- case moving_directions.front:
- this.move_front(this.#speed);
- return;
- case moving_directions.back:
- this.move_back(this.#speed);
- return;
- case moving_directions.left:
- this.move_left(this.#speed);
- return;
- case moving_directions.right:
- this.move_right(this.#speed);
- return;
- default:
- return;
- }
- }
- }
- export { actor };
|