Skip to content

Commit

Permalink
Merge pull request #104 from alesan99/modules
Browse files Browse the repository at this point in the history
Javascript Modules Conversion
  • Loading branch information
alesan99 authored Sep 3, 2024
2 parents cea0e30 + 57be768 commit 1e694f1
Show file tree
Hide file tree
Showing 53 changed files with 709 additions and 116 deletions.
10 changes: 8 additions & 2 deletions website/assets/items/list.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@
"silver_chains",
"spike_choker"
],
"pet": [
"pillbug"
],
"head": [
"hoodie",
"snapback",
"top_hat"
],
"pet": [
"pillbug"
"face": [
"catears",
"sunglasses",
"toothpick",
"visors"
],
"item": [
"beer",
Expand Down
21 changes: 19 additions & 2 deletions website/game/area.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
// Area
// logic that controls the background and elements of an area

import {DRAW, SAVEDATA, PROFILE, WORLD, NETPLAY, CURSOR} from "./main.js"
import { PHYSICSWORLD, PLAYER, PLAYER_CONTROLLER, MINIGAME, OBJECTS, NPCS } from "./world.js"
import AudioSystem from "./engine/audio.js"
import { BACKGROUNDIMG, BACKGROUNDSPRITE, BACKGROUNDANIM } from "./assets.js"
import { canvasWidth, canvasHeight, RenderImage } from "./engine/render.js"
import { Sprite, DrawableSprite, Animation } from "./engine/sprite.js"
import { setState } from "./state.js"
import {addItem, removeNuggets} from "./savedata.js"
import QuestSystem from "./quests.js"
import DialogueSystem from "./dialogue.js"
import Transition from "./transition.js"
import { MUSIC } from "./assets.js"
import {PhysicsObject,Character,Player,NPC,Pet,Trigger,Wall,Warp,Furniture,Particle} from "./objects/objects.js"


// Load area data from .json
function loadAreaFile(data, world, fromWarp, endFunc) {
let area = world.area
Expand Down Expand Up @@ -176,7 +191,7 @@ function checkCondition(c) {
if (c.questComplete != null) {
// "questComplete" just checks if the quest has ever been completed
// Setting it to false will check if it has never been completed
questCompleted = SAVEDATA.quests.completed[questName]
let questCompleted = SAVEDATA.quests.completed[questName]
if (c.questComplete === true && questCompleted) { // Quest must be complete
return true
} else if (c.questComplete === false && !questCompleted) { // Quest must not be complete
Expand Down Expand Up @@ -260,4 +275,6 @@ function conditionsUpdate() {
trig.active = isActive
}
}
}
}

export { loadAreaFile, checkCondition, conditionsUpdate }
38 changes: 26 additions & 12 deletions website/game/assets.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
//Load images, audio, and other data

IMG = []
SPRITE = []
ANIM = []
FONT = []
MUSIC = []
SFX = []
import LoadingScreen from "./loading.js"
import AudioSystem from "./engine/audio.js"
import { RenderImage, RenderFont } from "./engine/render.js"
import { Sprite } from "./engine/sprite.js"

BACKGROUND = []
BACKGROUNDIMG = []
BACKGROUNDSPRITE = []
BACKGROUNDANIM = []
var IMG = []
var SPRITE = []
var ANIM = []
var FONT = []
var MUSIC = []
var SFX = []

DIRECTORYTREE = {}
var ITEMS
var HEADOFFSET
var FACEOFFSET
var BODYOFFSET
var ITEMOFFSET
var CHICKENROTATION

var BACKGROUND = []
var BACKGROUNDIMG = []
var BACKGROUNDSPRITE = []
var BACKGROUNDANIM = []

//DIRECTORYTREE = {}

function loadGameAssets() {
// Loading Screen
Expand Down Expand Up @@ -250,4 +262,6 @@ function loadJSON5(filePath, callBack, errorCallBack) {
// DIRECTORYTREE = data
// })
// .catch(error => { console.error(error) });
// console.log(DIRECTORYTREE)
// console.log(DIRECTORYTREE)

export { IMG, SPRITE, ANIM, FONT, MUSIC, SFX, loadGameAssets, loadJSON, loadJSON5, ITEMS, HEADOFFSET, FACEOFFSET, BODYOFFSET, ITEMOFFSET, CHICKENROTATION, BACKGROUND, BACKGROUNDIMG, BACKGROUNDSPRITE, BACKGROUNDANIM }
20 changes: 18 additions & 2 deletions website/game/coop.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
// TODO: Visit other player's coops
// TODO: Wallpapers

import {DRAW, SAVEDATA, PROFILE, WORLD, NETPLAY, CURSOR} from "./main.js"
import {IMG, SPRITE, ANIM, FONT, SFX, ITEMS} from "./assets.js"
import {HEXtoRGB, RGBtoHEX, removeNuggets, addNuggets, spendNuggets, addItem, removeItem, getItemCategory, getItemData, getItem, placeFurniture, removeFurniture} from "./savedata.js"
import {openMenu, closeMenu, getOpenMenu} from "./state.js"
import {OBJECTS, PLAYER, PLAYER_CONTROLLER, PHYSICSWORLD} from "./world.js"
import QuestSystem from "./quests.js"
import Transition from "./transition.js"
import AudioSystem from "./engine/audio.js"
import {checkCondition} from "./area.js"
import {Button, TextField, ColorSlider, ScrollBar} from "./gui/gui.js"
import { canvasWidth, canvasHeight } from "./engine/render.js"
import {Furniture} from "./objects/objects.js"
import { getMousePos } from "./engine/input.js"

const Coop = (function() {
// Chicken Coop Furniture
const functions = {
Expand Down Expand Up @@ -29,7 +43,7 @@ const Coop = (function() {
let data = coopFurniture[i]
let itemId = data.id

obj = new Furniture(PHYSICSWORLD, itemId, data.x, data.y, data.dir)
let obj = new Furniture(PHYSICSWORLD, itemId, data.x, data.y, data.dir)
obj.static = false // Temporary, for initial collision check

WORLD.spawnObject("Furniture", obj)
Expand Down Expand Up @@ -252,4 +266,6 @@ const Coop = (function() {
}
};

return functions; })()
return functions; })()

export default Coop;
18 changes: 16 additions & 2 deletions website/game/dialogue.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
// Dialogue System
// NOT the speech bubbles that apeear when when you first talk to an NPC

import {DRAW, SAVEDATA, PROFILE, WORLD, NETPLAY, CURSOR} from "./main.js"
import {IMG, SPRITE, ANIM, FONT, SFX, ITEMS} from "./assets.js"
import {HEXtoRGB, RGBtoHEX, removeNuggets, addNuggets, spendNuggets, addItem, removeItem, getItemCategory, getItemData, getItem} from "./savedata.js"
import {openMenu, closeMenu, getOpenMenu} from "./state.js"
import {PLAYER, PLAYER_CONTROLLER} from "./world.js"
import QuestSystem from "./quests.js"
import Transition from "./transition.js"
import AudioSystem from "./engine/audio.js"
import {checkCondition} from "./area.js"
import {Button, TextField, ColorSlider, ScrollBar} from "./gui/gui.js"
import { canvasWidth, canvasHeight } from "./engine/render.js"

const DialogueSystem = (function() {
let open = false // Dialogue is open?
let stage = 0 // Stage in dialogue sequence
Expand Down Expand Up @@ -367,7 +379,7 @@ const DialogueSystem = (function() {
responseButtons = []

// Check which responses are available
responses = []
let responses = []
for (let i = 0; i < allResponses.length; i++) {
let r = allResponses[i]
let doAdd = true
Expand Down Expand Up @@ -465,4 +477,6 @@ const DialogueSystem = (function() {
}
};

return functions; })()
return functions; })()

export default DialogueSystem;
4 changes: 3 additions & 1 deletion website/game/engine/audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,6 @@ const AudioSystem = (function() {
}
};

return functions; })()
return functions; })()

export default AudioSystem;
2 changes: 2 additions & 0 deletions website/game/engine/canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ resizeCanvas()

// Resize the canvas whenever the window size changes
window.addEventListener('resize', resizeCanvas)

export {canvas, ctx, canvasWidth, canvasHeight}
7 changes: 6 additions & 1 deletion website/game/engine/input.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Input Handler; This passes user inputs to the game with useful functions

import {canvas} from "./canvas.js"
import {stateKeyPress, stateKeyRelease, stateMouseClick, stateMouseRelease, stateScroll} from "../state.js"

// Recieve keyboard inputs
window.addEventListener("keydown", keyPressed)
window.addEventListener("keyup", keyReleased)
Expand Down Expand Up @@ -114,4 +117,6 @@ function touchMoved(event) {
event.preventDefault();
var touch = event.touches[0];
[mouseScreenX, mouseScreenY] = convertTouchCoordsToScreen(touch.clientX, touch.clientY)
}
}

export {getMousePos, checkMouseInside}
8 changes: 7 additions & 1 deletion website/game/engine/render.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Rendering functions for a '2d' canvas.

import { ctx, canvasWidth, canvasHeight } from "./canvas.js"

class Render {
constructor (canvas) {
this.c = false
Expand Down Expand Up @@ -253,6 +255,8 @@ class Render {
}
}

import LoadingScreen from "../loading.js"

class RenderImage {
constructor (src, asyncFunc) {
this.image = new Image()
Expand Down Expand Up @@ -311,4 +315,6 @@ class RenderFont {
this.name = name
this.size = size || 20
}
}
}

export { Render, RenderImage, RenderFont, canvasWidth, canvasHeight }
6 changes: 5 additions & 1 deletion website/game/engine/sprite.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//Sprite object; Takes in an image with multiple frames and splits it up to render just one frame

import { DRAW } from "../main.js"

class Sprite {
//Initialize: image, frame count x & y, offset x & y, frame width & height, offset width & height to start splitting the image
constructor (img, fx=1, fy=1, w, h, ox=0, oy=0, sepx=0, sepy=0) {
Expand Down Expand Up @@ -127,4 +129,6 @@ class DrawableSprite {
}
DRAW.image(this.sprite.img, anim, this.drawx, this.drawy)
}
}
}

export { Sprite, Animation, DrawableSprite }
8 changes: 7 additions & 1 deletion website/game/gui/gui.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { DRAW, CURSOR } from "../main.js"
import { FONT } from "../assets.js"
import { getMousePos } from "../engine/input.js"

class Button {
constructor(label="", action=()=>{}, graphic, x=0, y=0, w, h) { //in px, label is text on button, action is function to call when clicked
this.visible = true;
Expand Down Expand Up @@ -461,4 +465,6 @@ class ColorSlider {
DRAW.setColor(255,255,255,1);
DRAW.rectangle(this.x+this.w*this.value -2, this.y, 4, this.h, "line");
}
}
}

export {Button, TextField, ScrollBar, ColorSlider}
10 changes: 9 additions & 1 deletion website/game/gui/itemgrid.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
// Item button grid; Imagine the inventory from minecraft
import {DRAW, SAVEDATA, PROFILE, WORLD, NETPLAY, CURSOR} from "../main.js"
import {IMG, SPRITE, ANIM, FONT, ITEMS} from "../assets.js"
import { getMousePos } from "../engine/input.js"
import {Button, ScrollBar} from "./gui.js"
import {HEXtoRGB, RGBtoHEX, removeNuggets, addNuggets, spendNuggets, addItem, removeItem, getItemCategory, getItemData, getItem} from "../savedata.js"

class ItemGrid {
// Callback function, x pos, y pos, cell width, cell height, grid width, grid height
constructor(action, list, selectedFunc, x, y, cw=56, ch=56, gw, gh) {
Expand Down Expand Up @@ -228,4 +234,6 @@ class ItemGrid {
}
}
}
}
}

export {ItemGrid};
9 changes: 7 additions & 2 deletions website/game/gui/notification.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Notification
// A little popup that appears at the top of the screen

import { DRAW } from "../main.js"
import { FONT } from "../assets.js"

const Notify = (function() {
let notifications = [];
let notifHeight = 32;
Expand Down Expand Up @@ -80,11 +83,13 @@ const Notify = (function() {
// Text
DRAW.setColor(notification.color[0],notification.color[1],notification.color[2],1.0*a)

for (line = 0; line < notification.lines; line++) {
for (let line = 0; line < notification.lines; line++) {
DRAW.text(notification.text[line], x +3, y+line*30 + 25, "left")
}
y += notification.lines*notifHeight
}
}
}
return notifyFunctions; })()
return notifyFunctions; })()

export default Notify;
14 changes: 13 additions & 1 deletion website/game/items.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
// Behavior for items (Clothing, consumables, furniture, etc.)

import {DRAW, SAVEDATA, PROFILE, WORLD, NETPLAY, CURSOR} from "./main.js"
import {IMG, SPRITE, ANIM, FONT, ITEMS} from "./assets.js"
import {HEXtoRGB, RGBtoHEX, removeNuggets, addNuggets, spendNuggets, addItem, removeItem, getItemCategory, getItemData, getItem, makePetData} from "./savedata.js"
import {openMenu, closeMenu, getOpenMenu} from "./state.js"
import {PLAYER, PLAYER_CONTROLLER} from "./world.js"
import QuestSystem from "./quests.js"
import Transition from "./transition.js"
import Coop from "./coop.js"
import DialogueSystem from "./dialogue.js"

let activeItems = {} // List of items that are currently in player's inventory, being worn by other chickens, and being sold in area
// Mark down an item as active
function requestItem(itemId, itemType) {
Expand Down Expand Up @@ -112,4 +122,6 @@ function adoptPet(itemId, name="") {
if (PLAYER.petObj) {
PLAYER.petObj.updateProfile(SAVEDATA.pet, "sendToServer")
}
}
}

export {requestItem, compareItems, clearItems, useItem, adoptPet}
4 changes: 3 additions & 1 deletion website/game/lib/vec2.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ function vec2Norm(ex, ey) {
// Dot Product
function vec2Dot(x1, y1, x2, y2) {
return x1*x2 + y1*y2
}
}

export { vec2Len, vec2Unit, vec2Norm, vec2Dot }
8 changes: 7 additions & 1 deletion website/game/loading.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// Loading Screen

import { DRAW } from "./main.js"
import { IMG, SPRITE } from "./assets.js"
import { canvasWidth, canvasHeight } from "./engine/render.js"

const LoadingScreen = (function() {
// Loading
let loading = false
Expand Down Expand Up @@ -129,4 +133,6 @@ const LoadingScreen = (function() {
},
};

return functions; })()
return functions; })()

export default LoadingScreen
Loading

0 comments on commit 1e694f1

Please sign in to comment.