Initial commit
This commit is contained in:
@@ -0,0 +1,460 @@
|
||||
/* AI Interactive Fiction - Web UI Styles */
|
||||
|
||||
/* Variables */
|
||||
:root {
|
||||
--text-color: #222;
|
||||
--background-color: #f8f4e8;
|
||||
--book-shadow: rgba(0, 0, 0, 0.3);
|
||||
--highlight-color: #783422;
|
||||
--control-color: #555;
|
||||
--light-color: rgba(255, 240, 210, 0.6);
|
||||
--viewport-aspect-ratio: 1.6;
|
||||
--book-width: 1000px;
|
||||
--book-height: 620px;
|
||||
--input-bg: rgba(255, 255, 255, 0.6);
|
||||
--img-aspect-ratio: 1.613;
|
||||
--aspect-ratio: min(var(--viewport-aspect-ratio), var(--img-aspect-ratio));
|
||||
font-size: calc(var(--book-height)/(34 * 1.5));
|
||||
}
|
||||
|
||||
/* Font faces */
|
||||
@font-face {
|
||||
font-family: "EB Garamond";
|
||||
src: url("../fonts/EBGaramond12-Regular.otf") format("opentype");
|
||||
font-weight: normal;
|
||||
font-style: normal;
|
||||
}
|
||||
|
||||
@font-face {
|
||||
font-family: "EB Garamond";
|
||||
src: url("../fonts/EBGaramond12-Italic.otf") format("opentype");
|
||||
font-weight: normal;
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
/* Global styles */
|
||||
* {
|
||||
box-sizing: border-box;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
html, body {
|
||||
height: 100%;
|
||||
font-family: "EB Garamond", serif;
|
||||
color: var(--text-color);
|
||||
background-color: #222;
|
||||
background-image: url(../images/brown-wooden-flooring.jpg);
|
||||
background-size: cover;
|
||||
background-position: center;
|
||||
overflow: hidden;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
min-height: 100vh;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
body.switched {
|
||||
transition: color 0.6s, background-color 0.6s;
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-size: 2rem;
|
||||
margin-bottom: 0.8rem;
|
||||
text-align: center;
|
||||
text-transform: uppercase;
|
||||
font-weight: normal;
|
||||
}
|
||||
|
||||
h2 {
|
||||
font-size: 1.2rem;
|
||||
text-align: center;
|
||||
font-weight: normal;
|
||||
}
|
||||
|
||||
h3 {
|
||||
font-size: 1.2rem;
|
||||
text-align: center;
|
||||
font-weight: normal;
|
||||
}
|
||||
|
||||
.header {
|
||||
padding-top: 3rem;
|
||||
padding-bottom: 3rem;
|
||||
}
|
||||
|
||||
.byline {
|
||||
font-feature-settings: "smcp";
|
||||
}
|
||||
|
||||
.separator {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
p, #ruler, #indent {
|
||||
font-size: 1.2rem;
|
||||
line-height: 1.2;
|
||||
color: rgba(0,0,0,0.9);
|
||||
margin-block-end: 0;
|
||||
margin-block-start: 0;
|
||||
}
|
||||
|
||||
a {
|
||||
transition: color 0.6s;
|
||||
color: #333;
|
||||
font-style: italic;
|
||||
text-decoration-thickness: 1px;
|
||||
}
|
||||
|
||||
a:hover {
|
||||
color: black;
|
||||
transition: color 0.1s;
|
||||
}
|
||||
|
||||
strong {
|
||||
color: black;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.container .hide {
|
||||
opacity: 0.0;
|
||||
}
|
||||
|
||||
.container .invisible {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.container > *, .container > p > * {
|
||||
opacity: 1.0;
|
||||
transition: opacity 0.5s;
|
||||
}
|
||||
|
||||
#command_input {
|
||||
position: absolute;
|
||||
bottom: 1rem;
|
||||
left: 3rem;
|
||||
right: 3rem;
|
||||
display: flex;
|
||||
}
|
||||
|
||||
#player_input {
|
||||
flex-grow: 1;
|
||||
font-family: inherit;
|
||||
font-size: 1.2rem;
|
||||
padding: 0.5rem;
|
||||
border: 1px solid rgba(0,0,0,0.3);
|
||||
border-radius: 0.25rem;
|
||||
background: rgba(255,255,255,0.9);
|
||||
}
|
||||
|
||||
#submit_command {
|
||||
margin-left: 0.5rem;
|
||||
font-family: inherit;
|
||||
font-size: 1.2rem;
|
||||
padding: 0.5rem 1rem;
|
||||
border: 1px solid rgba(0,0,0,0.3);
|
||||
border-radius: 0.25rem;
|
||||
background: rgba(255,255,255,0.9);
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
#submit_command:hover {
|
||||
background: rgba(255,255,255,1);
|
||||
}
|
||||
|
||||
#controls {
|
||||
z-index: 4;
|
||||
text-align: center;
|
||||
position: absolute;
|
||||
right: 0;
|
||||
left: 0;
|
||||
top: 1rem;
|
||||
padding-top: 1rem;
|
||||
user-select: none;
|
||||
transition: color 0.6s, background 0.6s;
|
||||
}
|
||||
|
||||
#controls [disabled] {
|
||||
color: #999;
|
||||
}
|
||||
|
||||
#controls input[type=range] {
|
||||
vertical-align: middle;
|
||||
-webkit-appearance: none;
|
||||
appearance: none;
|
||||
width: 5rem;
|
||||
cursor: pointer;
|
||||
outline: none;
|
||||
height: 0.5rem;
|
||||
background-color: transparent;
|
||||
box-sizing: border-box;
|
||||
border: 1px solid black;
|
||||
border-radius: 0.25rem;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
#controls input::-webkit-slider-thumb {
|
||||
-webkit-appearance: none;
|
||||
appearance: none;
|
||||
height: 0.5rem;
|
||||
width: 0.5rem;
|
||||
border-radius: 0.25rem;
|
||||
background-color: rgba(0,0,0,0.9);
|
||||
border: none;
|
||||
box-shadow: -407px 0 0 400px rgba(0,0,0,0.3);
|
||||
}
|
||||
|
||||
#controls input::-webkit-runnable-track {
|
||||
-webkit-appearance: none;
|
||||
appearance: none;
|
||||
height: 0.5rem;
|
||||
border-radius: 0.25rem;
|
||||
}
|
||||
|
||||
#controls>a:not(:last-of-type):after, #controls>span::after {
|
||||
content: " | ";
|
||||
}
|
||||
|
||||
#book {
|
||||
position: relative;
|
||||
width: var(--book-width);
|
||||
height: var(--book-height);
|
||||
background-image: url('../images/book-3057904.png');
|
||||
background-size: contain;
|
||||
background-position: center;
|
||||
background-repeat: no-repeat;
|
||||
perspective: 500px;
|
||||
perspective-origin: 50% 50%;
|
||||
max-width: 90vw;
|
||||
max-height: 90vh;
|
||||
margin: 0 auto;
|
||||
transform-origin: center center;
|
||||
}
|
||||
|
||||
#page_left, #page_right {
|
||||
position: absolute;
|
||||
top: 5%;
|
||||
bottom: 10%;
|
||||
width: 39%;
|
||||
box-sizing: border-box;
|
||||
padding: 0 3rem 1rem 1rem;
|
||||
overflow: visible;
|
||||
overflow-y: scroll;
|
||||
opacity: 0.95;
|
||||
mix-blend-mode: darken;
|
||||
}
|
||||
|
||||
#story {
|
||||
overflow-x: visible;
|
||||
margin-bottom: 3rem;
|
||||
}
|
||||
|
||||
#page_left {
|
||||
left: 11.5%;
|
||||
}
|
||||
|
||||
#page_right {
|
||||
right: 7%;
|
||||
height: calc(28 * 1.2 * 1.2rem);
|
||||
padding-bottom: 4rem;
|
||||
}
|
||||
|
||||
.user-input {
|
||||
font-style: italic;
|
||||
color: #555;
|
||||
margin-top: 1rem;
|
||||
}
|
||||
|
||||
.narrative {
|
||||
margin-top: 1rem;
|
||||
}
|
||||
|
||||
/* ===== Scrollbar CSS ===== */
|
||||
/* Firefox */
|
||||
* {
|
||||
scrollbar-width: auto;
|
||||
scrollbar-color: #000000 rgba(255,255,255,0);
|
||||
}
|
||||
|
||||
/* Chrome, Edge, and Safari */
|
||||
*::-webkit-scrollbar {
|
||||
width: calc(1rem/4);
|
||||
}
|
||||
|
||||
*::-webkit-scrollbar-track {
|
||||
background: rgba(255,255,255,0.0);
|
||||
}
|
||||
|
||||
*::-webkit-scrollbar-thumb {
|
||||
background-color: #000000;
|
||||
border-radius: calc(1rem/4/2);
|
||||
border: none;
|
||||
}
|
||||
|
||||
.fade-in {
|
||||
animation: fadeIn ease 1s;
|
||||
-webkit-animation: fadeIn ease 1s;
|
||||
-moz-animation: fadeIn ease 1s;
|
||||
-o-animation: fadeIn ease 1s;
|
||||
-ms-animation: fadeIn ease 1s;
|
||||
}
|
||||
|
||||
@keyframes fadeIn {
|
||||
0% {opacity:0;}
|
||||
100% {opacity:1;}
|
||||
}
|
||||
|
||||
@-moz-keyframes fadeIn {
|
||||
0% {opacity:0;}
|
||||
100% {opacity:1;}
|
||||
}
|
||||
|
||||
@-webkit-keyframes fadeIn {
|
||||
0% {opacity:0;}
|
||||
100% {opacity:1;}
|
||||
}
|
||||
|
||||
@-o-keyframes fadeIn {
|
||||
0% {opacity:0;}
|
||||
100% {opacity:1;}
|
||||
}
|
||||
|
||||
@-ms-keyframes fadeIn {
|
||||
0% {opacity:0;}
|
||||
100% {opacity:1;}
|
||||
}
|
||||
|
||||
#ruler, #indent {
|
||||
visibility: hidden;
|
||||
position: absolute;
|
||||
top: -8000px;
|
||||
width: auto;
|
||||
display: inline;
|
||||
left: -8000px;
|
||||
text-indent: 0;
|
||||
text-align: left;
|
||||
hyphens: none;
|
||||
margin-block-end: 0;
|
||||
}
|
||||
|
||||
#lighting {
|
||||
position: absolute;
|
||||
top: -35%;
|
||||
left: -35%;
|
||||
width: 180%;
|
||||
height: 180%;
|
||||
animation: gradient-animation-shrink 1s 1;
|
||||
background: radial-gradient(circle, rgba(255,240,182,0.1) 0%, rgba(255,237,165,0.2) 20%, rgba(0,0,0,0.9) 65%, rgba(0,0,0,0.9) 100%);
|
||||
mix-blend-mode: color-burn;
|
||||
pointer-events: none;
|
||||
z-index: 999;
|
||||
}
|
||||
|
||||
@keyframes gradient-animation-grow {
|
||||
0% { width: 180%; height: 180%; left: -35%; top: -35%; }
|
||||
100% { width: 170%; height: 170%; left: -33%; top: -33%; }
|
||||
}
|
||||
|
||||
@keyframes gradient-animation-shrink {
|
||||
0% { width: 170%; height: 170%; left: -33%; top: -33%; }
|
||||
100% { width: 180%; height: 180%; left: -35%; top: -35%; }
|
||||
}
|
||||
|
||||
.loading-indicator {
|
||||
display: inline-block;
|
||||
position: relative;
|
||||
width: 1.2rem;
|
||||
height: 1.2rem;
|
||||
margin-left: 0.5rem;
|
||||
}
|
||||
.loading-indicator div {
|
||||
box-sizing: border-box;
|
||||
display: block;
|
||||
position: absolute;
|
||||
width: 1rem;
|
||||
height: 1rem;
|
||||
border: 0.2rem solid #000;
|
||||
border-radius: 50%;
|
||||
animation: loading-indicator 1.2s cubic-bezier(0.5, 0, 0.5, 1) infinite;
|
||||
border-color: #000 transparent transparent transparent;
|
||||
}
|
||||
.loading-indicator div:nth-child(1) {
|
||||
animation-delay: -0.45s;
|
||||
}
|
||||
.loading-indicator div:nth-child(2) {
|
||||
animation-delay: -0.3s;
|
||||
}
|
||||
.loading-indicator div:nth-child(3) {
|
||||
animation-delay: -0.15s;
|
||||
}
|
||||
@keyframes loading-indicator {
|
||||
0% {
|
||||
transform: rotate(0deg);
|
||||
}
|
||||
100% {
|
||||
transform: rotate(360deg);
|
||||
}
|
||||
}
|
||||
|
||||
/* Media queries for responsive design */
|
||||
@media (max-width: 768px) {
|
||||
:root {
|
||||
font-size: calc(var(--book-height)/(40 * 1.5));
|
||||
}
|
||||
|
||||
#book {
|
||||
max-width: 95vw;
|
||||
max-height: 95vh;
|
||||
}
|
||||
|
||||
#page_left, #page_right {
|
||||
width: 38%;
|
||||
padding: 0 1rem 1rem 1rem;
|
||||
}
|
||||
}
|
||||
|
||||
/* Ensure responsive book sizing */
|
||||
@media (max-width: 1200px) {
|
||||
#book {
|
||||
transform: scale(0.95);
|
||||
transform-origin: center center;
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 992px) {
|
||||
#book {
|
||||
transform: scale(0.85);
|
||||
transform-origin: center center;
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
#book {
|
||||
transform: scale(0.75);
|
||||
transform-origin: center center;
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 576px) {
|
||||
#book {
|
||||
transform: scale(0.65);
|
||||
transform-origin: center center;
|
||||
}
|
||||
}
|
||||
|
||||
/* Additional responsive fix to ensure book remains centered */
|
||||
@media (max-height: 700px) {
|
||||
#book {
|
||||
transform: scale(0.8);
|
||||
transform-origin: center center;
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-height: 600px) {
|
||||
#book {
|
||||
transform: scale(0.7);
|
||||
transform-origin: center center;
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 2.0 MiB |
Binary file not shown.
|
After Width: | Height: | Size: 2.5 MiB |
@@ -0,0 +1,43 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>AI Interactive Fiction</title>
|
||||
<link rel="stylesheet" href="css/style.css">
|
||||
<script src="https://cdn.socket.io/4.6.0/socket.io.min.js"></script>
|
||||
<script src="js/smartypants.js"></script>
|
||||
<script src="js/tts-handler.js"></script>
|
||||
<script src="js/ai-fiction.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div id="book">
|
||||
<div id="lighting"></div>
|
||||
<div id="page_left">
|
||||
<div class="header">
|
||||
<h1>AI Interactive Fiction</h1>
|
||||
<h2 class="byline">A modern take on text adventures</h2>
|
||||
</div>
|
||||
<div class="separator">
|
||||
<double class="separator">⋆</double>
|
||||
</div>
|
||||
<div id="story"></div>
|
||||
</div>
|
||||
<div id="page_right">
|
||||
<div id="controls">
|
||||
<a href="#" id="rewind">Restart</a>
|
||||
<a href="#" id="speech">Speech</a>
|
||||
<span>Speed <input type="range" id="speed" min="0" max="100" value="50"> <a href="#" id="speed_reset">Reset</a></span>
|
||||
<a href="#" id="save">Save</a>
|
||||
<a href="#" id="reload">Load</a>
|
||||
</div>
|
||||
<div id="command_history"></div>
|
||||
<div id="command_input">
|
||||
<input type="text" id="player_input" placeholder="Enter your command..." autocomplete="off">
|
||||
<button id="submit_command">▶</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div id="ruler"></div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,440 @@
|
||||
/**
|
||||
* AI Interactive Fiction
|
||||
* Main client-side logic for web interface
|
||||
*/
|
||||
class AIFiction {
|
||||
constructor() {
|
||||
// DOM elements
|
||||
this.storyContainer = document.getElementById('story');
|
||||
this.commandHistoryContainer = document.getElementById('command_history');
|
||||
this.playerInput = document.getElementById('player_input');
|
||||
this.submitButton = document.getElementById('submit_command');
|
||||
this.speechButton = document.getElementById('speech');
|
||||
this.rewindButton = document.getElementById('rewind');
|
||||
this.saveButton = document.getElementById('save');
|
||||
this.loadButton = document.getElementById('reload');
|
||||
this.speedSlider = document.getElementById('speed');
|
||||
this.speedReset = document.getElementById('speed_reset');
|
||||
|
||||
// Game state
|
||||
this.gameState = {
|
||||
started: false,
|
||||
currentRoomId: '',
|
||||
textSpeed: 50
|
||||
};
|
||||
|
||||
// Socket connection - ensure we're connecting to the right URL
|
||||
this.socket = io(window.location.origin, {
|
||||
reconnectionAttempts: 5,
|
||||
timeout: 10000
|
||||
});
|
||||
|
||||
// Typing effect configuration
|
||||
this.typingSpeed = 30; // Default value, will be adjusted by slider
|
||||
this.typingTimeout = null;
|
||||
|
||||
// Bind event handlers
|
||||
this.bindEvents();
|
||||
|
||||
// Initialize socket communication
|
||||
this.initializeSocket();
|
||||
|
||||
// Initialize UI
|
||||
this.initializeUI();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the UI
|
||||
*/
|
||||
initializeUI() {
|
||||
this.updateTypingSpeed();
|
||||
this.updateSpeechButton();
|
||||
|
||||
// Disable buttons initially
|
||||
this.rewindButton.setAttribute('disabled', 'disabled');
|
||||
this.loadButton.setAttribute('disabled', 'disabled');
|
||||
|
||||
// Start the game
|
||||
this.startGame();
|
||||
}
|
||||
|
||||
/**
|
||||
* Bind event handlers to DOM elements
|
||||
*/
|
||||
bindEvents() {
|
||||
// Submit command on button click
|
||||
this.submitButton.addEventListener('click', () => this.submitCommand());
|
||||
|
||||
// Submit command on Enter key
|
||||
this.playerInput.addEventListener('keydown', (e) => {
|
||||
if (e.key === 'Enter') {
|
||||
this.submitCommand();
|
||||
}
|
||||
});
|
||||
|
||||
// Toggle speech
|
||||
this.speechButton.addEventListener('click', () => {
|
||||
if (tts.isReady()) {
|
||||
const enabled = tts.toggle();
|
||||
this.updateSpeechButton(enabled);
|
||||
|
||||
if (enabled) {
|
||||
// Speak the last narrative if speech was just enabled
|
||||
const lastNarrative = this.storyContainer.lastElementChild;
|
||||
if (lastNarrative && lastNarrative.classList.contains('narrative')) {
|
||||
tts.speak(lastNarrative.textContent);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
console.log('TTS not ready yet');
|
||||
}
|
||||
});
|
||||
|
||||
// Restart game
|
||||
this.rewindButton.addEventListener('click', () => {
|
||||
if (confirm('Are you sure you want to restart the game? All progress will be lost.')) {
|
||||
this.startGame();
|
||||
}
|
||||
});
|
||||
|
||||
// Save game
|
||||
this.saveButton.addEventListener('click', () => {
|
||||
this.socket.emit('saveGame');
|
||||
});
|
||||
|
||||
// Load game
|
||||
this.loadButton.addEventListener('click', () => {
|
||||
this.socket.emit('loadGame');
|
||||
});
|
||||
|
||||
// Adjust typing speed
|
||||
this.speedSlider.addEventListener('input', () => {
|
||||
this.updateTypingSpeed();
|
||||
});
|
||||
|
||||
// Reset speed to default
|
||||
this.speedReset.addEventListener('click', () => {
|
||||
this.speedSlider.value = 50;
|
||||
this.updateTypingSpeed();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize socket event handlers
|
||||
*/
|
||||
initializeSocket() {
|
||||
// Connection established
|
||||
this.socket.on('connect', () => {
|
||||
console.log('Connected to server');
|
||||
// Automatically start the game once connected
|
||||
if (!this.gameState.started) {
|
||||
this.startGame();
|
||||
}
|
||||
});
|
||||
|
||||
// Connection error
|
||||
this.socket.on('connect_error', (error) => {
|
||||
console.error('Connection error:', error);
|
||||
this.addSystemMessage('Connection error. Please check your network connection and try again.');
|
||||
});
|
||||
|
||||
// Game introduction received
|
||||
this.socket.on('gameIntroduction', (data) => {
|
||||
this.clearStory();
|
||||
this.addNarrative(data.introduction);
|
||||
this.addNarrative(data.initialRoomDescription);
|
||||
|
||||
this.gameState.started = true;
|
||||
this.gameState.currentRoomId = data.currentRoomId;
|
||||
|
||||
// Enable buttons
|
||||
this.rewindButton.removeAttribute('disabled');
|
||||
|
||||
// Focus on input field
|
||||
this.playerInput.focus();
|
||||
});
|
||||
|
||||
// Narrative response received
|
||||
this.socket.on('narrativeResponse', (data) => {
|
||||
this.addNarrative(data.text);
|
||||
|
||||
if (data.suggestions && data.suggestions.length > 0) {
|
||||
this.addSuggestions(data.suggestions);
|
||||
}
|
||||
|
||||
// Update game state
|
||||
if (data.gameState) {
|
||||
this.gameState.currentRoomId = data.gameState.currentRoomId;
|
||||
}
|
||||
|
||||
// Scroll to bottom and focus input
|
||||
this.scrollToBottom();
|
||||
this.playerInput.focus();
|
||||
|
||||
// Re-enable input (failsafe)
|
||||
this.playerInput.disabled = false;
|
||||
this.submitButton.disabled = false;
|
||||
});
|
||||
|
||||
// Game saved confirmation
|
||||
this.socket.on('gameSaved', () => {
|
||||
this.addSystemMessage('Game saved successfully.');
|
||||
// Enable load button
|
||||
this.loadButton.removeAttribute('disabled');
|
||||
});
|
||||
|
||||
// Game loaded confirmation
|
||||
this.socket.on('gameLoaded', (data) => {
|
||||
this.clearStory();
|
||||
this.addSystemMessage('Game loaded successfully.');
|
||||
this.addNarrative(data.currentRoomDescription);
|
||||
|
||||
// Update game state
|
||||
this.gameState.currentRoomId = data.currentRoomId;
|
||||
});
|
||||
|
||||
// Error messages
|
||||
this.socket.on('error', (data) => {
|
||||
this.addSystemMessage(`Error: ${data.message}`);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Start a new game
|
||||
*/
|
||||
startGame() {
|
||||
this.clearStory();
|
||||
this.addSystemMessage('Starting a new game...');
|
||||
this.socket.emit('startGame');
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit a player command
|
||||
*/
|
||||
submitCommand() {
|
||||
const command = this.playerInput.value.trim();
|
||||
|
||||
if (command === '') return;
|
||||
|
||||
// Disable input temporarily
|
||||
this.playerInput.disabled = true;
|
||||
this.submitButton.disabled = true;
|
||||
|
||||
// Add command to history
|
||||
this.addUserCommand(command);
|
||||
|
||||
// Add a temporary "thinking" message
|
||||
const thinkingId = this.addThinking();
|
||||
|
||||
// Send command to server
|
||||
this.socket.emit('playerCommand', { command });
|
||||
|
||||
// Clear input
|
||||
this.playerInput.value = '';
|
||||
|
||||
// Re-enable input field after a short delay (or after 8 seconds as failsafe)
|
||||
const timeout = setTimeout(() => {
|
||||
this.playerInput.disabled = false;
|
||||
this.submitButton.disabled = false;
|
||||
|
||||
// Remove thinking indicator
|
||||
const thinkingElement = document.getElementById(thinkingId);
|
||||
if (thinkingElement) {
|
||||
thinkingElement.remove();
|
||||
}
|
||||
|
||||
// Add system message if no response was received (likely timeout)
|
||||
if (document.getElementById(thinkingId)) {
|
||||
this.addSystemMessage('The server is taking too long to respond. Please try again.');
|
||||
}
|
||||
}, 8000);
|
||||
|
||||
// Store the timeout so it can be cleared if we get a response
|
||||
this.currentCommandTimeout = timeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a user command to the story
|
||||
*/
|
||||
addUserCommand(command) {
|
||||
const element = document.createElement('p');
|
||||
element.className = 'user-input';
|
||||
element.textContent = `> ${command}`;
|
||||
this.storyContainer.appendChild(element);
|
||||
this.scrollToBottom();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a narrative response with typing effect
|
||||
*/
|
||||
addNarrative(text) {
|
||||
const element = document.createElement('p');
|
||||
element.className = 'narrative hide';
|
||||
this.storyContainer.appendChild(element);
|
||||
|
||||
// Apply SmartyPants transformations for better typography
|
||||
const processedText = SmartyPants.process(text);
|
||||
|
||||
// Clear any existing typing timeouts
|
||||
if (this.typingTimeout) {
|
||||
clearTimeout(this.typingTimeout);
|
||||
}
|
||||
|
||||
// Add the text with a typing effect
|
||||
this.typeText(element, processedText, 0);
|
||||
|
||||
// Read text aloud if speech is enabled
|
||||
if (tts && tts.enabled) {
|
||||
tts.speak(text);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add suggestions to the story
|
||||
*/
|
||||
addSuggestions(suggestions) {
|
||||
const element = document.createElement('div');
|
||||
element.className = 'suggestions';
|
||||
|
||||
const heading = document.createElement('p');
|
||||
heading.textContent = 'Suggestions:';
|
||||
heading.style.fontStyle = 'italic';
|
||||
heading.style.marginTop = '1rem';
|
||||
element.appendChild(heading);
|
||||
|
||||
const list = document.createElement('ul');
|
||||
suggestions.forEach(suggestion => {
|
||||
const item = document.createElement('li');
|
||||
item.textContent = suggestion;
|
||||
|
||||
// Make suggestions clickable
|
||||
item.style.cursor = 'pointer';
|
||||
item.addEventListener('click', () => {
|
||||
this.playerInput.value = suggestion;
|
||||
this.submitCommand();
|
||||
});
|
||||
|
||||
list.appendChild(item);
|
||||
});
|
||||
element.appendChild(list);
|
||||
|
||||
this.storyContainer.appendChild(element);
|
||||
this.scrollToBottom();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a system message
|
||||
*/
|
||||
addSystemMessage(message) {
|
||||
const element = document.createElement('p');
|
||||
element.className = 'system-message';
|
||||
element.textContent = message;
|
||||
element.style.fontStyle = 'italic';
|
||||
element.style.color = '#555';
|
||||
this.storyContainer.appendChild(element);
|
||||
this.scrollToBottom();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a thinking indicator
|
||||
*/
|
||||
addThinking() {
|
||||
const id = 'thinking-' + Date.now();
|
||||
const element = document.createElement('div');
|
||||
element.id = id;
|
||||
element.className = 'thinking';
|
||||
element.innerHTML = '<p>Thinking<span class="loading-indicator"><div></div><div></div><div></div><div></div></span></p>';
|
||||
element.style.fontStyle = 'italic';
|
||||
element.style.color = '#777';
|
||||
this.storyContainer.appendChild(element);
|
||||
this.scrollToBottom();
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the story container
|
||||
*/
|
||||
clearStory() {
|
||||
while (this.storyContainer.firstChild) {
|
||||
this.storyContainer.removeChild(this.storyContainer.firstChild);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Type text into an element character by character
|
||||
*/
|
||||
typeText(element, text, index) {
|
||||
// Show the element if it was hidden
|
||||
if (index === 0) {
|
||||
element.classList.remove('hide');
|
||||
}
|
||||
|
||||
// Set the current text
|
||||
element.textContent = text.substring(0, index);
|
||||
|
||||
// If we haven't reached the end of the text
|
||||
if (index < text.length) {
|
||||
// Calculate delay (randomize slightly for more natural effect)
|
||||
const delay = Math.max(10, 100 - this.gameState.textSpeed) / 5;
|
||||
const randomDelay = delay * (0.8 + Math.random() * 0.4);
|
||||
|
||||
// Schedule the next character
|
||||
this.typingTimeout = setTimeout(() => {
|
||||
this.typeText(element, text, index + 1);
|
||||
}, randomDelay);
|
||||
} else {
|
||||
// Finished typing
|
||||
this.scrollToBottom();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the typing speed based on the slider value
|
||||
*/
|
||||
updateTypingSpeed() {
|
||||
this.gameState.textSpeed = parseInt(this.speedSlider.value, 10);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the speech button styling
|
||||
*/
|
||||
updateSpeechButton(enabled = false) {
|
||||
if (enabled) {
|
||||
this.speechButton.style.fontWeight = 'bold';
|
||||
this.speechButton.style.color = '#000';
|
||||
} else {
|
||||
this.speechButton.style.fontWeight = 'normal';
|
||||
this.speechButton.style.color = '#333';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Scroll the story container to the bottom
|
||||
*/
|
||||
scrollToBottom() {
|
||||
this.storyContainer.scrollTop = this.storyContainer.scrollHeight;
|
||||
}
|
||||
}
|
||||
|
||||
// Create the application when the DOM is fully loaded
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
// Set custom CSS variables based on viewport
|
||||
const updateViewportVariables = () => {
|
||||
const vw = window.innerWidth;
|
||||
const vh = window.innerHeight;
|
||||
document.documentElement.style.setProperty('--viewport-aspect-ratio', `${vw / vh}`);
|
||||
|
||||
// Adjust book size based on viewport
|
||||
const bookWidth = Math.min(vw * 0.9, vh * 1.4);
|
||||
const bookHeight = bookWidth / 1.613;
|
||||
document.documentElement.style.setProperty('--book-width', `${bookWidth}px`);
|
||||
document.documentElement.style.setProperty('--book-height', `${bookHeight}px`);
|
||||
};
|
||||
|
||||
// Update variables initially and on resize
|
||||
updateViewportVariables();
|
||||
window.addEventListener('resize', updateViewportVariables);
|
||||
|
||||
// Initialize the application
|
||||
window.app = new AIFiction();
|
||||
});
|
||||
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
* SmartyPants - Smart typography for web content
|
||||
* Converts straight quotes to curly quotes, dashes to em-dashes, etc.
|
||||
* Based on the original SmartyPants by John Gruber
|
||||
*/
|
||||
|
||||
const SmartyPants = (function() {
|
||||
// Regular expressions for matching
|
||||
const quotes = {
|
||||
double: {
|
||||
opening: /(\s|^)"(\w)/g,
|
||||
closing: /(\w)"/g,
|
||||
openingNested: /(\s|^)'(\w)/g,
|
||||
closingNested: /(\w)'/g
|
||||
},
|
||||
single: {
|
||||
opening: /(\s|^)'(\w)/g,
|
||||
closing: /(\w)'/g
|
||||
}
|
||||
};
|
||||
|
||||
const dashes = {
|
||||
emDash: /--/g,
|
||||
enDash: / - /g
|
||||
};
|
||||
|
||||
const ellipses = /\.\.\./g;
|
||||
|
||||
/**
|
||||
* Process text with SmartyPants transformations
|
||||
*/
|
||||
function process(text) {
|
||||
if (!text) return text;
|
||||
|
||||
let result = text;
|
||||
|
||||
// Transform double quotes
|
||||
result = result.replace(quotes.double.opening, '$1"$2');
|
||||
result = result.replace(quotes.double.closing, '$1"');
|
||||
|
||||
// Transform single quotes
|
||||
result = result.replace(quotes.single.opening, '$1\u2018$2');
|
||||
result = result.replace(quotes.single.closing, '$1\u2019');
|
||||
|
||||
// Transform apostrophes (same as closing single quotes)
|
||||
result = result.replace(/(\w)'(\w)/g, '$1\u2019$2');
|
||||
|
||||
// Transform dashes
|
||||
result = result.replace(dashes.emDash, '—');
|
||||
result = result.replace(dashes.enDash, ' – ');
|
||||
|
||||
// Transform ellipses
|
||||
result = result.replace(ellipses, '…');
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
return {
|
||||
process: process
|
||||
};
|
||||
})();
|
||||
|
||||
// Make available in browser and Node.js environments
|
||||
if (typeof module !== 'undefined' && module.exports) {
|
||||
module.exports = SmartyPants;
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
/**
|
||||
* Text-to-Speech Handler for AI Interactive Fiction
|
||||
* Uses Web Speech API for text-to-speech
|
||||
*/
|
||||
class TTSHandler {
|
||||
constructor() {
|
||||
this.enabled = false;
|
||||
this.speaking = false;
|
||||
this.queue = [];
|
||||
this.synthesis = window.speechSynthesis;
|
||||
this.utterance = null;
|
||||
|
||||
// Check if browser supports speech synthesis
|
||||
if (this.synthesis) {
|
||||
console.log('Speech synthesis is supported in this browser');
|
||||
this.browserSupport = true;
|
||||
} else {
|
||||
console.warn('Speech synthesis is not supported in this browser');
|
||||
this.browserSupport = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggle TTS on/off
|
||||
*/
|
||||
toggle() {
|
||||
this.enabled = !this.enabled;
|
||||
if (!this.enabled && this.speaking) {
|
||||
this.stop();
|
||||
}
|
||||
return this.enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Speak the given text
|
||||
*/
|
||||
speak(text) {
|
||||
if (!this.enabled || !this.browserSupport) return;
|
||||
|
||||
// Add to queue
|
||||
this.queue.push(text);
|
||||
|
||||
// If not already speaking, start processing queue
|
||||
if (!this.speaking) {
|
||||
this.processQueue();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the speech queue
|
||||
*/
|
||||
processQueue() {
|
||||
if (this.queue.length === 0 || this.speaking) return;
|
||||
|
||||
this.speaking = true;
|
||||
const text = this.queue.shift();
|
||||
|
||||
try {
|
||||
this.utterance = new SpeechSynthesisUtterance(text);
|
||||
|
||||
// Configure speech options
|
||||
this.utterance.rate = 1.0; // Speech rate (0.1 to 10)
|
||||
this.utterance.pitch = 1.0; // Speech pitch (0 to 2)
|
||||
|
||||
// When speech ends, process the next item
|
||||
this.utterance.onend = () => {
|
||||
this.speaking = false;
|
||||
this.processQueue();
|
||||
};
|
||||
|
||||
// If speech is interrupted or errors
|
||||
this.utterance.onerror = (event) => {
|
||||
console.error('TTS error:', event.error);
|
||||
this.speaking = false;
|
||||
this.processQueue();
|
||||
};
|
||||
|
||||
this.synthesis.speak(this.utterance);
|
||||
} catch (error) {
|
||||
console.error('TTS error:', error);
|
||||
this.speaking = false;
|
||||
this.processQueue();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop current speech
|
||||
*/
|
||||
stop() {
|
||||
if (this.synthesis && this.speaking) {
|
||||
this.synthesis.cancel();
|
||||
}
|
||||
this.queue = [];
|
||||
this.speaking = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if TTS is ready
|
||||
*/
|
||||
isReady() {
|
||||
return this.browserSupport;
|
||||
}
|
||||
}
|
||||
|
||||
// Create a global instance
|
||||
const tts = new TTSHandler();
|
||||
Reference in New Issue
Block a user