-
Notifications
You must be signed in to change notification settings - Fork 0
/
enermy.js
153 lines (136 loc) · 3.42 KB
/
enermy.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
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
152
(function(){
mit.Enermy = function(enermy_type)
{
this.init=function(enermy_type)
{
this.type = enermy_type;
this.isDead = false;
this.img = mit.image[enermy_type].obj; //[string]
this.audio = mit.image[enermy_type].audio;
this.w = mit.image[enermy_type].size_w;
this.h = mit.image[enermy_type].size_h;
this.up_speed = 0;
this.left_speed = mit.Background.water_speed+5;
this.min_y = mit.World.water_level;
this.max_y = mit.World.world_h - this.h;
};
this.playAudio = function()
{
};
};
mit.Enermy.prototype=mit.Sprite;
mit.EnermyFactory = {
sprite_array : [],
enermy_count: 5,
types: ["coin","coin","coin","food","food","enermy","enermy"],
enermy_types: ["octopus","cuttlefish"],
food_types: ["fish","fish2","fish3"],
create: function()
{
//if condition allow
var type_name = this.create_type_name();
//create new enermy
var new_sprite = new mit.Enermy(type_name);
new_sprite.init(type_name);
//if ok
new_sprite.pos_y = this.getRandomPos().pos_y;
new_sprite.pos_x = this.getRandomPos().pos_x;
//add it to array
this.sprite_array.push(new_sprite);
},
create_type_name : function()
{
var r1 = common.randomNumber(0,this.types.length-1);
var type = this.types[r1];
var type_name;
switch(type)
{
case "food":
type_name = this.food_types[common.randomNumber(0,this.food_types.length-1)];
break;
case "coin":
type_name = "coin";
break;
case "enermy":
type_name = this.enermy_types[common.randomNumber(0,this.enermy_types.length-1)];
break;
default:
type_name = "fish";
}
//console.log(r1+" "+type+ " "+type_name+" ");
return type_name;
},
getRandomPos : function()
{
var pos = {};
pos.pos_y = common.randomNumber(mit.World.water_level, mit.World.world_h - 50);
//pos.pos_y =200;
var last = this.sprite_array[this.sprite_array.length-1];
if(last)
{
pos.pos_x = last.pos_x + common.randomNumber(50,500);
}
else
{
pos.pos_x = mit.World.world_w;
}
return pos;
},
checkOutOfScreen : function()
{
var size = this.sprite_array.length;
for(var i=0; i<size;i++)
{
if(this.sprite_array[i].outOfLeftScreen())
{
this.sprite_array[i].isToclear = true;
this.sprite_array[i] = null;
this.sprite_array.splice(i,1);
size--;
i--;
}
}
},
checkCollison : function()
{
//check all collsion
//do something to whale
//delete it from array
var size = this.sprite_array.length;
var whale_bounds = mit.whale.getBounds();
for(var i=0; i<size;i++)
{
if(common.intersect( this.sprite_array[i].getBounds(), whale_bounds))
{
mit.whale.afterCollison(this.sprite_array[i].type);
this.sprite_array[i].isToclear = true;
this.sprite_array[i].isDead = true;
this.sprite_array[i] = null;
this.sprite_array.splice(i,1);
size--;
i--;
}
}
},
draw: function(content)
{
//after create finished
//after collison finished
//draw enermy in array
if(this.sprite_array.length <= this.enermy_count)
{
this.create();
}
this.checkOutOfScreen();
this.checkCollison();
if(this.sprite_array.length==0) return;
this.sprite_array.forEach(function(s,index,array)
{
s.pos_x -= s.left_speed;
try{
content.drawImage(s.img, s.pos_x, s.pos_y, s.w, s.h );
}catch(e){console.error("draw sprite array fail");}
});
}
};
}());