-
Notifications
You must be signed in to change notification settings - Fork 0
/
interactable.js
122 lines (111 loc) · 3.94 KB
/
interactable.js
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
import { ItemFrame } from "./itemFrame.js"
import { Rect } from "./rect.js";
export class Interactable {
constructor (name, x, y, length, width, colour, type, gameTime, id){
this.name = name;
this.x = x;
this.y = y;
this.xSpeed = 0;
this.ySpeed = 0;
this.length = length;
this.width = width;
this.colour = colour;
this.type = type;
this.id = id;
this.expireTime = -1;
this.creationTime = gameTime;
if (this.name == "lobby"){
this.id = -2
}
if (this.type == "portal"){
this.text = "Press E to enter: " + this.name
} else if (this.type == "bag") {
this.text = "Press E to open";
this.expireTime = 30000;
this.inventory = {
inventorySize: 8,
items: [],
rects: [new Rect(900, 50, 400, 400)]
}
for (var i = 0; i < this.inventory.inventorySize; i ++){
if (i <this.inventory.inventorySize/2){
this.inventory.items.push(new ItemFrame("", i, 930 + i*(350/(this.inventory.inventorySize/2)), 100, 80, 80));
} else {
this.inventory.items.push(new ItemFrame("", i, 930 + (i-this.inventory.inventorySize/2)*
(350/(this.inventory.inventorySize/2)), 190, 80, 80));
}
}
} else if (this.type == "healStation"){
this.text = "Press E to spawn healing"
} else if (this.type == "button"){
this.text = "Press E to pull switch"
}
}
touching(rectList){
for(var i = 0; i < rectList.length; i++){
if (this.rectRectDetect(this, rectList[i]) && this != rectList[i]){
return true;
}
}
return false;
}
checkExpire(gameTime, interactables){
if (gameTime - this.creationTime > this.expireTime && this.expireTime != -1){
interactables.splice(interactables.indexOf(this), 1)
}
}
checkForDuplicate(array){
for (var i = 0; i < array.length; i ++){
if (array[i].id == this.id){
return true;
}
}
return false;
}
checkEmpty(){
for (var i = 0; i < this.inventory.items.length; i ++){
if (this.inventory.items[i].itemName != ""){
return false;
}
}
return true;
}
addToInventory(itemName){
var itemAdded = false;
for (var i = 0; i < this.inventory.items.length && !itemAdded; i ++){
if (this.inventory.items[i].itemName == ""){
this.inventory.items[i].itemName = itemName;
this.inventory.items[i].refreshItem();
itemAdded = true;
}
}
}
update(blocks){
this.x += this.xSpeed;
for (var c = 0; c < blocks.length; c ++){
if (this.rectRectDetect(this, blocks[c]) && this != blocks[c] && blocks[c].type != "blood"){
this.x += -this.xSpeed;
this.xSpeed = 0;
}
}
this.ySpeed += 0.3;
this.y += this.ySpeed;
for (var c = 0; c < blocks.length; c ++){
if (this.rectRectDetect(this, blocks[c]) && this != blocks[c] && blocks[c].type != "blood"){
this.y += -this.ySpeed;
this.ySpeed = 0;
}
}
}
rectRectDetect(rect, rect2){
var leftSide = rect.x;
var rightSide = rect.x + rect.length;
var topSide = rect.y;
var botSide = rect.y + rect.width;
if (rect2.x + rect2.length > leftSide && rect2.x < rightSide && rect2.y + rect2.width> topSide && rect2.y < botSide){
return true;
} else {
return false;
}
}
}