From f98bba041f386312b4a231372ae702e1f7e94199 Mon Sep 17 00:00:00 2001 From: zenoamaro Date: Sun, 30 Jun 2019 16:37:12 +0200 Subject: [PATCH] Chooser AI --- src/ai.js | 18 +++++++++++++++--- src/main.js | 24 +++++++++++++----------- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/ai.js b/src/ai.js index c1f028f..0d8f08b 100644 --- a/src/ai.js +++ b/src/ai.js @@ -4,11 +4,23 @@ import {randomInt} from './math.js'; export const AI_TYPES = { moron: { - decide() { + decide(player) { return { a: randomInt(0, 180), - p: randomInt(PLAYER_INITIAL_POWER, PLAYER_MAX_ENERGY*PLAYER_ENERGY_POWER_MULTIPLIER)} - } + p: randomInt(PLAYER_INITIAL_POWER, PLAYER_MAX_ENERGY*PLAYER_ENERGY_POWER_MULTIPLIER), + currentWeapon: player.currentWeapon, + }; + }, + }, + + chooser: { + decide(player) { + return { + a: randomInt(0, 180), + p: randomInt(PLAYER_INITIAL_POWER, PLAYER_MAX_ENERGY*PLAYER_ENERGY_POWER_MULTIPLIER), + currentWeapon: randomInt(0, player.weapons.length-1), + }; + }, }, }; diff --git a/src/main.js b/src/main.js index ba36a4c..4a9eda3 100644 --- a/src/main.js +++ b/src/main.js @@ -47,7 +47,7 @@ for (let [color, borderColor] of PLAYER_COLORS) { weapons: [...PLAYER_STARTING_WEAPONS], currentWeapon: 0, energy: PLAYER_MAX_ENERGY, - ai: i !== 0 ? 'moron' : undefined, + ai: i !== 0 ? sample(Object.keys(AI_TYPES)) : undefined, fallHeight: 0, dead: false, }); @@ -71,7 +71,7 @@ function update() { else if (state === 'aim') { const player = players[currentPlayer]; - const {x, y, a, p, energy} = player; + const {x, y, a, p, weapons, energy} = player; const maxPower = energy * PLAYER_ENERGY_POWER_MULTIPLIER; const isPrecise = key('Alt'); const isFast = key('Shift'); @@ -80,10 +80,11 @@ function update() { if (player.ai) { let ai = AI_TYPES[player.ai]; - const plan = ai.decide(); - const a = player.a = wrap(0, plan.a, 180); - const p = player.p = clamp(0, plan.p, maxPower); - shoot = {a, p}; + const plan = ai.decide(player); + player.a = wrap(0, plan.a, 180); + player.p = clamp(0, plan.p, maxPower); + player.currentWeapon = clamp(0, plan.currentWeapon, weapons.length-1); + shoot = true; } else if (key('ArrowLeft')) { @@ -125,19 +126,20 @@ function update() { } if (shoot) { - const {a, p} = shoot; + const {a, p, c, weapons, currentWeapon} = player; const [px, py] = vec(x, y-3, a+180, 5); - const weaponType = player.weapons[player.currentWeapon]; + const weaponType = weapons[currentWeapon]; weaponType.ammo -= 1; projectile = { osc: createOsc('sine'), player: player, weaponTypeId: weaponType.type, - ox:px, oy:py, a, p, - x:px, y:py, t: 0, - c: player.c + ox: px, oy: py, + x: px, y: py, + a, p, c, + t: 0, }; projectile.osc.start();