source: lost-haven/main/Weapon.java@ 4d8825f

Last change on this file since 4d8825f was 8edd04e, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 4 years ago

Make the decompiled game code compile successfully

  • Property mode set to 100644
File size: 4.7 KB
RevLine 
[8edd04e]1package main;
2
3import java.awt.image.BufferedImage;
4import java.util.*;
5
6public class Weapon extends Item {
7
8 BufferedImage[] imgProj;
9
10 protected LinkedList<Effect> spawnEffects;
11 protected LinkedList<Effect> contactEffects;
12
13 private int attackSpeed;
14 private int range;
15
16 public Weapon(String name, ItemType type, String img, BufferedImage[] imgProj, int attackSpeed, int range, int damage) {
17 super(name, type, "weapons/" + img);
18 this.imgProj = imgProj;
19 this.spawnEffects = new LinkedList<Effect>();
20 this.contactEffects = new LinkedList<Effect>();
21 this.contactEffects.add(new Damage(damage, TargetType.Enemy));
22 this.attackSpeed = attackSpeed;
23 this.range = range;
24 }
25
26 public Weapon(Weapon copy, Point loc) {
27 super(copy, loc);
28 this.imgProj = copy.imgProj;
29 this.spawnEffects = new LinkedList<Effect>();
30 Iterator<Effect> iter = copy.spawnEffects.iterator();
31 while (iter.hasNext()) {
32 this.spawnEffects.add(((Effect)iter.next()).copy());
33 }
34 this.contactEffects = new LinkedList<Effect>();
35 iter = copy.contactEffects.iterator();
36 while (iter.hasNext()) {
37 this.contactEffects.add(((Effect)iter.next()).copy());
38 }
39 this.attackSpeed = copy.attackSpeed;
40 this.range = copy.range;
41 }
42
43 public Weapon copy(Point loc) {
44 return new Weapon(this, loc);
45 }
46
47 public void addSpawnEffect(Effect effect) {
48 this.spawnEffects.add(effect);
49 }
50
51 public void applySpawnEffects(Creature cr) {
52 Iterator<Effect> iter = this.spawnEffects.iterator();
53 while (iter.hasNext()) {
54 ((Effect)iter.next()).applyEffect(cr);
55 }
56 }
57
58 public void addContactEffect(Effect effect) {
59 this.contactEffects.add(effect);
60 }
61
62 public void applyContactEffects(Creature cr) {
63 Iterator<Effect> iter = this.contactEffects.iterator();
64 while (iter.hasNext()) {
65 ((Effect)iter.next()).applyEffect(cr);
66 }
67 }
68
69 private int calcDirection(Point loc, Point target) {
70 int dir, xDif2 = Point.xDif(loc, target);
71 int yDif2 = Point.yDif(target, loc);
72 double angle = 1.5707963267948966D;
73 if (xDif2 == 0) {
74 if (yDif2 != 0) {
75 angle *= Math.abs(yDif2) / yDif2;
76 }
77 } else {
78 angle = Math.atan(yDif2 / xDif2);
79 }
80 if (1.1780972450961724D <= angle) {
81 dir = 0;
82 } else if (0.39269908169872414D <= angle && angle < 1.1780972450961724D) {
83 dir = 1;
84 } else if (-0.39269908169872414D <= angle && angle < 0.39269908169872414D) {
85 dir = 2;
86 } else if (-1.1780972450961724D <= angle && angle < -0.39269908169872414D) {
87 dir = 3;
88 } else {
89 dir = 4;
90 }
91 if (xDif2 < 0)
92 dir = (dir + 4) % 8;
93 return dir;
94 }
95
96 public Projectile spawnProjectile(Point loc, Point target) {
97 Projectile proj = new Projectile(this.imgProj[calcDirection(loc, target)], loc);
98 proj.addSpawnEffects(this.spawnEffects);
99 proj.addContactEffects(this.contactEffects);
100 proj.setTarget(target);
101 return proj;
102 }
103
104 public int getDamage() {
105 return ((Damage)getContactEffect(EffectType.Damage)).getDamage();
106 }
107
108 public int getAttackSpeed() {
109 return this.attackSpeed;
110 }
111
112 public int getRange() {
113 return this.range;
114 }
115
116 public Effect getSpawnEffect(EffectType type) {
117 Iterator<Effect> iter = this.spawnEffects.iterator();
118 while (iter.hasNext()) {
119 Effect cur = iter.next();
120 if (cur.getType() == type) {
121 return cur;
122 }
123 }
124 return null;
125 }
126
127 public Effect getContactEffect(EffectType type) {
128 Iterator<Effect> iter = this.contactEffects.iterator();
129 while (iter.hasNext()) {
130 Effect cur = iter.next();
131 if (cur.getType() == type) {
132 return cur;
133 }
134 }
135 return null;
136 }
137
138 public void setDamage(int damage) {
139 ((Damage)getContactEffect(EffectType.Damage)).setDamage(damage);
140 }
141
142 public void setAttackSpeed(int attackSpeed) {
143 this.attackSpeed = attackSpeed;
144 }
145
146 public void setRange(int range) {
147 this.range = range;
148 }
149
150 public void setSpawnEffect(Effect effect) {
151 Iterator<Effect> iter = this.spawnEffects.iterator();
152 Effect target = null;
153 while (iter.hasNext() && target == null) {
154 Effect cur = iter.next();
155 if (cur.getType() == effect.getType()) {
156 target = cur;
157 }
158 }
159 if (target != null) {
160 this.spawnEffects.remove(target);
161 }
162 this.spawnEffects.add(effect);
163 }
164
165 public void setContactEffect(Effect effect) {
166 Iterator<Effect> iter = this.contactEffects.iterator();
167 Effect target = null;
168 while (iter.hasNext() && target == null) {
169 Effect cur = iter.next();
170 if (cur.getType() == effect.getType()) {
171 target = cur;
172 }
173 }
174 if (target != null) {
175 this.contactEffects.remove(target);
176 }
177 this.contactEffects.add(effect);
178 }
179}
Note: See TracBrowser for help on using the repository browser.