[game] Collision detection
This commit is contained in:
parent
2388acaa94
commit
38f5f95827
6 changed files with 511 additions and 242 deletions
|
|
@ -2,20 +2,42 @@ import { load_texture } from "./assets";
|
|||
import { btn, Button } from "./input";
|
||||
import { Vec2, new_v2, vadd, vsub, vnorm, vmul } from "./vector";
|
||||
import { spr, use_texture, Texture } from "./graphics";
|
||||
import { has_collision, Level } from "./level";
|
||||
|
||||
export interface ActorProps {
|
||||
id: string;
|
||||
|
||||
cx: number;
|
||||
cy: number;
|
||||
xr: number;
|
||||
yr: number;
|
||||
|
||||
position: Vec2;
|
||||
velocity: Vec2;
|
||||
friction: number;
|
||||
id: string;
|
||||
collide_radius: number;
|
||||
}
|
||||
|
||||
export function new_actor_props(id: string, position: Vec2): ActorProps {
|
||||
export function new_actor_props(
|
||||
id: string,
|
||||
position: Vec2,
|
||||
collide_radius: number
|
||||
): ActorProps {
|
||||
const cx = Math.trunc(position.x / 16);
|
||||
const cy = Math.trunc(position.y / 16);
|
||||
const xr = (position.x - cx * 16) / 16;
|
||||
const yr = (position.y - cy * 16) / 16;
|
||||
|
||||
return {
|
||||
cx,
|
||||
cy,
|
||||
xr,
|
||||
yr,
|
||||
velocity: new_v2(0),
|
||||
friction: 0.6,
|
||||
friction: 0.7,
|
||||
id,
|
||||
position,
|
||||
collide_radius,
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -30,9 +52,15 @@ export class Actor {
|
|||
|
||||
update() {}
|
||||
|
||||
update_physics() {
|
||||
const velocity = vmul(this.props.velocity, this.props.friction);
|
||||
// Zero if we're smaller than some epsilon.
|
||||
// IDEAS: Make gameplay logic at 30fps and render updates at 60fps? Does
|
||||
// this mean we do some "update" in render?
|
||||
//
|
||||
// TODO: Update physics in a better kind of way rather than an object call.
|
||||
update_physics(level: Level | undefined) {
|
||||
// This is very nice: https://deepnight.net/tutorial/a-simple-platformer-engine-part-1-basics/
|
||||
// Apply friction to velocity and zero if we're close enough to zero.
|
||||
const props = this.props;
|
||||
const velocity = vmul(props.velocity, props.friction);
|
||||
if (Math.abs(velocity.x) < 0.01) {
|
||||
velocity.x = 0;
|
||||
}
|
||||
|
|
@ -40,13 +68,62 @@ export class Actor {
|
|||
velocity.y = 0;
|
||||
}
|
||||
|
||||
const new_position = vadd(this.props.position, velocity);
|
||||
let cx = props.cx;
|
||||
let cy = props.cy;
|
||||
|
||||
// Adjust xr with velocity, check for collisions, etc.
|
||||
let xr = props.xr + velocity.x;
|
||||
do {
|
||||
// TODO: Cap velocity to 1 tile/frame? Then we wouldn't need this loop...
|
||||
if (xr >= 0.7 && has_collision(level, cx + 1, cy)) {
|
||||
xr = 0.7;
|
||||
velocity.x = 0;
|
||||
}
|
||||
if (xr <= 0.3 && has_collision(level, cx - 1, cy)) {
|
||||
xr = 0.3;
|
||||
velocity.x = 0;
|
||||
}
|
||||
if (xr > 1) {
|
||||
cx += 1;
|
||||
xr -= 1;
|
||||
}
|
||||
if (xr < 0) {
|
||||
cx -= 1;
|
||||
xr += 1;
|
||||
}
|
||||
} while (xr > 1 || xr < 0);
|
||||
|
||||
let yr = props.yr + velocity.y;
|
||||
do {
|
||||
if (yr >= 0.4 && has_collision(level, cx, cy + 1)) {
|
||||
yr = 0.4;
|
||||
velocity.y = 0;
|
||||
}
|
||||
if (yr <= 0.1 && has_collision(level, cx, cy - 1)) {
|
||||
yr = 0.1;
|
||||
velocity.y = 0;
|
||||
}
|
||||
if (yr > 1) {
|
||||
cy += 1;
|
||||
yr -= 1;
|
||||
}
|
||||
if (yr < 0) {
|
||||
cy -= 1;
|
||||
yr += 1;
|
||||
}
|
||||
} while (yr > 1 || yr < 0);
|
||||
|
||||
// TODO: Collision detection
|
||||
// const { w, h } = this.bounds;
|
||||
|
||||
this.props.velocity = velocity;
|
||||
this.props.position = new_position;
|
||||
const new_position = new_v2((cx + xr) * 16, (cy + yr) * 16);
|
||||
|
||||
props.cx = cx;
|
||||
props.cy = cy;
|
||||
props.xr = xr;
|
||||
props.yr = yr;
|
||||
props.velocity = velocity;
|
||||
props.position = new_position;
|
||||
}
|
||||
|
||||
draw(_clock: number) {}
|
||||
|
|
@ -55,7 +132,7 @@ export class Actor {
|
|||
}
|
||||
|
||||
const robo_info = {
|
||||
anchor: new_v2(16, 24), // Distance from upper-left of sprite.
|
||||
anchor: new_v2(16, 16), // Distance from upper-left of sprite.
|
||||
bounds: new_v2(32), // Width/height of sprite.
|
||||
sprite: "./bot.png",
|
||||
animations: [
|
||||
|
|
@ -90,7 +167,7 @@ export class Robo extends Actor {
|
|||
a.x += 1;
|
||||
}
|
||||
vnorm(a);
|
||||
this.props.velocity = vadd(this.props.velocity, vmul(a, 1.5));
|
||||
this.props.velocity = vadd(this.props.velocity, vmul(a, 0.06));
|
||||
}
|
||||
|
||||
draw(clock: number) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue