Skip to content

Commit

Permalink
Code
Browse files Browse the repository at this point in the history
  • Loading branch information
KhushalGupta305 authored Sep 30, 2018
1 parent 5927eb8 commit 4f133f1
Show file tree
Hide file tree
Showing 5 changed files with 245 additions and 0 deletions.
45 changes: 45 additions & 0 deletions Board.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package net.obviam.bt;

import java.util.ArrayList;
import java.util.List;

public class Board {

final int width;
final int height;

private List<Droid> droids = new ArrayList<Droid>();

public Board(int width, int height) {
this.width = width;
this.height = height;
}

public int getWidth() {
return width;
}

public int getHeight() {
return height;
}

public void addDroid(Droid droid) {
if (isTileWalkable(droid.getX(), droid.getY())) {
droids.add(droid);
droid.setBoard(this);
}
}

public boolean isTileWalkable(int x, int y) {
for (Droid droid : droids) {
if (droid.getX() == x && droid.getY() == y) {
return false;
}
}
return true;
}

public List<Droid> getDroids() {
return droids;
}
}
105 changes: 105 additions & 0 deletions Droid.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package net.obviam.bt;

import net.obviam.bt.ai.Routine;

public class Droid {

final String name;
int x;
int y;
int range;
int damage;
int health;

Routine routine;
Board board;

public Droid(String name, int x, int y, int health, int damage, int range) {
this.name = name;
this.x = x;
this.y = y;
this.health = health;
this.damage = damage;
this.range = range;
}

public void update() {
if (routine.getState() == null) {
// hasn't started yet so we start it
routine.start();
}
routine.act(this, board);
}

public int getX() {
return x;
}

public void setX(int x) {
this.x = x;
}

public int getY() {
return y;
}

public void setY(int y) {
this.y = y;
}

public int getRange() {
return range;
}

public void setRange(int range) {
this.range = range;
}

public int getDamage() {
return damage;
}

public void setDamage(int damage) {
this.damage = damage;
}

public int getHealth() {
return health;
}

public void setHealth(int health) {
this.health = health;
}

public boolean isAlive() {
return health > 0;
}

public Routine getRoutine() {
return routine;
}

public void setRoutine(Routine routine) {
this.routine = routine;
}

public String getName() {
return name;
}

public void setBoard(Board board) {
this.board = board;
}

@Override
public String toString() {
return "Droid{" +
"name=" + name +
", x=" + x +
", y=" + y +
", health=" + health +
", range=" + range +
", damage=" + damage +
'}';
}
}
25 changes: 25 additions & 0 deletions Main.form
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="net.obviam.bt.Main">
<grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="5" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="521" height="403"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="a88e" binding="canvas" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="2" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<background color="-1118482"/>
<enabled value="true"/>
</properties>
<border type="none"/>
<children/>
</grid>
</children>
</grid>
</form>
45 changes: 45 additions & 0 deletions Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package net.obviam.bt;

import net.obviam.bt.ai.Routine;
import net.obviam.bt.ai.Routines;

import javax.swing.*;

public class Main {
private JPanel panel1;
private JPanel mainPanel;
private JPanel canvas;

public static void main(String[] args) {
// JFrame frame = new JFrame("Main");
// frame.setPreferredSize(new Dimension(800, 600));
// frame.setResizable(false);
// frame.setContentPane(new Main().mainPanel);
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// frame.pack();
// frame.setVisible(true);

Board board = new Board(25, 25);
Droid droid1 = new Droid("Droid_1", 2, 2, 10, 1, 3);
Droid droid2 = new Droid("Droid_2", 10, 10, 10, 2, 2);

Routine brain1 = Routines.sequence(
Routines.moveTo(5, 10),
Routines.moveTo(15, 12),
Routines.moveTo(2, 4)
);
droid1.setRoutine(brain1);

Routine brain2 = Routines.sequence(
Routines.repeat(Routines.wander(board), 4)
);
droid2.setRoutine(brain2);

for (int i = 0; i < 30; i++) {
System.out.println(droid1.toString());
System.out.println(droid2.toString());
droid1.update();
droid2.update();
}
}
}
25 changes: 25 additions & 0 deletions Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package net.obviam.bt;

import net.obviam.bt.ai.Repeat;
import net.obviam.bt.ai.Routine;
import net.obviam.bt.ai.Wander;

public class Test {

public static void main(String[] args) {
// Setup
Board board = new Board(10, 10);

Droid droid = new Droid("MyDroid", 5, 5, 10, 1, 2);
board.addDroid(droid);

Routine routine = new Repeat((new Wander(board)));
droid.setRoutine(routine);
System.out.println(droid);

for (int i = 0; i < 10; i++) {
droid.update();
System.out.println(droid);
}
}
}

0 comments on commit 4f133f1

Please sign in to comment.