Initial commit: labirynt 3D pseudo-raycasting game
This commit is contained in:
50
lib/player.dart
Normal file
50
lib/player.dart
Normal file
@@ -0,0 +1,50 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user