new weapon

This commit is contained in:
Yanick Champoux 2022-03-06 17:36:50 -05:00
parent fd72aafec0
commit b40efe242e
5 changed files with 114 additions and 65 deletions

View File

@ -1,30 +1,4 @@
export function weapon_cost_mass(weapon){
let cost = 0;
let mass = 0;
if( weapon.weapon_type === 'beam' ) {
return beam_cost_mass(weapon);
}
if( weapon.weapon_type == 'submunition' ) {
return { mass: 1, cost: 3 };
}
if( weapon.weapon_type === 'pds' ) {
return { mass: 1, cost: 3 };
}
if( weapon.weapon_type === 'scattergun' ) {
return { mass: 1, cost: 4 };
}
if( weapon.weapon_type === 'needle' ) {
return { mass: 2, cost: 6 };
}
return { cost, mass };
}
const is_broadside = arcs => { const is_broadside = arcs => {
if( arcs.length !== 4 ) return false; if( arcs.length !== 4 ) return false;
@ -33,42 +7,3 @@ const is_broadside = arcs => {
return !arcs.some( a => a.length === 1 ); return !arcs.some( a => a.length === 1 );
} }
function beam_cost_mass({weapon_class, arcs}) {
let mass;
if( weapon_class === 1 ) {
mass = 1;
}
if( weapon_class == 2 ) {
mass = 2 + (arcs.length > 3 ? 1 : 0);
}
if( weapon_class == 3 ) {
mass = 4;
if( is_broadside(arcs) ) {
mass += 2;
}
else {
mass += arcs.length - 1;
}
}
if( weapon_class == 4 ) {
mass = 8;
if( is_broadside(arcs) ) {
mass += 4;
}
else {
mass += 2*(arcs.length - 1);
}
}
return {
mass, cost: 3 * mass
}
}

View File

@ -0,0 +1,22 @@
<Field label="weapon type">
<select bind:value={type}>
{#each weaponTypes as weapon (weapon.type)}
<option value={weapon.type}>{weapon.name}</option>
{/each}
</select>
<button class="button small blue" on:click={addWeapon} >add weapon</button>
</Field>
<script>
import { getContext } from "svelte";
import Field from "../../Field/index.svelte";
import { weaponTypes } from '$lib/shipDux/weaponry/weapons.js';
export let ship = getContext("ship");
let type = weaponTypes[0].value;
const addWeapon = () => ship.dispatch.addWeapon(type);
</script>

View File

@ -2,6 +2,8 @@
<Firecons {...firecons} /> <Firecons {...firecons} />
<ADFC {...adfc} /> <ADFC {...adfc} />
<AddWeapon />
</Section> </Section>
<script> <script>
@ -13,10 +15,13 @@ import Field from '$lib/components/Field/index.svelte';
import Firecons from './Firecons.svelte'; import Firecons from './Firecons.svelte';
import ADFC from './ADFC.svelte'; import ADFC from './ADFC.svelte';
import AddWeapon from './AddWeapon.svelte';
export let firecons = {}; export let firecons = {};
export let adfc = {}; export let adfc = {};
export let weapons = [];
</script> </script>
<style> <style>

View File

@ -1,9 +1,12 @@
import { Updux } from "updux"; import { Updux } from "updux";
import u from "updeep"; import u from "updeep";
import weapons from './weapons.js';
const reqs = { cost: 0, mass: 0 }; const reqs = { cost: 0, mass: 0 };
const dux = new Updux({ const dux = new Updux({
subduxes: { weapons },
initial: { initial: {
firecons: { firecons: {
stations: 0, stations: 0,

View File

@ -0,0 +1,84 @@
import { Updux } from "updux";
import u from "updeep";
const reqs = { cost: 0, mass: 0 };
export const weaponTypes = [
{ name: 'beam', type: 'beam', reqs: beam_cost_mass },
{ name: 'submunition pack', type: 'submunition', reqs: { mass:1, cost:3 }},
{ name: 'point defence system', type: 'pds', reqs: {mass:1,cost:3}},
{ name: 'scattergun', type: 'scattergun', reqs: { mass:1,cost:4 }},
{ name: 'needle weapon', type: 'needle', reqs: { mass: 2, cost: 6 }},
];
const dux = new Updux({
initial: [],
actions: {
addWeapon: null
},
});
dux.setMutation('addWeapon', type => state => {
return [
...state,
{
id: state.length +1,
type: weaponTypes[0].type,
reqs: weaponReqs(weaponTypes[0].type),
}
]
});
function weaponReqs(weapon) {
const {reqs} = weaponTypes.find( wt => wt.type === weapon.type ) ||{};
if(!reqs) return {};
if( typeof reqs === 'function' ) return reqs(weapon);
return reqs;
}
function beam_cost_mass({weapon_class, arcs}) {
let mass;
if( weapon_class === 1 ) {
mass = 1;
}
if( weapon_class == 2 ) {
mass = 2 + (arcs.length > 3 ? 1 : 0);
}
if( weapon_class == 3 ) {
mass = 4;
if( is_broadside(arcs) ) {
mass += 2;
}
else {
mass += arcs.length - 1;
}
}
if( weapon_class == 4 ) {
mass = 8;
if( is_broadside(arcs) ) {
mass += 4;
}
else {
mass += 2*(arcs.length - 1);
}
}
return {
mass, cost: 3 * mass
}
}
export default dux;