Skip to content

Commit

Permalink
Basic Sheep Movement
Browse files Browse the repository at this point in the history
  • Loading branch information
zenled committed Dec 28, 2017
1 parent 4896fb0 commit b4129da
Show file tree
Hide file tree
Showing 9 changed files with 275 additions and 25 deletions.
70 changes: 70 additions & 0 deletions web/controllers/herd_controller.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import '../game_objects/game_object.dart';
import '../game_objects/sheep/sheep.dart';
import '../game_objects/sheep/i_sheep_herder.dart';

const _creator_num_of_sheep = 15;
const _creator_sheep_in_row = 5;
const _creator_sheep0_x = -5.0;
const _creator_sheep0_z = -10.0;
const _creator_distance = 2.0;

const walking_plane_y = 1.1;

class HerdController {
final GameObject rootObject;
final ISheepHerder sheepHerder;

List<Sheep> _herdMembers = <Sheep>[];

HerdController(this.rootObject, this.sheepHerder){
_createHerdMembers();
}

void moveHerdMembers(){
for (Sheep sheep in _herdMembers){
sheep.move();
}
}

void _createHerdMembers(){
int row = 0;
int column = 0;
for (int i = 0; i < _creator_num_of_sheep; i++){
Sheep sheep = new Sheep(sheepHerder);
// sets inital position
double z = _creator_sheep0_z;
z += row * _creator_distance;
sheep.z = z;

double x = _creator_sheep0_x;
x += column * _creator_distance;
sheep.x = x;

rootObject.addChild(sheep);
_herdMembers.add(sheep);

column++;
if (column == _creator_sheep_in_row){
row++;
column = 0;
}
}

// for (int i = 0; i < _initial_num_of_sheep; i++){
// Sheep sheep = new Sheep(sheepHerder);
// sheep.translateY(walking_plane_y);
// sheep.z = -10.0;

// if (i == 0){
// sheep.x = -5.0;
// }

// if (i == 2){
// sheep.x = 5.0;
// }

// rootObject.addChild(sheep);
// _herdMembers.add(sheep);
// }
}
}
41 changes: 23 additions & 18 deletions web/game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ import 'global.dart';
import 'matrix4.dart';
import 'input_handler.dart';

// Game Objects
import 'game_objects/root_object.dart';
import 'game_objects/player/player.dart';
import 'game_objects/main_camera.dart';
import 'game_objects/pyramid.dart';
import 'game_objects/world/world.dart';

// Controllers
import 'controllers/herd_controller.dart';

class Game {
Matrix4 _pMatrix;
Matrix4 _mvMatrix;
Expand All @@ -39,6 +43,9 @@ class Game {
MainCamera mainCamera;
Player player;

// Controllers
HerdController herdController;

Game() {
// inits Matrix-es
_mvMatrix = new Matrix4()..identity();
Expand Down Expand Up @@ -85,30 +92,23 @@ class Game {
// inits inputHandler
_inputHandler = new InputHandler(window);

//inits root object
// inits Game Objects ----------------------------------------------------------------------
// rootObject
rootObject = new RootObject();

// Player pyramid = new Player();
// pyramid.translateZ(-10.0);
player = new Player();
player.translateY(5.0);

mainCamera = new MainCamera(player);

Pyramid pyramid = new Pyramid();
pyramid.translateZ(-20.0);
pyramid.translateX(5.0);
rootObject.addChild(pyramid);

Pyramid pyramid1 = new Pyramid();
pyramid1.translateZ(-20.0);
pyramid1.translateX(-5.0);
rootObject.addChild(pyramid1);

// world (scenery)
World world = new World();
rootObject.addChild(world);

// player
player = new Player();
rootObject.addChild(player);

// mainCamera
mainCamera = new MainCamera(player);

// inits Controllers ----------------------------------------------------------------------
herdController = new HerdController(rootObject, player);
}

void mvPushMatrix() {
Expand Down Expand Up @@ -140,6 +140,10 @@ class Game {
rootObject.handleUserInputCall();
}

void _handleControllers(){
herdController.moveHerdMembers();
}

void startGame() {
window.animationFrame.then(_tick);
// _timer = new Timer.periodic(
Expand All @@ -154,6 +158,7 @@ class Game {
void _tick(_) {
window.animationFrame.then(_tick);
_handleUserInput();
_handleControllers();
_drawScene();
}

Expand Down
6 changes: 5 additions & 1 deletion web/game_objects/game_object.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import 'dart:web_gl';
import 'dart:typed_data';

import '../global.dart' as global;
import '../input_handler.dart';
import '../matrix4.dart';
import '../math_util.dart' as math_util;

export 'game_object.dart';
export '../global.dart';
Expand Down Expand Up @@ -176,4 +176,8 @@ abstract class GameObject {

global.mvPopMatrix();
}

math_util.Point3D get point3D => new math_util.Point3D(x, y, z);

math_util.Point2D get point2D_birdView => new math_util.Point2D(x, -z);
}
19 changes: 18 additions & 1 deletion web/game_objects/player/player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ library Player;
import '../game_object.dart';
import '../pyramid.dart';

class Player extends GameObject {
import '../sheep/i_sheep_herder.dart';

class Player extends GameObject implements ISheepHerder {
static const xSpeed = 0.04;
static const ySpeed = 0.04;
static const zSpeed = 0.04;

static const initialPositionX = 0.0;
static const initialPositionY = 5.0;
static const initialPositionZ = 0.0;

static const yRotationSpeed = 2.0;

// how many degrees shotld the drone tilt (this does not affect position)
Expand All @@ -17,6 +23,11 @@ class Player extends GameObject {
GameObject droneBody;

Player() {
// sets inital position
translateX(initialPositionX);
translateY(initialPositionY);
translateZ(initialPositionZ);

droneBody = new Pyramid();
addChild(droneBody);
}
Expand Down Expand Up @@ -71,4 +82,10 @@ class Player extends GameObject {
rotateY(yRotationSpeed);
}
}

// ISheepHerder --------------------------------------------------------------------

// TODO: implement rotation
@override
double get rotation => rotationY;
}
13 changes: 13 additions & 0 deletions web/game_objects/sheep/i_sheep_herder.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import '../../math_util.dart' as math_util;

abstract class ISheepHerder {
double get x;
double get y;
double get z;

double get rotation;

math_util.Point3D get point3D;

math_util.Point2D get point2D_birdView;
}
38 changes: 38 additions & 0 deletions web/game_objects/sheep/sheep.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
library Sheep;

import '../game_object.dart';
import '../../math_util.dart' as math_util;

import 'i_sheep_herder.dart';
part 'sheep_body.dart';

class Sheep extends GameObject {
// [herder] must be less than [attention_distance] away from Sheep in order for sheep to respond to it
static const attention_distance = 20.0;

static const movement_speed = 0.02;

// Shomehing that herds sheep (sheep run away from this object)
ISheepHerder herder;

SheepBody body;

Sheep(this.herder) {
body = new SheepBody(this);
addChild(body);
}

void move() {
// rotates away from player
rotationY =
math_util.angle2D(herder.point2D_birdView, this.point2D_birdView);
rotateY(90.0);

// if herder away enough it doesn't move
if (math_util.distance3D(herder.point3D, point3D) > attention_distance) {
return;
}

translateZ(movement_speed);
}
}
69 changes: 69 additions & 0 deletions web/game_objects/sheep/sheep_body.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
part of Sheep;

class SheepBody extends GameObject {
SheepBody(GameObject parent) {
this.parent = parent;

List<double> vertices = <double>[
// Front face
0.0, 1.0, 0.0,
-1.0, -1.0, 1.0,
1.0, -1.0, 1.0,

// Right face
0.0, 1.0, 0.0,
1.0, -1.0, 1.0,
1.0, -1.0, -1.0,

// Back face
0.0, 1.0, 0.0,
1.0, -1.0, -1.0,
-1.0, -1.0, -1.0,

// Left face
0.0, 1.0, 0.0,
-1.0, -1.0, -1.0,
-1.0, -1.0, 1.0,

// NOTE: Missing the bottom triangles :)
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
1.0, -1.0, 1.0,
-1.0, -1.0, -1.0,
1.0, -1.0, 1.0,
-1.0, -1.0, 1.0,
];
super.setVertexBuffer(vertices);

var colors = [
// Front face
1.0, 0.0, 0.0, 1.0,
1.0, 0.0, 0.0, 1.0,
1.0, 0.0, 0.0, 1.0,

// Right face
0.0, 0.0, 1.0, 1.0,
0.0, 0.0, 1.0, 1.0,
0.0, 0.0, 1.0, 1.0,

// Back face
0.0, 0.0, 1.0, 1.0,
0.0, 0.0, 1.0, 1.0,
0.0, 0.0, 1.0, 1.0,

// Left face
0.0, 0.0, 1.0, 1.0,
0.0, 0.0, 1.0, 1.0,
0.0, 0.0, 1.0, 1.0,

// Bottom face
0.0, 0.0, 1.0, 1.0,
0.0, 0.0, 1.0, 1.0,
0.0, 0.0, 1.0, 1.0,
0.0, 0.0, 1.0, 1.0,
0.0, 0.0, 1.0, 1.0,
0.0, 0.0, 1.0, 1.0
];
super.setColorBuffer(colors);
}
}
6 changes: 1 addition & 5 deletions web/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@ import 'dart:html';

import 'global.dart' as global;
import 'game.dart';
import 'math_util.dart';

void main() {
global.canvas = querySelector("#canvas");
global.gl = global.canvas.getContext3d();

global.game = new Game();
global.game.startGame();

// querySelector("body").addEventListener("keydown", (Event event){
// if ()
// event.preventDefault();
// });
}
Loading

0 comments on commit b4129da

Please sign in to comment.