Jump to content
  • Advertisement
Sign in to follow this  
BlackSunStudios

Help Again with wire coding (java)

This topic is 2606 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Sorry guys need some help again.

Im writing a "sequel" to a game ive made called Extron.

Ive written the first classes of code. At the moment im running into a obstacle, Wire. -_-
Basically, I need help coding the wire of the game. I want the wire to be based on a boolean powered. I want it to be very fast and responsive but i have literally no idea how to code this.
I have an idea of how it would work but i have no idea how to code it.

The complete code of the game so far (the game is using slick2d):

package Main;

import org.newdawn.slick.*;
import Main.Components.*;

public class Main extends BasicGame {

static int width = 1280;
static int height = 800;
int tilesize = 5;
int xtiles = (width/tilesize);
int ytiles = (height/tilesize);
int Keyset = 0;
Tile[][] tile;

public Main() {
super("CompTron");

}
public void render(GameContainer arg0, Graphics g) throws SlickException {
switch(Keyset){
case 0:
g.setColor(Color.white);
g.drawString("Wire", 10, 10);
break;

case 1:
g.setColor(Color.white);
g.drawString("BaseComponent", 10, 10);
break;
}

for(int x = 0; x < xtiles; x++){
for(int y = 0; y < ytiles; y++){
if(tile[x][y].type != Type.base ){
tile[x][y].Draw(g);
}
}
}
}
public void init(GameContainer arg0) throws SlickException {
tile = new Tile[xtiles][ytiles];
for(int i = 0, x = 0;i < xtiles; i++, x += tilesize){
for(int b = 0, y = 0; b < ytiles; b++, y += tilesize){
tile = new Tile(x,y);
}
}

}
public void update(GameContainer gc, int arg1) throws SlickException {

for(int i = 0; i < xtiles; i++){
for(int b = 0; b < ytiles; b++){
if(tile.type != Type.base){
try{

tile.Interact(tile[b - 1], tile[b + 1], tile[i - 1], tile[i + 1]);
}catch(Exception e){}
}
}
}





Input in = gc.getInput();
if(in.isKeyDown(in.KEY_ESCAPE)){
System.exit(0);
}
if(in.isKeyDown(in.KEY_1)){
Keyset = 0;
}
if(in.isKeyDown(in.KEY_2)){
Keyset = 1;
}
if(in.isMouseButtonDown(in.MOUSE_LEFT_BUTTON)){

for(int i = 0;i < xtiles; i++){
for(int b = 0; b < ytiles; b++){
if(tile.occupied == false){
if(tile.x > in.getMouseX() - 5){
if(tile.x < in.getMouseX()){
if(tile.y > in.getMouseY() - 5){
if(tile.y < in.getMouseY()){



switch(Keyset){

case 0:
tile = new Wire(tile.getX(), tile.getY());
break;
case 1:
tile = new BaseComponent(tile.getX(), tile.getY());
break;

}
}
}
}
}
}

}
}
}

if(in.isMouseButtonDown(in.MOUSE_RIGHT_BUTTON)){
for(int i = 0;i < xtiles; i++){
for(int b = 0; b < ytiles; b++){
if(tile.x > in.getMouseX() - 5){
if(tile.x < in.getMouseX()){
if(tile.y > in.getMouseY() - 5){
if(tile.y < in.getMouseY()){

tile.setType(Type.base);
tile.setOccupied(false);
}
}
}
}

}
}
}
}


public static void main(String[] args)
throws SlickException
{
AppGameContainer app =
new AppGameContainer(new Main());

app.setDisplayMode(width,height, true);
app.setShowFPS(false);
app.start();
}

}

package Main;

import org.newdawn.slick.Graphics;


public class Tile {
int x;
int y;
int xsize;
String license = "";
int ysize;
Type type = Type.base;
boolean occupied = false;
boolean powered = false;
int source;
public Tile(int x, int y){
this.x = x;
this.y = y;
xsize = 5;
ysize = 5;
}

public void Draw(Graphics g){}
public void Interact(Tile up,Tile down, Tile left, Tile right){}
public void Init(){}
public int getX(){
return x;
}
public int getY(){
return y;
}
public int getXsize(){
return xsize;
}
public int getYsize(){
return ysize;
}
public void setX(int x){
this.x = x;
}
public void setY(int y){
this.y = y;
}
public void setXsize(int xsize){
this.xsize = xsize;
}
public void setYsize(int ysize){
this.ysize = ysize;
}
public void setType(Type type){
this.type = type;
}
public void setOccupied(boolean occupied){
this.occupied = occupied;
}
public void Update(){}
public void setLicense(String license){
this.license = license;
}
public String getLicense(){
return license;
}
public boolean getPowered(){
return powered;
}
public void setPowered(boolean powered){
this.powered = powered;
}
public int getSource(){
return source;
}
public void setSource(int source){
this.source = source;
}



}

package Main;

public enum Type {

base, tile

}

package Main.Components;



import org.newdawn.slick.*;
import Main.*;

public class BaseComponent extends Component{


public BaseComponent(int x, int y) {
super(x, y);
setType(Type.tile);
setLicense("BaseComponent");
}


public void Draw(Graphics g) {
g.setColor(Color.gray);
g.fillRect(getX(), getY(), getXsize(), getYsize());

}


public void Update() {


}

public void Interact(Tile up,Tile down, Tile left, Tile right) {


}



public void Init() {


}






}

package Main.Components;



import Main.*;

public abstract class Component extends Tile {

public Component(int x, int y) {
super(x, y);

}
Tile[] in = new Tile[100];
Tile[] out = new Tile[100];

public void setIn(Tile input, int inputnum){
in[inputnum] = input;

}
public void setOut(Tile output, int outputnum){
out[outputnum] = output;
}




}

package Main.Components;



import org.newdawn.slick.*;
import Main.Tile;
import Main.Type;

public class Wire extends Tile {



public Wire(int x, int y){
super(x,y);
setType(Type.tile);
Init();
setOccupied(true);
setLicense("Wire");

}



public void Interact(Tile up,Tile down, Tile left, Tile right) {
if(up.getLicense() == "BaseComponent" || down.getLicense() == "BaseComponent" || left.getLicense() == "BaseComponent" || right.getLicense() == "BaseComponent" ){
this.setPowered(true);
this.setSource(1);
if(up.getLicense() == "Wire"){
up.setSource(1);
}
if(down.getLicense() == "Wire"){
down.setSource(1);
}
if(left.getLicense() == "Wire"){
left.setSource(1);
}
if(right.getLicense() == "Wire"){
right.setSource(1);
}
}else{setSource(0);}
if(this.getLicense() == "Wire" && this.getSource() > 0){
if(up.getPowered() == true){
this.setPowered(true);
}
if(down.getPowered() == true){
this.setPowered(true);
}
if(left.getPowered() == true){
this.setPowered(true);
}
if(right.getPowered() == true){
this.setPowered(true);
}
}
if(up.getLicense() == "Wire"){

}
if(down.getLicense() == "Wire"){}
if(left.getLicense() == "Wire"){}
if(right.getLicense() == "Wire"){}


}



public void Init() {


}

public void Update() {
// TODO Auto-generated method stub

}

public void Draw(Graphics g) {
if(getPowered() == true){
g.setColor(Color.red);
g.fillRect(getX(), getY(), getXsize(), getYsize() );
}else{
g.setColor(new Color(100,0,0));
g.fillRect(getX(), getY(), getXsize(), getYsize() );
}
}







}



The reason i have a component class is because i want some components to have multiple inputs.

Thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!