Scott Welsh

Tank Showdown

Using the Box2d physics library in createjs allowed for us to achieve the desired gameplay rules and effects quickly. Implementing our game within a physics world also gives each object a consistent and expected behavior and feel.

BouncePlatform.ts

	module objects{
    export class BouncePlatform extends objects.PhysicsObject{
    
        public health: number;
        public destructable: boolean;
        public graphic;

        constructor( position: math.Vec2){
            super( );
            this.name = "wall";
      

            this.x = position.x;
            this.y = position.y;

            var image=  managers.Game.assetManager.getResult("wall4");
            
            if(image){
                this.graphic = new createjs.Bitmap(image);
                this.graphic.regX = this.graphic.getBounds().width * 0.5;
                this.graphic.regY = this.graphic.getBounds().width * 0.5;
                this.graphic.scaleX = .151;
                this.graphic.scaleY = .151;
                this.addChild(this.graphic);
            }
            // this.graphic.x = -32;
            // this.graphic.y = -32;

            this.createRigidBody(Box2D.Dynamics.b2Body.b2_staticBody);
            this.createRigidbodyFixture( Box2D.Collision.Shapes.b2PolygonShape.AsBox(32 / config.Quality.scale ,32 / config.Quality.scale ), 0, 1, 10);
            
            managers.Game.addObjectToScene(this);

            console.log("rigidbody", this.rigidbody)
        }

        public Update(): void{
                if(this.rigidbody){
                let position = this.rigidbody.GetPosition();
                // console.log('wall rigidbody position', position)
                this.x = position.x * config.Quality.scale;
                this.y = position.y * config.Quality.scale;
                // console.log('wall graphic position', this.x)


                this.rotation = this.rigidbody.GetAngle() * (180 / Math.PI);
            }
        }

        public BeginContact(self, other) {
            // console.log("other", other.name);
            
            if(other.name == "player"){
                console.log("bounce the player")

                let original_velocity = other.rigidbody.GetLinearVelocity();

                let bounceVelocity =  new Box2D.Common.Math.b2Vec2(0, 1);
    
                let new_velocity = original_velocity;
                new_velocity.Add(bounceVelocity);
                
                other.rigidbody.SetLinearVelocity( original_velocity );
                
            }

        }
    }
}
	
Copyright Scott Welsh 2024
☕ Buy me a coffee