Working torpedo

This commit is contained in:
aronmal 2023-05-26 18:24:19 +02:00
parent b3695916a0
commit d54db32980
Signed by: aronmal
GPG key ID: 816B7707426FC612
3 changed files with 32 additions and 26 deletions

View file

@ -13,8 +13,8 @@ import {
initlialTargetPreview, initlialTargetPreview,
initlialMouseCursor, initlialMouseCursor,
overlapsWithAnyBorder, overlapsWithAnyBorder,
isHit, isAlreadyHit,
composeTargetTiles, targetList,
} from "@lib/utils/helpers" } from "@lib/utils/helpers"
import { CSSProperties, useCallback } from "react" import { CSSProperties, useCallback } from "react"
import { useEffect, useReducer, useState } from "react" import { useEffect, useReducer, useState } from "react"
@ -33,22 +33,26 @@ function Gamefield() {
const settingTarget = useCallback( const settingTarget = useCallback(
(isGameTile: boolean, x: number, y: number) => { (isGameTile: boolean, x: number, y: number) => {
if (!isGameTile || isHit(x, y, hits).length) return const list = targetList(targetPreview, mode)
setTarget((t) => { if (
if (t.x === x && t.y === y && t.show) { !isGameTile ||
DispatchHits({ !list.filter(({ x, y }) => !isAlreadyHit(x, y, hits)).length
type: "fireMissile", )
payload: { hit: (x + y) % 2 !== 0, x, y }, return
}) if (target.show && target.x == x && target.y == y) {
return { preview: false, show: false, x, y } DispatchHits({
} else { type: "fireMissile",
const target = { show: true, x, y } payload: list.map(({ x, y }) => ({
if (overlapsWithAnyBorder(target, mode)) return t hit: isAlreadyHit(x, y, hits),
return target x,
} y,
}) })),
})
setTarget((t) => ({ ...t, show: false }))
} else if (!overlapsWithAnyBorder(targetPreview, mode))
setTarget({ show: true, x, y })
}, },
[hits, mode] [hits, mode, targetPreview]
) )
useEffect(() => { useEffect(() => {

View file

@ -30,12 +30,12 @@ export interface Hit extends Position {
} }
interface fireMissile { interface fireMissile {
type: "fireMissile" type: "fireMissile" | "htorpedo" | "vtorpedo"
payload: { payload: {
x: number x: number
y: number y: number
hit: boolean hit: boolean
} }[]
} }
interface removeMissile { interface removeMissile {
type: "removeMissile" type: "removeMissile"

View file

@ -28,8 +28,10 @@ export function fieldIndex(count: number, x: number, y: number) {
} }
export function hitReducer(formObject: Hit[], action: HitDispatch) { export function hitReducer(formObject: Hit[], action: HitDispatch) {
switch (action.type) { switch (action.type) {
case "fireMissile": { case "fireMissile":
const result = [...formObject, action.payload] case "htorpedo":
case "vtorpedo": {
const result = [...formObject, ...action.payload]
return result return result
} }
@ -61,8 +63,8 @@ function isBorder(x: number, y: number, count: number) {
return x < 2 || x > count + 1 || y < 2 || y > count + 1 return x < 2 || x > count + 1 || y < 2 || y > count + 1
} }
export function isHit(x: number, y: number, hits: Hit[]) { export function isAlreadyHit(x: number, y: number, hits: Hit[]) {
return hits.filter((h) => h.x === x && h.y === y) return !!hits.filter((h) => h.x === x && h.y === y).length
} }
function isSet(x: number, y: number, targetList: TargetList[], show: boolean) { function isSet(x: number, y: number, targetList: TargetList[], show: boolean) {
@ -72,7 +74,7 @@ function isSet(x: number, y: number, targetList: TargetList[], show: boolean) {
) )
} }
function targetList( export function targetList(
{ x: targetX, y: targetY }: Position, { x: targetX, y: targetY }: Position,
mode: number mode: number
): TargetList[] { ): TargetList[] {
@ -103,7 +105,7 @@ function targetList(
} }
export function overlapsWithAnyBorder(target: Position, mode: number) { export function overlapsWithAnyBorder(target: Position, mode: number) {
return targetList(target, mode).filter(({ x, y }) => isBorder(x, y, count)) return !!targetList(target, mode).filter(({ x, y }) => isBorder(x, y, count))
.length .length
} }
@ -118,7 +120,7 @@ export function composeTargetTiles(
return { return {
...targetItem, ...targetItem,
show, show,
imply: !!isHit(x, y, hits).length, imply: isAlreadyHit(x, y, hits),
} }
}) })
} }