/
Entity.java
81 lines (67 loc) · 2.17 KB
/
Entity.java
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
import java.util.*;
import java.io.*;
import javax.sound.sampled.*;
public abstract class Entity
{
boolean dead;
Vector<String> sounds = new Vector<String>();
World world;
char sprite;
double pos;
double vel;
//every entity needs these things
abstract public void update();
abstract public void destroy();
//every entity needs these things and will probably use the same code for them
public void draw()
{
world.screen.setChar((int)pos, sprite);
}
public double getPos()
{
return pos;
}
public boolean collides(Entity me, double pos)
{
//if there is nothing there OR there is something there but it is just me there is no collision
return world.getEntsAt(pos).size()==0||(world.getEntsAt(pos).size()==1&&world.getEntsAt(pos).contains(me));
}
//overloaded function used to exlude a type of entity from detection of a collision
public boolean collides(Entity me, Class type, double pos)
{
Vector<Entity> ents = world.getEntsAt(pos);
for (Entity ent : ents)
{
if(ent.getClass()!=type)
{
if(ent!=me)
{
return true;
}
}
}
return false;
}
//this is reasonable because each entity will only have one type of sound to play
//this bit of code is extremely inefficient in terms of memory because streams are not explicitly disposed,
//rather they are left for the garbage collector to take care of.
//a relatively easy fix would be to use threads for every sound played so that the streams can be closed when no longer used
public void playSound()
{
Random generator = new Random();
try
{
InputStream audioSrc = getClass().getResourceAsStream(sounds.get(generator.nextInt(sounds.size())));
InputStream bufferedIn = new BufferedInputStream(audioSrc);
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(bufferedIn);
AudioFormat audioFormat = audioInputStream.getFormat();
DataLine.Info dataLineInfo = new DataLine.Info(Clip.class, audioFormat);
Clip clip = (Clip) AudioSystem.getLine(dataLineInfo);
clip.open(audioInputStream);
clip.start();
} catch (Exception e)
{
e.printStackTrace();
}
}
}