mirror of https://github.com/nkoehring/starsy.git
move object logic to composable
parent
1cdf93c48f
commit
72dd514322
@ -1,38 +1,44 @@
|
||||
export default [
|
||||
{ type: 'planet', name: 'Mercury', radius: 1, distance: 100, satellites: [], rings: 0 },
|
||||
{ type: 'planet', name: 'Venus', radius: 4, distance: 120, satellites: [], rings: 0 },
|
||||
{ type: 'planet', name: 'Terra', radius: 4, distance: 140, satellites: [
|
||||
{ name: 'ISS', radius: 1, type: 'station' },
|
||||
{ name: 'Luna', radius: 2, type: 'moon' },
|
||||
], rings: 0 },
|
||||
{ type: 'planet', name: 'Mars', radius: 2, distance: 160, satellites: [
|
||||
{ name: 'MTO', radius: 1, type: 'station' },
|
||||
{ name: 'Phobos', radius: 1, type: 'moon' },
|
||||
{ name: 'Daimos', radius: 1, type: 'moon' },
|
||||
], rings: 0 },
|
||||
{ type: 'planet', name: 'Jupiter', radius: 40, distance: 260, satellites: [
|
||||
{ name: 'Io', radius: 2, type: 'moon' },
|
||||
{ name: 'Europa', radius: 2, type: 'moon' },
|
||||
{ name: 'Ganymede', radius: 4, type: 'moon' },
|
||||
{ name: 'Callisto', radius: 3, type: 'moon' },
|
||||
], rings: 1 },
|
||||
{ type: 'planet', name: 'Saturn', radius: 36, distance: 410, satellites: [
|
||||
{ name: 'Mimas', radius: 1, type: 'moon' },
|
||||
{ name: 'Enceladus', radius: 1, type: 'moon' },
|
||||
{ name: 'Tethys', radius: 1, type: 'moon' },
|
||||
{ name: 'Dione', radius: 1, type: 'moon' },
|
||||
{ name: 'Rhea', radius: 1, type: 'moon' },
|
||||
{ name: 'Titan', radius: 3, type: 'moon' },
|
||||
{ name: 'Iapetus', radius: 1, type: 'moon' },
|
||||
], rings: 5 },
|
||||
{ type: 'planet', name: 'Uranus', radius: 16, distance: 680, satellites: [
|
||||
{ name: 'Miranda', radius: 1, type: 'moon' },
|
||||
{ name: 'Ariel', radius: 1, type: 'moon' },
|
||||
{ name: 'Umbriel', radius: 1, type: 'moon' },
|
||||
{ name: 'Titania', radius: 1, type: 'moon' },
|
||||
{ name: 'Oberon', radius: 1, type: 'moon' },
|
||||
], rings: 2 },
|
||||
{ type: 'planet', name: 'Neptune', radius: 15, distance: 950, satellites: [
|
||||
{ name: 'Triton', radius: 1, type: 'moon' },
|
||||
], rings: 0 },
|
||||
]
|
||||
export default {
|
||||
star: {
|
||||
designation: 'Sol',
|
||||
radius: 400,
|
||||
},
|
||||
objects: [
|
||||
{ type: 'planet', name: 'Mercury', designation: 'Sol-1', radius: 1, distance: 100, satellites: [], rings: 0 },
|
||||
{ type: 'planet', name: 'Venus', designation: 'Sol-2', radius: 4, distance: 120, satellites: [], rings: 0 },
|
||||
{ type: 'planet', name: 'Terra', designation: 'Sol-3', radius: 4, distance: 140, satellites: [
|
||||
{ name: 'ISS', radius: 1, type: 'station' },
|
||||
{ name: 'Luna', radius: 2, type: 'moon' },
|
||||
], rings: 0 },
|
||||
{ type: 'planet', name: 'Mars', designation: 'Sol-4', radius: 2, distance: 160, satellites: [
|
||||
{ name: 'MTO', radius: 1, type: 'station' },
|
||||
{ name: 'Phobos', radius: 1, type: 'moon' },
|
||||
{ name: 'Daimos', radius: 1, type: 'moon' },
|
||||
], rings: 0 },
|
||||
{ type: 'planet', name: 'Jupiter', designation: 'Sol-5', radius: 40, distance: 260, satellites: [
|
||||
{ name: 'Io', radius: 2, type: 'moon' },
|
||||
{ name: 'Europa', radius: 2, type: 'moon' },
|
||||
{ name: 'Ganymede', radius: 4, type: 'moon' },
|
||||
{ name: 'Callisto', radius: 3, type: 'moon' },
|
||||
], rings: 1 },
|
||||
{ type: 'planet', name: 'Saturn', designation: 'Sol-6', radius: 36, distance: 410, satellites: [
|
||||
{ name: 'Mimas', radius: 1, type: 'moon' },
|
||||
{ name: 'Enceladus', radius: 1, type: 'moon' },
|
||||
{ name: 'Tethys', radius: 1, type: 'moon' },
|
||||
{ name: 'Dione', radius: 1, type: 'moon' },
|
||||
{ name: 'Rhea', radius: 1, type: 'moon' },
|
||||
{ name: 'Titan', radius: 3, type: 'moon' },
|
||||
{ name: 'Iapetus', radius: 1, type: 'moon' },
|
||||
], rings: 5 },
|
||||
{ type: 'planet', name: 'Uranus', designation: 'Sol-7', radius: 16, distance: 680, satellites: [
|
||||
{ name: 'Miranda', radius: 1, type: 'moon' },
|
||||
{ name: 'Ariel', radius: 1, type: 'moon' },
|
||||
{ name: 'Umbriel', radius: 1, type: 'moon' },
|
||||
{ name: 'Titania', radius: 1, type: 'moon' },
|
||||
{ name: 'Oberon', radius: 1, type: 'moon' },
|
||||
], rings: 2 },
|
||||
{ type: 'planet', name: 'Neptune', designation: 'Sol-8', radius: 15, distance: 950, satellites: [
|
||||
{ name: 'Triton', radius: 1, type: 'moon' },
|
||||
], rings: 0 },
|
||||
],
|
||||
}
|
||||
|
@ -0,0 +1,117 @@
|
||||
import { reactive, ref, computed } from 'vue'
|
||||
import {
|
||||
MIN_SIZE_STAR,
|
||||
MAX_SIZE_STAR,
|
||||
MIN_SIZE_PLANET,
|
||||
MAX_SIZE_PLANET,
|
||||
MIN_DISTANCE_PLANET,
|
||||
MAX_DISTANCE_PLANET,
|
||||
MIN_AMOUNT_RINGS,
|
||||
MAX_AMOUNT_RINGS,
|
||||
} from './constants'
|
||||
import { steepCurve } from './utils'
|
||||
import exampleData from './example-data.js'
|
||||
|
||||
|
||||
const star = reactive(exampleData.star)
|
||||
const objects = reactive(exampleData.objects)
|
||||
const selectedObject = ref(null)
|
||||
const deletedObject = ref(null) // { index: Number, object: Object }
|
||||
|
||||
const starCX = computed(() => {
|
||||
const r = star.radius
|
||||
return -1 * r * steepCurve(r, 50, 0.955)
|
||||
})
|
||||
|
||||
export default function useObjects () {
|
||||
|
||||
function addObject() {
|
||||
const amount = objects.length
|
||||
let distance = 100
|
||||
|
||||
if (amount) {
|
||||
const lastObject = objects[amount - 1]
|
||||
distance = Math.min(MAX_DISTANCE_PLANET, lastObject.distance + 2*lastObject.radius + 10)
|
||||
}
|
||||
|
||||
objects.push({
|
||||
type: 'planet',
|
||||
name: `${star.designation}-${amount + 1}`,
|
||||
radius: 1,
|
||||
distance,
|
||||
satellites: [],
|
||||
rings: 0,
|
||||
})
|
||||
}
|
||||
|
||||
function updateSelectedObject (payload) {
|
||||
if (payload.name && !payload.name.trim().length) {
|
||||
payload.name = selectedObject.value.designation
|
||||
}
|
||||
if (payload.distance) {
|
||||
payload.distance = Math.max(MIN_DISTANCE_PLANET, payload.distance)
|
||||
payload.distance = Math.min(MAX_DISTANCE_PLANET, payload.distance)
|
||||
}
|
||||
if (payload.radius) {
|
||||
payload.radius = Math.max(MIN_SIZE_PLANET, payload.radius)
|
||||
payload.radius = Math.min(MAX_SIZE_PLANET, payload.radius)
|
||||
}
|
||||
if (payload.rings) {
|
||||
payload.rings = Math.max(MIN_AMOUNT_RINGS, payload.rings)
|
||||
payload.rings = Math.min(MAX_AMOUNT_RINGS, payload.rings)
|
||||
}
|
||||
for (const key in payload) {
|
||||
selectedObject.value[key] = payload[key]
|
||||
}
|
||||
}
|
||||
|
||||
function deleteObject (object) {
|
||||
if (deletedObject.value) {
|
||||
const lost = deletedObject.value.object.name
|
||||
const confirmed = confirm(`
|
||||
Attention! Only the LAST deleted object can be restored.
|
||||
${lost} will be lost forever! Proceed anyway?`
|
||||
)
|
||||
if (!confirmed) return
|
||||
}
|
||||
|
||||
if (!object) object = selectedObject.value
|
||||
const index = objects.indexOf(object)
|
||||
|
||||
console.debug('deleting object at index', index)
|
||||
|
||||
if (index >= 0) objects.splice(index, 1)
|
||||
if (object === selectedObject.value) selectedObject.value = null
|
||||
|
||||
deletedObject.value = { index, object }
|
||||
}
|
||||
|
||||
function restoreDeleted () {
|
||||
const { index, object } = deletedObject.value
|
||||
console.debug('restoring deleted object', index)
|
||||
objects.splice(index, 0, object)
|
||||
deletedObject.value = null
|
||||
}
|
||||
|
||||
function autoName (object) {
|
||||
const index = objects.indexOf(object)
|
||||
return `${star.designation}-${index}`
|
||||
}
|
||||
|
||||
function randomizeObject (object) {
|
||||
}
|
||||
|
||||
return {
|
||||
star,
|
||||
starCX,
|
||||
objects,
|
||||
selectedObject,
|
||||
deletedObject,
|
||||
addObject,
|
||||
deleteObject,
|
||||
updateSelectedObject,
|
||||
restoreDeleted,
|
||||
randomizeObject,
|
||||
autoName,
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue