Skip to content

Commit

Permalink
Created Vector class and stop robot at walls
Browse files Browse the repository at this point in the history
  • Loading branch information
darozak committed Jan 27, 2024
1 parent 064887c commit 09f6678
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 58 deletions.
14 changes: 10 additions & 4 deletions Code/dist/dungeon.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,22 @@ class Dungeon {
"#...#",
"#####"
];
#enter = new Vector(3, 3);
constructor(data) {
this.#data = data;
console.log("Dungeon constructed");
console.log(this.#data.tiles[0].name);
}
get enter() { return this.#enter; }
getTile(pos) {
return this.#map[pos.x][pos.y];
}
// Returns the index value of the object occupying location x, y.
getTileID(x, y) {
return this.#data.tiles.findLastIndex(d => d.key === this.#map[x][y]);
getTileID(pos) {
console.log(this.#map[pos.x][pos.y]);
return this.#data.tiles.findLastIndex(d => d.key === this.#map[pos.x][pos.y]);
}
getTileSpeed(x, y) {
return this.#data.tiles[0].speed;
getTileSpeed(pos) {
return this.#data.tiles[this.getTileID(pos)].speed;
}
}
1 change: 1 addition & 0 deletions Code/dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

<p>Before the script...</p>
<script src="data.js"></script>
<script src="vector.js"></script>
<script src="dungeon.js"></script>
<script src="robot.js"></script>
<script src="main.js"></script>
Expand Down
47 changes: 21 additions & 26 deletions Code/dist/robot.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,38 @@
"use strict";
class Robot {
#direction = [
new Vector(0, -1),
new Vector(1, -1),
new Vector(1, 0),
new Vector(1, 1),
new Vector(0, 1),
new Vector(-1, 1),
new Vector(-1, 0),
new Vector(-1, -1)
];
#stats = {
xpos: 3,
ypos: 4,
xtarg: 0,
ytarg: 0,
pos: new Vector(0, 3),
targ: new Vector(0, 3),
tile: "x",
speed: 1.0 // Meters per second
};
#data;
#dungeon;
constructor(data) {
this.#data = data;
this.#dungeon = new Dungeon(data);
this.#stats.pos = this.#dungeon.enter;
this.#stats.targ = this.#dungeon.enter;
this.#stats.tile = this.#dungeon.getTile(this.#stats.pos);
}
move(dir) {
if (dir == 1 || dir == 2 || dir == 3) {
this.#stats.xtarg = this.#stats.xpos + 1;
}
else if (dir == 5 || dir == 6 || dir == 7) {
this.#stats.xtarg = this.#stats.xpos - 1;
}
else {
this.#stats.xtarg = this.#stats.xpos;
}
if (dir == 7 || dir == 0 || dir == 1) {
this.#stats.ytarg = this.#stats.ypos - 1;
}
else if (dir == 3 || dir == 4 || dir == 5) {
this.#stats.ytarg = this.#stats.ypos + 1;
}
else {
this.#stats.ytarg = this.#stats.ypos;
}
if (this.#dungeon.getTileSpeed(this.stats.xtarg, this.stats.ytarg) > 0) {
this.#stats.xpos = this.#stats.xtarg;
this.#stats.ypos = this.#stats.ytarg;
this.#stats.targ = this.#stats.pos.add(this.#direction[dir]);
console.log(this.#dungeon.getTileID(this.#stats.targ));
if (this.#dungeon.getTileSpeed(this.#stats.targ) > 0) {
this.#stats.pos = this.#stats.targ;
}
}
get stats() {
return this.#stats;
return structuredClone(this.#stats);
}
}
1 change: 0 additions & 1 deletion Code/dist/types.js

This file was deleted.

12 changes: 12 additions & 0 deletions Code/dist/vector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"use strict";
class Vector {
x;
y;
constructor(x, y) {
this.x = x;
this.y = y;
}
add(vector) {
return new Vector(this.x + vector.x, this.y + vector.y);
}
}
18 changes: 14 additions & 4 deletions Code/src/dungeon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,29 @@ class Dungeon {
"#...#",
"#####"
];
#enter = new Vector(3,3);

constructor(data: Data) {
this.#data = data;
console.log("Dungeon constructed");
console.log(this.#data.tiles[0].name);
}

get enter() {return this.#enter}



getTile(pos: Vector) {
return this.#map[pos.x][pos.y];
}

// Returns the index value of the object occupying location x, y.
getTileID(x: number, y: number) {
return this.#data.tiles.findLastIndex(d => d.key === this.#map[x][y]);
getTileID(pos: Vector) {
console.log(this.#map[pos.x][pos.y]);
return this.#data.tiles.findLastIndex(d => d.key === this.#map[pos.x][pos.y]);
}

getTileSpeed(x: number, y: number) {
return this.#data.tiles[0].speed;
getTileSpeed(pos: Vector) {
return this.#data.tiles[this.getTileID(pos)].speed;
}
}
45 changes: 22 additions & 23 deletions Code/src/robot.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
class Robot {

#direction = [
new Vector(0,-1),
new Vector(1,-1),
new Vector(1,0),
new Vector(1,1),
new Vector(0,1),
new Vector(-1,1),
new Vector(-1,0),
new Vector(-1,-1)
]

#stats = {
xpos: 3,
ypos: 4,
xtarg: 0,
ytarg: 0,
pos: new Vector(0,3),
targ: new Vector(0,3),
tile: "x",
speed: 1.0 // Meters per second
}

Expand All @@ -14,32 +24,21 @@ class Robot {
constructor(data: Data) {
this.#data = data;
this.#dungeon = new Dungeon(data);
this.#stats.pos = this.#dungeon.enter;
this.#stats.targ = this.#dungeon.enter;
this.#stats.tile = this.#dungeon.getTile(this.#stats.pos);
}

move(dir: number) {
if(dir == 1 || dir == 2 || dir == 3) {
this.#stats.xtarg = this.#stats.xpos + 1;
} else if (dir == 5 || dir == 6 || dir == 7) {
this.#stats.xtarg = this.#stats.xpos - 1;
} else {
this.#stats.xtarg = this.#stats.xpos;
}

if(dir == 7 || dir == 0 || dir == 1) {
this.#stats.ytarg = this.#stats.ypos - 1;
} else if (dir == 3 || dir == 4 || dir == 5) {
this.#stats.ytarg = this.#stats.ypos + 1;
} else {
this.#stats.ytarg = this.#stats.ypos;
}
this.#stats.targ = this.#stats.pos.add(this.#direction[dir]);
console.log(this.#dungeon.getTileID(this.#stats.targ));

if (this.#dungeon.getTileSpeed(this.stats.xtarg, this.stats.ytarg) > 0) {
this.#stats.xpos = this.#stats.xtarg;
this.#stats.ypos = this.#stats.ytarg;
if (this.#dungeon.getTileSpeed(this.#stats.targ) > 0) {
this.#stats.pos = this.#stats.targ;
}
}

get stats() {
return this.#stats
return structuredClone(this.#stats);
}
}
13 changes: 13 additions & 0 deletions Code/src/vector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Vector {
x: number;
y: number;

constructor(x: number, y: number) {
this.x = x;
this.y = y;
}

add(vector: Vector) {
return new Vector(this.x + vector.x, this.y + vector.y);
}
}

0 comments on commit 09f6678

Please sign in to comment.