51 lines
1.2 KiB
Dart
51 lines
1.2 KiB
Dart
import 'dart:math';
|
|
import 'maze.dart';
|
|
|
|
class Player {
|
|
double x;
|
|
double y;
|
|
double angle;
|
|
static const double moveSpeed = 0.05;
|
|
static const double rotSpeed = 0.04;
|
|
|
|
Player({required this.x, required this.y, this.angle = 0});
|
|
|
|
void moveForward(Maze maze) {
|
|
_tryMove(maze, cos(angle) * moveSpeed, sin(angle) * moveSpeed);
|
|
}
|
|
|
|
void moveBackward(Maze maze) {
|
|
_tryMove(maze, -cos(angle) * moveSpeed, -sin(angle) * moveSpeed);
|
|
}
|
|
|
|
void strafeLeft(Maze maze) {
|
|
_tryMove(maze, cos(angle - pi / 2) * moveSpeed, sin(angle - pi / 2) * moveSpeed);
|
|
}
|
|
|
|
void strafeRight(Maze maze) {
|
|
_tryMove(maze, cos(angle + pi / 2) * moveSpeed, sin(angle + pi / 2) * moveSpeed);
|
|
}
|
|
|
|
void rotateLeft() {
|
|
angle -= rotSpeed;
|
|
}
|
|
|
|
void rotateRight() {
|
|
angle += rotSpeed;
|
|
}
|
|
|
|
void _tryMove(Maze maze, double dx, double dy) {
|
|
const margin = 0.2;
|
|
final nx = x + dx;
|
|
final ny = y + dy;
|
|
if (!maze.isWall((nx + margin).toInt(), y.toInt()) &&
|
|
!maze.isWall((nx - margin).toInt(), y.toInt())) {
|
|
x = nx;
|
|
}
|
|
if (!maze.isWall(x.toInt(), (ny + margin).toInt()) &&
|
|
!maze.isWall(x.toInt(), (ny - margin).toInt())) {
|
|
y = ny;
|
|
}
|
|
}
|
|
}
|