-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🎸 Add splash screen, easy menu and game starting
✅ Closes: #13
- Loading branch information
1 parent
e13e687
commit 1efee2e
Showing
26 changed files
with
343 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
MODE=development |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
MODE=live |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
import { createRoot } from 'react-dom/client'; | ||
import Main from './components/Main'; | ||
|
||
const root = createRoot(document.body); | ||
root.render(<Main />); | ||
const el = document.getElementById('root'); | ||
|
||
if (el) { | ||
const root = createRoot(el); | ||
root.render(<Main />); | ||
} else { | ||
const root = createRoot(document.body); | ||
root.render(<Main />); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,99 @@ | ||
import React from "react"; | ||
import React, { useState, useEffect, useCallback } from "react"; | ||
import Menu from "./ui/Menu"; | ||
import Game from "./game/Game"; | ||
import splash from "../images/ui/cover.jpg"; | ||
|
||
function getWindowDimensions() { | ||
const { innerWidth: width, innerHeight: height } = window; | ||
return { | ||
width, | ||
height | ||
}; | ||
} | ||
|
||
let timeoutId: NodeJS.Timeout = null; | ||
|
||
function Main() { | ||
return ( | ||
<> | ||
<h2>Hello from React!</h2> | ||
</> | ||
); | ||
const [splashScreenOpacity, setSplashScreenOpacity] = useState(0.5); | ||
const [lastUpdate, setLastUpdate] = useState(new Date().getTime()); | ||
const [windowDimensions, setWindowDimensions] = useState({ | ||
height: window.innerHeight, | ||
width: window.innerWidth, | ||
}); | ||
const [screen, setScreen] = useState('menu'); | ||
const [gameState, setGameState] = useState({ | ||
location: 'town', | ||
backgroundAlt: 'Town background', | ||
playerPosition: { | ||
x: 0.5, | ||
y: 0, | ||
direction: 'down', | ||
currentAnimation: 'idle' | ||
} | ||
}); | ||
const [language, setLanguage] = useState('en'); | ||
|
||
const loadGame = useCallback(() => { | ||
|
||
}, []); | ||
|
||
const setSplashInterval = useCallback(() => { | ||
setInterval(() => { | ||
const now = new Date().getTime(); | ||
const delta = now - lastUpdate; | ||
const deltaModifier = delta / 100 | ||
setLastUpdate(now); | ||
setSplashScreenOpacity(splashScreenOpacity + (0.02 * deltaModifier)); | ||
}, 20) | ||
}, []) | ||
|
||
useEffect(() => { | ||
setSplashInterval(); | ||
|
||
function handleResize() { | ||
if (timeoutId) { | ||
clearTimeout(timeoutId); | ||
timeoutId = null; | ||
} | ||
|
||
const id = setTimeout(() => { | ||
setWindowDimensions(getWindowDimensions()); | ||
}, 100); | ||
|
||
timeoutId = id; | ||
} | ||
|
||
window.addEventListener('resize', handleResize); | ||
return () => window.removeEventListener('resize', handleResize); | ||
}); | ||
|
||
useEffect(() => { | ||
if (splashScreenOpacity >= 1) { | ||
const interval_id = setInterval(function(){}, Number.MAX_SAFE_INTEGER); | ||
|
||
for (let i = 1; i < Number(interval_id); i += 1) { | ||
clearInterval(i); | ||
} | ||
} | ||
}, [splashScreenOpacity]) | ||
|
||
if (splashScreenOpacity < 1) { | ||
return ( | ||
<img | ||
src={splash} | ||
alt="Background splash screen" | ||
width={windowDimensions.width} | ||
height={windowDimensions.height} | ||
style={{ opacity: splashScreenOpacity, transition: 'linear' }} | ||
/> | ||
) | ||
} | ||
|
||
if (screen === 'game') { | ||
return <Game savedGame={gameState} setGameState={setGameState} windowDimensions={windowDimensions} />; | ||
} | ||
|
||
return <Menu loadGame={loadGame} setScreen={setScreen} language={language} setLanguage={setLanguage} />; | ||
} | ||
|
||
export default Main; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import React from "react"; | ||
import { MenuButtonProps } from "../../types/menu"; | ||
|
||
function MenuButton({ clickHandler, text, classes = '' }: MenuButtonProps) { | ||
|
||
return <button className={classes} onClick={clickHandler}>{text}</button> | ||
} | ||
|
||
export default MenuButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import React from "react"; | ||
import { TextProps } from "../../types/menu"; | ||
|
||
function Subtitle({ text, classes = '' }: TextProps) { | ||
return <h4 className={classes}>{text}</h4> | ||
} | ||
|
||
export default Subtitle; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import React from "react"; | ||
import { TextProps } from "../../types/menu"; | ||
|
||
function Text({ text, classes = '' }: TextProps) { | ||
return <p className={classes}>{text}</p> | ||
} | ||
|
||
export default Text; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import React from "react"; | ||
import { TextProps } from "../../types/menu"; | ||
|
||
function Title({ text, classes = '' }: TextProps) { | ||
return <h2 className={classes}>{text}</h2> | ||
} | ||
|
||
export default Title; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import React from "react"; | ||
import { BackgroundProps } from "../../types/game"; | ||
|
||
const ratio = 12.7675752773; | ||
|
||
function Background({ image, savedGame, windowDimensions }: BackgroundProps) { | ||
const canvasWidth = ratio * windowDimensions.height; | ||
|
||
return <img | ||
src={image} | ||
alt={savedGame.backgroundAlt} | ||
height={windowDimensions.height} | ||
style={{ | ||
position: 'absolute', | ||
transition: 'linear', | ||
left: -(canvasWidth * savedGame.playerPosition.x), | ||
}} | ||
/> | ||
} | ||
|
||
export default Background; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import React from "react"; | ||
import Player from "./Player"; | ||
import Background from "./Background"; | ||
import { GameProps } from "../../types/game"; | ||
import map from "../../images/map/map.png"; | ||
import town from "../../images/map/town.png"; | ||
|
||
function Game({ savedGame, setGameState, windowDimensions }: GameProps) { | ||
return ( | ||
<> | ||
{ | ||
savedGame.location === "town" ? | ||
<Background image={town} savedGame={savedGame} windowDimensions={windowDimensions} /> : | ||
<Background image={map} savedGame={savedGame} windowDimensions={windowDimensions} /> | ||
} | ||
|
||
<Player /> | ||
</> | ||
); | ||
} | ||
|
||
export default Game; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import React from "react"; | ||
import idleDown from "../../animations/player/down/idle.png"; | ||
|
||
function Player() { | ||
|
||
return <img | ||
src={idleDown} | ||
alt={"Player"} | ||
style={{ | ||
position: 'absolute', | ||
transition: 'linear', | ||
top: '80%', | ||
left: '50%', | ||
transform: 'translate(-50%, -50%)', | ||
}} | ||
/> | ||
} | ||
|
||
export default Player; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import React from "react"; | ||
import { LanguageProps } from "../../types/menu"; | ||
|
||
function Language({ language, setLanguage }: LanguageProps) { | ||
|
||
return ( | ||
<> | ||
<label htmlFor="language">Language</label> | ||
<select name="language"> | ||
<option></option> | ||
</select> | ||
</> | ||
) | ||
} | ||
|
||
export default Language; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React, { useCallback } from "react"; | ||
import Language from "./Language"; | ||
import MenuButton from "../common/MenuButton"; | ||
import Title from "../common/Title"; | ||
import { MenuProps } from "../../types/menu"; | ||
|
||
function Menu({ loadGame, setScreen, language, setLanguage }: MenuProps) { | ||
const quitGame = useCallback(() => { | ||
window.close(); | ||
}, []); | ||
|
||
const startNewGame = useCallback(() => { | ||
setScreen('game') | ||
}, []) | ||
|
||
return ( | ||
<div> | ||
<Title classes="center white" text="Main Menu" /> | ||
<Language language={language} setLanguage={setLanguage} /> | ||
<MenuButton clickHandler={startNewGame} text={'Start game'} /> | ||
<MenuButton clickHandler={quitGame} text={'Quit game'} /> | ||
</div> | ||
) | ||
} | ||
|
||
export default Menu; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.