-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
151 lines (121 loc) · 4.79 KB
/
script.js
File metadata and controls
151 lines (121 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
var current_state;
var game_data;
var invalidTransition = false; //boolean for invalid paths
var itemsCollected = []; //an array for the items to check if it is already collected
setTimeout(function(){
$('#splash').hide();
$('#main').show();
canvas.style.display = "display: block; margin: 0 auto;" //makes canvas in the middle
}, 4500); //4500
var itemsList = document.getElementById("itemList"); //so i can print out how many items in html
var items = 0; //for the json
itemsList.innerHTML = items; //connects them
const canvas = document.querySelector("canvas");
resize();
window.addEventListener('resize', resize);
$.getJSON( "game.json", function( data ) {
game_data = data;
current_state = data['start_state'];
setBackgroundImage();
load_imgs();
});
function resize(){
canvas.width = window.innerWidth - 83;
canvas.height = window.innerHeight - 125;
try{
draw(game_data['states'][current_state]['bk_img_loaded']);
}catch{}
}
function setBackgroundImage(){
if (game_data && game_data['states'][current_state] && game_data['states'][current_state]['bg_img']) {
var backgroundImage = game_data['states'][current_state]['bg_img'];
document.body.style.backgroundImage = `url(${backgroundImage})`;
}
}
function all_loaded(){
draw(game_data['states']['0,0a']['bk_img_loaded']);
console.log(current_state);
}
function load_imgs(){
for (var key in game_data['states']){
if(game_data['states'][key]['bk_img'] != null){
console.log( key, game_data['states'][key]['bk_img']);
game_data['states'][key]['bk_img_loaded'] = new Image();
game_data['states'][key]['bk_img_loaded'].onload = function(){ //gets the first image
all_loaded(); //calls after the image is loaded
};
game_data['states'][key]['bk_img_loaded'].src = game_data['states'][key]['bk_img'];
}
}
}
const up_arrow = 38;
const down_arrow = 40;
const left_arrow = 37;
const right_arrow = 39;
var cur_pos_x = 0;
var cur_pos_y = 0;
document.body.onkeydown = function(e){
if(e.keyCode == up_arrow){
cur_pos_x = cur_pos_x +1;
}
//alert(String.fromCharCode(e.keyCode)+" --> "+e.keyCode);
key_input(e.keyCode)
};
function key_input(what_key) {
for (var i = 0; i < game_data['states'][current_state]['next_state'].length; i++) {
if (what_key == game_data['states'][current_state]['next_state'][i]['key_input']) {
if (game_data['states'][current_state]['next_state'][i].invalid) { //if the next state has an invalid
invalidTransition = true; //set to true
}
next_state(game_data['states'][current_state]['next_state'][i]['state_name']) //move onto the next state
break;
}
}
}
function next_state( state) {
console.log("Current State = " + current_state + " --> New State= " + state)
current_state = state
console.log("Updated Current State = " + current_state);
draw(game_data['states'][current_state]['bk_img_loaded']);
if (invalidTransition) { //if there is an invalid transition
drawText(); //draw invalid text
setTimeout(function () {
draw(game_data['states'][current_state]['bk_img_loaded']);
invalidTransition = false; //reset the bool
}, 600); //will delete after
}
if (game_data['states'][current_state]['items'] != null && !itemsCollected[current_state]) { //if there is an item and if the item wasnt already collected
items += game_data['states'][current_state]['items']; //add to it for the counter
itemsList.innerHTML = items; //print it in html
itemsCollected[current_state] = true; //mark the items as collected in this state
}
if (items == 6) { //checks if there has been 6 items yet
document.getElementById("items").classList.add('green-items'); //changes text to green
}
}
function draw(img) {
var ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
}
function draw_src(src, x, y, w, h) {
var ctx = canvas.getContext('2d');
var img = new Image();
img.onload = function() {
ctx.drawImage(img, x, y, w, h);
};
img.src = src;
}
function drawText() {
var ctx = canvas.getContext('2d');
ctx.font = "bolder 50px Anton";
var gradient = ctx.createLinearGradient(0, 0, canvas.width, 0);
gradient.addColorStop(0, "#E51717");
gradient.addColorStop(1, "#FFFFFF");
ctx.fillStyle = gradient;
ctx.strokeStyle = "black";
ctx.lineWidth = 8;
ctx.textAlign = "center";
ctx.strokeText("Invalid Path", canvas.width / 2, canvas.height / 2);
ctx.fillText("Invalid Path", canvas.width / 2, canvas.height / 2);
}