forked from mastifikator/FantasyJavaPatterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDamageMediator.java
More file actions
50 lines (36 loc) · 1.72 KB
/
Copy pathDamageMediator.java
File metadata and controls
50 lines (36 loc) · 1.72 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
package Mediator;
public class DamageMediator implements Runnable {
Unit unitAttacker;
Unit unitAttacked;
public DamageMediator(Unit unitAttacker, Unit unitAttacked){
this.unitAttacker = unitAttacker;
this.unitAttacked = unitAttacked;
}
@Override
public void run() {
while(unitAttacked.getHealth() > 0 && unitAttacker.getHealth() > 0) {
//тут можно реализовать сложную логику нанесения повреждений
synchronized (this) {
unitAttacked.health -= unitAttacker.getDamage();
System.out.println(unitAttacker.getName() + " нанес " + unitAttacked.getName() + " " + unitAttacker.getDamage() + " повреждений");
if (unitAttacked.health <= 0) {
System.out.println(unitAttacked.getName() + " погиб");
unitAttacked.dead();
System.out.println(unitAttacker.getName() + " победил!");
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
unitAttacker.health -= unitAttacked.getDamage();
System.out.println(unitAttacked.getName() + " нанес " + unitAttacker.getName() + " " + unitAttacked.getDamage() + " повреждений");
if (unitAttacker.health <= 0) {
System.out.println(unitAttacker.getName() + " погиб");
unitAttacker.dead();
System.out.println(unitAttacked.getName() + " победил!");
}
}
}
}
}