Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Adventure game
  • Loading branch information
nlz18001 committed Feb 12, 2024
1 parent 3c5c3ed commit fb53890
Show file tree
Hide file tree
Showing 3 changed files with 411 additions and 0 deletions.
82 changes: 82 additions & 0 deletions week-04/index.html
@@ -0,0 +1,82 @@
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Week 04</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css">

</head>
<body>



<div class="container mt-5">
<div class="row">
<main class="col-sm-10 offset-sm-1 col-md-8 offset-md-2 col-lg-6 offset-lg-3 col-xl-4 offset-xl-4 ">
<h1>Build a Life</h1>
<p>Where do you want to live? How do you see yourself settling down with your spouse? Let's start building your life!</p>
</main>
</div>

<div class="row">
<main class="col-sm-10 offset-sm-1 col-md-8 offset-md-2 col-lg-6 offset-lg-3 col-xl-4 offset-xl-4 ">
<!-- Output goes here -->

</main>
</div>
</div>

<!-- JS Templates -->
<template id="confirm">
<div class="card mb-4">
<h5 class="card-header">Confirm</h5>
<div class="card-body">
<h5 class="card-title">Question</h5>
<p class="card-text">With supporting text below as a natural lead-in to additional content.</p>
<button class="btn btn-primary btn-true">OK</button>
<button class="btn btn-secondary btn-false">Cancel</button>
</div>
</div>
</template>

<template id="prompt">
<div class="card mb-4">
<h5 class="card-header">Prompt</h5>
<div class="card-body">
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">
<h5 class="card-title">What is Your Name?</h5>
</label>
<input type="text" class="form-control" id="exampleFormControlInput1" placeholder="Type your response...">
</div>
<button class="btn btn-primary btn-true">OK</button>
<button class="btn btn-secondary btn-false">Cancel</button>
</div>
</div>
</template>

<template id="alert">
<div class="card mb-4">
<h5 class="card-header">Alert</h5>
<div class="card-body">
<h5 class="card-title">Title</h5>
<p class="card-text">text</p>
<button class="btn btn-primary btn-true">OK</button>
<button class="btn btn-secondary btn-false">Cancel</button>
</div>
</div>
</template>
<!-- Don't touch! We need this script -->
<script type="module" src="../.core/js/main.js"></script>


<!-- Game framework -->
<script src="js/game-framework.js" type="module"></script>


<!-- Open the following file in an editor to complete your assignment. -->
<script src="js/main.js" type="module"></script>


</body>
</html>
108 changes: 108 additions & 0 deletions week-04/js/game-framework.js
@@ -0,0 +1,108 @@
/**
* Game Framework
* It's safe to ignore this file. It's just a helper for the game.
*
*/

// Get the templates
let confirmTmpl = document.querySelector('#confirm').content;
let promptTmpl = document.querySelector('#prompt').content;
let alertTmpl = document.querySelector('#alert').content;



function Confirm(question = '', helpText='', appendTo = 'main') {

if (typeof question == 'string' && question !== '') {

return new Promise((resolve, reject) => {

// documentFragment from string
let df = confirmTmpl.cloneNode(true);

df.querySelector('.card-title').textContent = question;
df.querySelector('.card-text').textContent = helpText;


df.querySelector('.btn-true').addEventListener('click', (event) => {
console.log('Promise Resolved', true)
resolve(true)
}, {once: true})

df.querySelector('.btn-false').addEventListener('click', (event) => {
console.log('Promise Resolved', false)
resolve(false)
}, { once: true })

document.querySelector(appendTo).appendChild(df)
});
}
}


function Alert(question = '', helpText = '', appendTo = 'main') {

if (typeof question == 'string' && question !== '') {

return new Promise((resolve, reject) => {

// documentFragment from string
let df = alertTmpl.cloneNode(true);

df.querySelector('.card-title').textContent = question;
df.querySelector('.card-text').textContent = helpText;

df.querySelector('.btn-true').addEventListener('click', (event) => {
console.log('Promise Resolved', true)
resolve(true)
}, { once: true })

df.querySelector('.btn-false').addEventListener('click', (event) => {
console.log('Promise Resolved', false)
resolve(false)
}, { once: true })

document.querySelector(appendTo).appendChild(df)
});
}
}


function Prompt(question = '', helpText = '', appendTo = 'main') {

if (typeof question == 'string' && question !== '') {

return new Promise((resolve, reject) => {

// documentFragment from string
let df = promptTmpl.cloneNode(true);

df.querySelector('.card-title').textContent = question;
// df.querySelector('.card-text').textContent = helpText;
let input = df.querySelector('input');

df.querySelector('.btn-true').addEventListener('click', (event) => {
console.log('Promise Resolved', input.value)
resolve(input.value)
}, { once: true })

df.querySelector('.btn-false').addEventListener('click', (event) => {
console.log('Promise Resolved', false)
resolve(false)
}, { once: true })

document.querySelector(appendTo).appendChild(df)
});
}
}


// Autoscroll to the bottom of the page
const resizeObserver = new ResizeObserver(entries =>
window.scrollTo(0, document.body.scrollHeight)
)

// start observing a DOM node
resizeObserver.observe(document.body)

export { Confirm, Alert, Prompt }

0 comments on commit fb53890

Please sign in to comment.