Add collectable items

This commit is contained in:
asraelite 2018-03-05 23:53:32 +00:00
parent 826986cdbf
commit 69fee5e73d
10 changed files with 131 additions and 52 deletions

View file

@ -24,6 +24,15 @@ export default class Body {
return Math.sqrt(this.xvel ** 2 + this.yvel ** 2);
}
getCelestialCollision(celestials) {
let result = false;
celestials.forEach(c => {
let dis = this.distanceTo(c);
if (dis < c.radius) result = c;
});
return result;
}
getWorldPoint(lx, ly) {
let [cx, cy] = this.localCom;
let [nx, ny] = this.rotateVector(lx - cx, ly - cy, this.r);
@ -55,7 +64,7 @@ export default class Body {
tickGravity(bodies) {
bodies.forEach(b => {
let force = b.mass / this.mass / (this.distanceTo(b) ** 2) * G;
let force = b.mass / (this.distanceTo(b) ** 2) * G;
let [[ax, ay], [bx, by]] = [this.com, b.com];
let angle = Math.atan2(by - ay, bx - ax);
this.xvel += Math.cos(angle) * force;

View file

@ -1,64 +1,59 @@
import Body from './body.mjs';
import {modules} from '../data.mjs';
import {celestials, particles} from './index.mjs';
import {playerShip} from './index.mjs';
import {images as assets} from '../assets.mjs';
import {celestials, particles, entities} from './index.mjs';
import * as particle from './particle.mjs';
import * as consts from '../consts.mjs';
import * as events from '../game/events.mjs';
export function createThrustExhaust(thruster) {
let ship = thruster.ship;
let [fx, fy] = ship.relativeVector(0, 0.2);
let [dx, dy] = ship.relativeVector((Math.random() - 0.5) * 0.5, 0.5);
let [cx, cy] = thruster.com;
particles.add(new Particle(cx + dx, cy + dy, {
xvel: ship.xvel + fx,
yvel: ship.yvel + fy,
color: '#f4c542',
lifetime: 5,
size: 0.07
}));
}
class Particle extends Body {
constructor(x, y, {
export default class Entity extends Body {
constructor(x, y, type = 'fuel', id = 'small', {
xvel = 0,
yvel = 0,
spray = 0.1,
fizzle = 0,
maxFizzle = fizzle * 2,
color = '#fff',
gravity = false,
lifetime = 50,
size = 0.1,
friction = 0.99
}) {
super(x, y, 0.1);
gravity = false
} = {}) {
super(x, y, 100);
this.size = size;
this.xvel = xvel + (Math.random() - 0.5) * spray;
this.yvel = yvel + (Math.random() - 0.5) * spray;
this.friction = friction;
this.fizzle = fizzle;
this.maxFizzle = maxFizzle;
this.color = color;
this.xvel = xvel;
this.yvel = yvel;
this.width = 1;
this.height = 1;
this.radius = (this.width + this.height) / 2;
this.type = type;
this.id = id;
this.image = assets.modules[type][id];
this.gravity = gravity;
this.life = lifetime;
this.touched = false;
}
get com() {
return [this.x - this.size / 2, this.y - this.size / 2];
return [this.x + this.width / 2, this.y + this.height / 2];
}
orbit(celestial, radius) {
this.gravity = true;
}
remove() {
entities.delete(this);
}
tick() {
if (!this.life--) {
particles.delete(this);
return;
}
this.r += consts.ENTITY_ROTATION_RATE;
this.tickMotion();
if (this.gravity) this.tickGravity(celestials);
let col = this.getCelestialCollision(celestials);
this.xvel *= this.friction;
this.yvel *= this.friction;
this.x += (Math.random() - 0.5) * this.fizzle;
this.y += (Math.random() - 0.5) * this.fizzle;
if (this.fizzle < this.mazFizzle) this.fizzle *= 1.05;
if (col !== false) {
this.remove();
}
if (playerShip.colliding(this.com, this.radius)) {
let success = events.collectItem(this.type, this.id);
if (!success) return;
particle.createPickupBurst(playerShip, this.com);
this.remove();
}
}
}

View file

@ -21,6 +21,7 @@ export function init() {
particles.clear();
spawn.player();
spawn.startPlanet();
spawn.testEntity();
}
export function tick() {

View file

@ -15,6 +15,31 @@ export function createThrustExhaust(thruster) {
}));
}
export function createPickupBurst(ship, point) {
for (let i = 0; i < 20; i++) {
particles.add(new Particle(...point, {
xvel: ship.xvel,
yvel: ship.yvel,
color: '#eae55d',
lifetime: Math.random() * 20 + 15,
friction: 0.95,
size: Math.random() * 0.2 + 0.05,
spray: 0.3
}));
}
}
export function createItemToss(ship) {
particles.add(new Particle(...ship.com, {
xvel: ship.xvel,
yvel: ship.yvel,
color: '#a87234',
lifetime: 50,
size: 0.6,
spray: 0.4
}));
}
class Particle extends Body {
constructor(x, y, {
xvel = 0,
@ -46,7 +71,7 @@ class Particle extends Body {
}
tick() {
if (!this.life--) {
if (this.life-- <= 0) {
particles.delete(this);
return;
}

View file

@ -83,6 +83,19 @@ export default class Ship extends Body {
Math.max(Math.sqrt((bx - lx) ** 2 + (by - ly) ** 2), a), 0) + 1;
}
colliding(point, radius) {
let [px, py] = point;
let result = false;
this.modules.forEach(m => {
let [mx, my] = this.getWorldPoint(...m.localCom);
let dis = Math.sqrt((py - my) ** 2 + (px - mx) ** 2);
if (dis < radius) result = true;
});
return result;
}
resolveCollisions() {
this.landed = false;
@ -127,8 +140,11 @@ export default class Ship extends Body {
applyThrust({ forward = 0, left = 0, right = 0, back = 0,
turnLeft = 0, turnRight = 0}) {
let turnForce = (turnRight - turnLeft) / 20;
this.applyDirectionalForce(0, -forward / 30, turnForce);
let thrustForce = -forward * consts.THRUST_POWER;
let turnForce = (turnRight - turnLeft) * consts.TURN_POWER;
this.applyDirectionalForce(0, thrustForce, turnForce);
this.modules.forEach(m => {
if (m.type !== 'thruster') return;

View file

@ -1,6 +1,7 @@
import Ship from './ship.mjs';
import Module from './module.mjs';
import Celestial from './celestial.mjs';
import Entity from './entity.mjs';
import {modules} from '../data.mjs';
import * as world from './index.mjs';
@ -16,11 +17,17 @@ export function player() {
export function startPlanet() {
return celestial(0, 0, 40, {
density: 10,
density: 3,
type: 'green'
});
}
export function testEntity() {
let entity = new Entity(0, -50);
world.entities.add(entity);
return entity;
}
export function celestial(x, y, radius, params) {
let celestial = new Celestial(x - radius, y - radius, radius, params);
world.celestials.add(celestial);