Skip to content

Commit

Permalink
ressources and initial character
Browse files Browse the repository at this point in the history
  • Loading branch information
adisoftbn committed Dec 23, 2017
1 parent 395929c commit 139cf03
Show file tree
Hide file tree
Showing 23 changed files with 201 additions and 38 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ testem.log
# System Files
.DS_Store
Thumbs.db

*.bat
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@angular/platform-browser": "^5.1.2",
"@angular/platform-browser-dynamic": "^5.1.2",
"@angular/router": "^5.1.2",
"babylonjs": "3.1.0",
"core-js": "^2.4.1",
"rxjs": "^5.1.0",
"zone.js": "^0.8.4"
Expand Down
9 changes: 6 additions & 3 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Component, AfterViewInit } from '@angular/core';
import { IGameRenderer, GameRenderer } from './shared/engine';

import { IBaseModel, Character } from './shared/engine/models';
import { IBaseModel, Character } from './shared/engine/object';

@Component({
selector: 'app-root',
Expand All @@ -10,7 +10,7 @@ import { IBaseModel, Character } from './shared/engine/models';
})
export class AppComponent implements AfterViewInit {
private _game: IGameRenderer
private _currentCharacter: IBaseModel;
private _currentCharacter: Character;
ngAfterViewInit() {
this._game = new GameRenderer('renderCanvas');
this._game.createScene();
Expand All @@ -20,6 +20,9 @@ export class AppComponent implements AfterViewInit {
}

createTestGame() {
this._currentCharacter = new Character(this._game, 'currentCharacter', '/assets/Rabbit/', 'Rabbit.babylon');
this._currentCharacter = new Character(this._game);
this._currentCharacter.buildFromGallery('dude', () => {
this._game.getCamera().lockedTarget = this._currentCharacter.getModelHead();
});
}
}
1 change: 1 addition & 0 deletions src/app/shared/engine/GameRenderer.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface IGameRenderer {
getCamera();

getShadowGenerator();
getCharacterGallery();

animate();

Expand Down
33 changes: 24 additions & 9 deletions src/app/shared/engine/GameRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,51 @@
import {
Engine, Scene, FollowCamera, Light, DirectionalLight, IShadowLight,
Vector3, HemisphericLight, MeshBuilder, ShadowGenerator
Vector3, HemisphericLight, MeshBuilder, ShadowGenerator, ArcRotateCamera, StandardMaterial, Color3
} from 'babylonjs';


import { IGameRenderer } from './GameRenderer.interface';
import { CharacterGalleryManager } from './gallery';


export class GameRenderer implements IGameRenderer {
private _canvas: HTMLCanvasElement;
private _engine: Engine;
private _scene: Scene;
private _camera: FollowCamera;
private _camera: ArcRotateCamera;
private _light: IShadowLight;
private _shadowGenerator: ShadowGenerator;
private _characterGallery: CharacterGalleryManager;
constructor(canvasElement: string) {
this._canvas = <HTMLCanvasElement>document.getElementById(canvasElement);
this._engine = new Engine(this._canvas, true);
this._engine.enableOfflineSupport = false;
this._characterGallery = new CharacterGalleryManager(this);
}

public createScene() {
this._scene = new Scene(this._engine);
this._light = new DirectionalLight('light1', new Vector3(0, -0.5, -1.0), this._scene);
this._camera = new ArcRotateCamera('camera1', 0, 0, 10, new Vector3(0, 30, 0), this._scene);

this._camera = new FollowCamera('camera1', new Vector3(0, 5, -10), this._scene);
this._scene.ambientColor = new Color3(0.3, 0.3, 0.3);

this._light.position = new Vector3(20, 150, 70);

this._camera.attachControl(this._canvas, true);
this._camera.setPosition(new BABYLON.Vector3(0, 40, 12));
this._camera.minZ = 10.0;

this._light = new DirectionalLight('light1', new Vector3(0, 1, 0), this._scene);

const sphere = MeshBuilder.CreateSphere('sphere1', { segments: 16, diameter: 2 }, this._scene);
this._camera.lockedTarget = sphere;
this._shadowGenerator = new BABYLON.ShadowGenerator(1024, this._light);
const ground = MeshBuilder.CreateGround('ground1', { width: 6, height: 6, subdivisions: 2 }, this._scene);
const groundMaterial = new StandardMaterial('ground', this._scene);
groundMaterial.diffuseColor = new Color3(0.5, 0.5, 0.5);
groundMaterial.specularColor = new Color3(0, 0, 0);
ground.material = groundMaterial;
ground.receiveShadows = true;

sphere.position.y = 1;
this._shadowGenerator = new ShadowGenerator(2048, this._light);

const ground = MeshBuilder.CreateGround('ground1', { width: 6, height: 6, subdivisions: 2 }, this._scene);
}

public getScene(): Scene {
Expand All @@ -46,7 +57,11 @@ export class GameRenderer implements IGameRenderer {
}

public getShadowGenerator() {
return this._shadowGenerator;
}

public getCharacterGallery() {
return this._characterGallery;
}

public animate() {
Expand Down
37 changes: 37 additions & 0 deletions src/app/shared/engine/gallery/CharacterGalleryManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { IGameRenderer } from '../';
import { ICharacterGalleryItem, CharacterGalleryItem } from '../model/';

export class CharacterGalleryManager {
private _gameRenderer: IGameRenderer;
private _models = {
rabbit: new CharacterGalleryItem(
'assets/models/Rabbit/',
'Rabbit.babylon',
'babylon',
{
stand: [0, 30],
run: [31, 54]
}
),
dude: new CharacterGalleryItem(
'assets/models/Dude/',
'Dude.babylon',
'babylon',
{
stand: [0, 30],
run: [31, 54]
}
)
}
constructor(gameRenderer: IGameRenderer) {
this._gameRenderer = gameRenderer;
}

getModelByName(name: string): ICharacterGalleryItem {
if (this._models[name]) {
return this._models[name];
} else {
throw new Error(`Cannot find model ${name}!`);
}
}
}
1 change: 1 addition & 0 deletions src/app/shared/engine/gallery/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './CharacterGalleryManager';
6 changes: 6 additions & 0 deletions src/app/shared/engine/model/CharacterGalleryItem.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface ICharacterGalleryItem {
modelPath: string;
modelFileName: string;
modelType: string;
animations: any;
}
15 changes: 15 additions & 0 deletions src/app/shared/engine/model/CharacterGalleryItem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ICharacterGalleryItem } from './CharacterGalleryItem.interface';

export class CharacterGalleryItem implements ICharacterGalleryItem {
modelPath = '';
modelFileName = '';
modelType = 'babylon';
animations = {};

constructor(modelPath: string, modelFileName: string, modelType: string = 'babylon', animations = {}) {
this.modelPath = modelPath;
this.modelFileName = modelFileName;
this.modelType = modelType;
this.animations = animations;
}
}
2 changes: 2 additions & 0 deletions src/app/shared/engine/model/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './CharacterGalleryItem.interface';
export * from './CharacterGalleryItem';
6 changes: 0 additions & 6 deletions src/app/shared/engine/models/BaseModel.interface.ts

This file was deleted.

19 changes: 0 additions & 19 deletions src/app/shared/engine/models/Character.ts

This file was deleted.

9 changes: 9 additions & 0 deletions src/app/shared/engine/object/BaseModel.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface IBaseModel {
moveForward();
moveBackward();
rotateLeft(angle);
rotateRight(angle);
buildFromUrl(modelName: string, path: string, modelFileName: string, callback?: Function);
buildFromGallery(modelName, callback?: Function);
getModel();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { IBaseModel } from './BaseModel.interface';
import { IGameRenderer } from '../';

export class BaseModel implements IBaseModel {
private _model = null;
protected _gameRenderer = null;
constructor(gameRenderer: IGameRenderer) {
this._gameRenderer = gameRenderer;
Expand All @@ -19,5 +18,15 @@ export class BaseModel implements IBaseModel {
}
rotateRight(angle) {

}
buildFromUrl(modelName: string, path: string, modelFileName: string, callback?: Function) {

}
buildFromGallery(modelName, callback?: Function) {

}

getModel() {

}
}
85 changes: 85 additions & 0 deletions src/app/shared/engine/object/Character.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { Vector3, SceneLoader, MeshBuilder } from 'babylonjs';

import { IGameRenderer } from '../';
import { BaseModel } from './BaseModel';

export class Character extends BaseModel {

protected _model = null;
protected _modelhead = null;
protected _animations = {};
protected _skeletons = [];

public animationSpeed = 0.8;

constructor(gameRenderer: IGameRenderer) {
super(gameRenderer);
this._gameRenderer = gameRenderer;
this._modelhead = MeshBuilder.CreateSphere('', { segments: 16, diameter: 0.01 }, this._gameRenderer.getScene());

this._modelhead.position.y = 6;


}

getModel() {
return this._model;
}

getModelHead() {
return this._modelhead;
}

public buildFromUrl(modelName: string, path: string, modelFileName: string, callback?: Function) {
const scene = this._gameRenderer.getScene();
SceneLoader.ImportMesh(modelName, path, modelFileName, scene,
(newMeshes, particleSystems, skeletons) => {
this.importMeshSuccess(newMeshes, particleSystems, skeletons);
if (callback) {
callback();
}
});
}

public buildFromGallery(modelName, callback?: Function) {
const model = this._gameRenderer.getCharacterGallery().getModelByName(modelName);
console.log(model);
if (model) {
this._animations = model.animations;
const scene = this._gameRenderer.getScene();
SceneLoader.ImportMesh('', model.modelPath, model.modelFileName, scene,
(newMeshes, particleSystems, skeletons) => {
this.importMeshSuccess(newMeshes, particleSystems, skeletons);
if (callback) {
callback();
}
});
}
}
switchToAnimation(animationName) {
if (this._animations[animationName]) {
this._gameRenderer.getScene().beginAnimation(
this._skeletons[0],
this._animations[animationName][0],
this._animations[animationName][1],
true,
this.animationSpeed
);
}
}

private importMeshSuccess(newMeshes, particleSystems, skeletons) {
try {
if (newMeshes.length > 0) {
const scene = this._gameRenderer.getScene();
this._skeletons = skeletons;
this._model = newMeshes[0];
this._model.scaling = new Vector3(0.1, 0.1, 0.1);
this._gameRenderer.getShadowGenerator().getShadowMap().renderList.push(this._model);
// scene.beginAnimation(skeletons[0], 0, 500, true, 0.8);
}
} catch (e) {
console.log(e);
}
}
}
File renamed without changes.
Binary file added src/assets/models/Dude/0.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/models/Dude/1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/models/Dude/2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/models/Dude/3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/assets/models/Dude/Dude.babylon

Large diffs are not rendered by default.

Binary file added src/assets/models/Rabbit/0.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/assets/models/Rabbit/Rabbit.babylon

Large diffs are not rendered by default.

0 comments on commit 139cf03

Please sign in to comment.