From 3d6524bee2f01a6be03a00f0747af1ddcd33d1c3 Mon Sep 17 00:00:00 2001 From: ad044 Date: Fri, 4 Sep 2020 02:16:46 +0400 Subject: [PATCH] finished orb animation --- src/components/Game.tsx | 2 +- src/components/Hub.tsx | 8 +-- src/components/Orb.tsx | 121 ++++++++++++++++++++++++++--------- src/components/Starfield.tsx | 6 +- 4 files changed, 97 insertions(+), 40 deletions(-) diff --git a/src/components/Game.tsx b/src/components/Game.tsx index 70816a6..90dba43 100644 --- a/src/components/Game.tsx +++ b/src/components/Game.tsx @@ -236,8 +236,8 @@ const Game = () => { setTimeout(() => { setSpriteUpdateCooldown(false); }, 1000); - break; } + break; // only change sprite focus default: if (!spriteUpdateCooldown) { diff --git a/src/components/Hub.tsx b/src/components/Hub.tsx index 5df8eca..6daa14d 100644 --- a/src/components/Hub.tsx +++ b/src/components/Hub.tsx @@ -1,8 +1,8 @@ -import React, { Suspense, memo} from "react"; -import GrayRing from "./GrayRing"; -import PurpleRing from "./PurpleRing"; -import LevelSprite from "./LevelSprite"; +import React, { Suspense } from "react"; import level_sprites from "../resources/level_sprites.json"; +import GrayRing from "./GrayRing"; +import LevelSprite from "./LevelSprite"; +import PurpleRing from "./PurpleRing"; export type PositionAndScaleProps = [number, number, number]; export type RotationProps = [number, number, number, (string | undefined)?]; diff --git a/src/components/Orb.tsx b/src/components/Orb.tsx index fff2334..3ef24dd 100644 --- a/src/components/Orb.tsx +++ b/src/components/Orb.tsx @@ -1,53 +1,110 @@ -import React, { useRef, memo, useState } from "react"; +import React, { memo, useRef, useState } from "react"; +import { useFrame, useLoader } from "react-three-fiber"; import * as THREE from "three"; -import { useUpdate, useThree, useFrame, useLoader } from "react-three-fiber"; import orbSprite from "../static/sprites/orb.png"; -const curve = new THREE.QuadraticBezierCurve3( - new THREE.Vector3(1.2, 0, 0), - new THREE.Vector3(0.5, -0.8, 0), - new THREE.Vector3(-1.2, 1, 0) -); - -const points = curve.getPoints(100); - +// initialize outside the component otherwise it gets overwritten when it rerenders let orbIdx = 0; +let orbDirectionChangeCount = 0; const Orb = memo(() => { const orbRef = useRef(); const [orbDirection, setOrbDirection] = useState("left"); - const ref = useUpdate((geometry: any) => { - geometry.setFromPoints(points); - }, []); + const [currentCurve, setCurrentCurve] = useState("first"); const orbSpriteTexture: any = useLoader(THREE.TextureLoader, orbSprite); + // first one goes from up to down left to right + const firstCurve = new THREE.QuadraticBezierCurve3( + new THREE.Vector3(1.2, 0, 0), + new THREE.Vector3(0.5, -0.8, 0), + new THREE.Vector3(-1.2, 1, 0) + ); + + // second one goes from down to up left to right + const secondCurve = new THREE.QuadraticBezierCurve3( + new THREE.Vector3(-1.2, -0.8, 0), + new THREE.Vector3(-0.5, -0.1, 0), + new THREE.Vector3(1.2, 0.8, 0) + ); + useFrame(() => { - var orbPos = curve.getPoint(orbIdx / 250); - if (orbPos.x < -1.4) setOrbDirection("right"); - if (orbPos.x > 1.4) setOrbDirection("left"); - if (orbDirection === "left") { - orbIdx++; - } else { - orbIdx--; + let orbPosFirst = firstCurve.getPoint(orbIdx / 250); + let orbPosSecond = secondCurve.getPoint(orbIdx / 250); + + if (orbPosFirst.x < -1.4) { + switch (currentCurve) { + case "first": + setOrbDirection("right"); + break; + case "second": + setOrbDirection("left"); + break; + } + orbDirectionChangeCount++; + } + + if (orbPosFirst.x > 1.4) { + switch (currentCurve) { + case "first": + setOrbDirection("left"); + break; + case "second": + setOrbDirection("right"); + break; + } + orbDirectionChangeCount++; + } + + if (orbDirection === "left") { + switch (currentCurve) { + case "first": + orbIdx++; + break; + case "second": + orbIdx--; + break; + } + } else { + switch (currentCurve) { + case "first": + orbIdx--; + break; + case "second": + orbIdx++; + break; + } + } + + if (orbDirectionChangeCount % 6 === 0 && orbDirectionChangeCount !== 0) { + orbDirectionChangeCount = 0; + switch (currentCurve) { + case "first": + orbIdx = 250; + setCurrentCurve("second"); + break; + case "second": + orbIdx = 0; + setCurrentCurve("first"); + break; + } + setOrbDirection("left"); + } + + if (currentCurve === "first") { + (orbRef.current as any).position.x = orbPosFirst.x; + (orbRef.current as any).position.y = orbPosFirst.y; + } else { + (orbRef.current as any).position.x = orbPosSecond.x; + (orbRef.current as any).position.y = orbPosSecond.y; } - (orbRef.current as any).position.x = orbPos.x; - (orbRef.current as any).position.y = orbPos.y; }); + return ( - + - {/* - - - */} ); }); diff --git a/src/components/Starfield.tsx b/src/components/Starfield.tsx index 2bf2d7b..710f098 100644 --- a/src/components/Starfield.tsx +++ b/src/components/Starfield.tsx @@ -1,4 +1,4 @@ -import { a, Interpolation } from "@react-spring/three"; +import { Interpolation } from "@react-spring/three"; import React, { createRef, memo, useMemo, useRef } from "react"; import { useFrame } from "react-three-fiber"; import * as THREE from "three"; @@ -157,7 +157,7 @@ const Starfield = memo((props: StarfieldProps) => { }); return ( - + {posesBlueFromRight.map((pos: any, idx: number) => { return ( { ); })} - + ); });