xxxxxxxxxx
469
let scene = 0;
let astro, spaceship;
let sleep_pod;
let characters_scene_1 = [];
let characters_scene_2 = [];
let in_dialogue = true;
let dialogue_line = "Kill time while waiting for the ship";
let current_object;
let background_sky;
let renew_screen_OK = false;
//////////////////////////////////////////////////////////////////////////
//////////////////////////MAIN FUNCTIONS//////////////////////////////////
//////////////////////////////////////////////////////////////////////////
function preload() {
dialogue_background = loadImage("assets/dialogue_background.png");
title = loadImage("assets/title.png");
title_on_click = loadImage("assets/title_on_click.png");
renew_screen = loadImage("assets/renew_screen.png");
scene_1_background = loadImage("assets/scene_1_background.png");
scene_2_background = loadImage("assets/base_background-blue.png");
scene_2_background_sky = loadImage("assets/base_night_background-1.png");
sleep_pod_image = loadImage("assets/sleep_pod.png");
astro = new Obj(loadImage("assets/astro.png"), 20, height / 2);
astro.loadAnimation("assets/astronaut/pixil-frame-", 8);
astro.rename("astro");
spaceship_img = loadImage("assets/spaceship.png");
font = loadFont("assets/font.ttf");
console.log("Preload Check");
}
function setup() {
createCanvas(600, 400);
astro.limit_area(0, width, height/2, height);
spaceship = new Obj(spaceship_img, width - spaceship_img.width, int(random(-100, -200)));
spaceship.rename("spaceship");
console.log("Setup check");
}
function draw() {
switch (scene) {
case 1:
scene_1();
break;
case 2:
scene_2();
break;
case 3:
scene_3();
break;
default:
titlePage();
break;
}
}
//////////////////////////////////////////////////////////////////////////
//////////////////////////SCENES AND INIT/////////////////////////////////
//////////////////////////////////////////////////////////////////////////
function titlePage () {
image(title, 0, 0, 600, 400);
}
function scene_1_init() {
let gen_num = int(random(0, 11));
console.log("silos generated: " + gen_num);
for(let i = 0; i < gen_num; i ++) {
let temp_img = loadImage("assets/silo/pixil-frame-" + int(random(0, 5)) + ".png");
let temp = new Obj(temp_img, int(random(0, width - temp_img.width)), int(random(height/2, height - temp_img.height)));
temp.limit_area(0, width, height / 2, height);
temp.loadAnimation("assets/silo/pixil-frame-", 6);
temp.movement = true;
temp.loadInteractionMethod(temp.call_dialogue);
characters_scene_1.unshift(temp);
}
astro.reset_movement(20, height / 2);
spaceship.update_line ("Its time to go.");
spaceship.loadInteractionMethod(spaceship.call_dialogue);
spaceship.reset_movement(width - spaceship_img.width, int(random(-500, -2500)));
spaceship.move(spaceship.x, height - spaceship_img.height - 10);
characters_scene_1.unshift(spaceship);
//Mini game 1 - placeholder
// placeholder_1 = new Obj(loadImage("assets/placeholder.png"), int(random(width / 7, width * 6/7)), int(random(height*2.1/3, height *7 / 8)));
// placeholder_1.loadInteractionMethod(placeholder_1.call_dialogue);
// placeholder_1.line = "this is a placeholder";
// characters_scene_1.unshift(placeholder_1);
console.log("scene 1 init check");
}
function scene_1() {
image(scene_1_background, 0, 0);
for(let i = 0; i < characters_scene_1.length; i ++){
characters_scene_1[i].draw();
}
astro.draw();
dialogue();
}
function scene_2_init() {
characters_scene_1 = [];
astro.reset_movement(20, height * 2/ 3);
spaceship.reset_movement(20, height * 1.7 / 3);
spaceship.update_line("Rest well for another day.");
spaceship.loadInteractionMethod(spaceship.call_dialogue);
background_sky = new Obj(scene_2_background_sky, width / 2, height - 1000 - scene_2_background_sky.height / 2);
background_sky.set_targetWidth(600);
background_sky.move(background_sky.x, background_sky.targetHeight / 2);
background_sky.velocity_multiplier = 0.2;
background_sky.interactive = false;
sleep_pod = new Obj(sleep_pod_image, width * 2 / 5, height * 2 / 3);
sleep_pod.update_line("Good night.");
sleep_pod.rename("sleep_pod");
sleep_pod.loadInteractionMethod(sleep_pod.call_dialogue);
background_image = new Obj(scene_2_background, width / 2, height / 2);
background_image.interactive = false;
// characters_scene_2.unshift(astro);
characters_scene_2.unshift(sleep_pod);
characters_scene_2.unshift(spaceship);
characters_scene_2.unshift(background_image);
characters_scene_2.unshift(background_sky);
console.log("Scene 2 init Check");
}
function scene_2(){
for(let i = 0; i < characters_scene_2.length; i ++) {
characters_scene_2[i].draw();
}
astro.draw();
dialogue();
}
function scene_3_init() {
characters_scene_2 = [];
}
function scene_3() {
image(renew_screen, 0, 0);
}
//////////////////////////////////////////////////////////////////////////
//////////////////////////HELPER FUNCTIONS////////////////////////////////
//////////////////////////////////////////////////////////////////////////
function checkContact(character_group) {
for(let i = 0; i < character_group.length; i ++) {
if(character_group[i].interactive) {
let cX = character_group[i].x;
let cY = character_group[i].y;
let aX = astro.x;
let aY = astro.y;
let cIWidth = character_group[i].img.width;
let cIHeight = character_group[i].img.height;
if(mouseX >= cX - cIWidth/2 && mouseX <= cX + cIWidth/2 && mouseY >= cY - cIHeight / 2 && mouseY <= cY + cIHeight / 2) {
if(aX >= cX - cIWidth/2 - 5 & aX <= cX + cIWidth / 2 + 5 && aY >= cY - cIHeight /2 - 5 && aY <= cY + cIHeight / 2 - 5) {
character_group[i].interact();
}
}
}
}
console.log("Interaction Check: called");
}
function dialogue() {
if (in_dialogue) {
let anchor_x = (width / 2) - dialogue_background.width / 2;
let anchor_y = (height * 7 / 8) - dialogue_background.height / 2;
push();
image(dialogue_background, anchor_x, anchor_y);
if(dialogue_line) {
textFont(font);
text(dialogue_line, anchor_x + 10, anchor_y + 10, 130, 40);
}
pop();
}
}
function check_for_dialogue_actions(following_action) {
if(in_dialogue) {
if(mouseX >= (width / 2) - dialogue_background.width / 2 && mouseX <= (width / 2) + dialogue_background.width && mouseY >= (height * 7 / 8) - dialogue_background.height / 2 && mouseY <= (height * 7 / 8) + dialogue_background.height / 2) {
in_dialogue = false;
following_action();
}
}
}
//////////////////////////////////////////////////////////////////////////
//////////////////////////MOUSE CONTROLS//////////////////////////////////
//////////////////////////////////////////////////////////////////////////
function mousePressed() {
if(mouseY <= height && mouseY >= 0 && mouseX <= width && mouseX >= 0){
switch (scene) {
case 1:
astro.move(mouseX, mouseY);
checkContact(characters_scene_1);
check_for_dialogue_actions(()=>{
if(current_object.name === "spaceship") {
scene_2_init();
scene += 1;
} else {
current_object.vanish();
}
});
break;
case 2:
astro.move(mouseX, mouseY);
checkContact(characters_scene_2);
check_for_dialogue_actions(()=>{
if(current_object.name === "sleep_pod") {
scene_3_init();
scene += 1;
}
});
break;
case 3:
break;
default:
temp = title;
title = title_on_click;
break;
}
}
}
function mouseReleased() {
if(mouseY <= height && mouseY >= 0 && mouseX <= width && mouseX >= 0) {
switch (scene) {
case 1:
break;
case 2:
break;
case 3:
if(renew_screen_OK){
scene_1_init();
scene = 1;
renew_screen_OK = false;
} else {
renew_screen_OK = true;
}
break;
default:
scene = 1;
title = temp;
scene_1_init();
break;
}
}
}
//////////////////////////////////////////////////////////////////////////
//////////////////////////OBJECT CLASS////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
class Obj {
constructor(img, x, y) {
this.img = img;
this.stationary_img = img;
this.img_left;
this.animations = [];
this.animation_index = 0;
this.animation_timestamp = 0;
this.interactive = true;
this.targetWidth;
this.targetHeight;
this.name = "default";
this.line = "???????????????";
this.interaction_method = ()=> {console.log('Interaction triggered: method not defined | Object name: ' + this.name);};
this.opacity = 255;
this.vanishing = false;
this.movement = false;
this.x = (x) ? x : 0;
this.y = (y) ? y : 0;
this.velocityX = 1;
this.velocityY = 1;
this.velocity_multiplier = 1;
}
set_targetWidth(x) {
this.targetWidth = x;
this.targetHeight = this.targetWidth * this.img.height / this.img.width;
}
rename(name) {
this.name = name;
}
move(x, y) {
if(x) {
this.targetX = x;
this.velocityX = (this.targetX - this.x) / Math.abs(this.targetX - this.x);
}
if(y) {
this.targetY = y;
this.velocityY = (this.targetY - this.y) / Math.abs(this.targetY - this.y);
}
if(this.targetX && Math.abs(this.x - this.targetX) >= 3) {
this.x += this.velocityX * this.velocity_multiplier;
this.movement = true;
}
if(this.targetY && Math.abs(this.y - this.targetY) >= 3) {
this.y += this.velocityY * this.velocity_multiplier;
this.movement= true;
}
if(this.movement) {
this.movement = !(Math.abs(this.targetY - this.y) < 3 && Math.abs(this.targetX - this.x) < 3);
};
}
reset_movement(x, y) {
this.x = x;
this.y = y;
this.targetX = null;
this.targetY = null;
this.movement = false;
}
draw() {
push();
imageMode(CENTER);
tint(255, this.opacity);
image(this.img, this.x, this.y, this.targetWidth, this.targetHeight);
pop();
this.iterate();
}
loadInteractionMethod(method) {
this.interaction_method = method;
}
interact() {
this.interaction_method();
this.iterate();
}
call_dialogue() {
in_dialogue = true;
current_object = this;
dialogue_line = this.line;
dialogue();
}
update_line (line){
this.line = line;
}
loadAnimation(filename, item_count) {
let temp = [];
for(let i = item_count - 1; i >= 1; i --) {
temp.unshift(loadImage(filename+i+".png"));
}
this.animations.unshift(temp);
}
updateImg() {
if(this.movement && this.animations.length >= 1) {
this.img = this.animations[0][this.animation_index];
temp = millis() - this.animation_timestamp;
if(temp >= 100) {
this.animation_index += 1;
this.animation_timestamp = millis();
}
this.animation_index %= this.animations[0].length;
} else {
this.img = this.stationary_img;
this.animation_index = 0;
}
}
vanish() {
this.opacity -= 3;
this.vanishing = true;
if(this.opacity <= 0) {
this.opacity = 0;
this.vanishing = false;
this.loadInteractionMethod(()=>{});
}
}
reset_opacity() {
this.opacity = 255;
this.vanishing = false;
}
limit_area(xmin, xmax, ymin, ymax) {
this.limitXMin = (xmin) ? xmin : this.limitXMin;
this.limitXMax = (xmax) ? xmax : this.limitXMax;
this.limitYMin = (ymin) ? ymin : this.limitYMin;
this.limitYMax = (ymax) ? ymax : this.limitYMax;
this.x = (this.x >= this.limitXMax - this.img.width) ? (this.limitXMax - this.img.width) : this.x;
this.x = (this.x <= this.limitXMin) ? this.limitXMin : this.x;
this.y = (this.y >= this.limitYMax - this.img.height) ? (this.limitYMax - this.img.height) : this.y;
this.y = (this.y <= this.limitYMin) ? this.limitYMin : this.y;
}
is_collide(another_object) {
return ((this.x + this.img.width) >= another_object.x && this.x <= (another_object.img.width + another_object.x)) &&
((this.y + this.img.height) >= another_object.y && this.y <= (another_object.img.height + another_object.y));
}
iterate() {
if(this.limitXMax) {this.limit_area();}
this.move();
if(this.vanishing) {this.vanish();}
this.updateImg();
}
}