Skip to content

Commit

Permalink
ch06
Browse files Browse the repository at this point in the history
  • Loading branch information
joshtynjala committed Jan 22, 2023
1 parent 482bae2 commit be45cce
Show file tree
Hide file tree
Showing 13 changed files with 608 additions and 0 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,46 @@ jobs:
run: |
haxelib run openfl build html5 -final -clean --app-main=VelocityAngle
cp -R bin/html5/bin ../gh-pages/ch05/VelocityAngle
- name: Build ch06/Bouncing1
working-directory: ch06
run: |
haxelib run openfl build html5 -final -clean --app-main=Bouncing1
cp -R bin/html5/bin ../gh-pages/ch06/Bouncing1
- name: Build ch06/Bouncing2
working-directory: ch06
run: |
haxelib run openfl build html5 -final -clean --app-main=Bouncing2
cp -R bin/html5/bin ../gh-pages/ch06/Bouncing2
- name: Build ch06/Fountain
working-directory: ch06
run: |
haxelib run openfl build html5 -final -clean --app-main=Fountain
cp -R bin/html5/bin ../gh-pages/ch06/Fountain
- name: Build ch06/Friction1
working-directory: ch06
run: |
haxelib run openfl build html5 -final -clean --app-main=Friction1
cp -R bin/html5/bin ../gh-pages/ch06/Friction1
- name: Build ch06/Friction2
working-directory: ch06
run: |
haxelib run openfl build html5 -final -clean --app-main=Friction2
cp -R bin/html5/bin ../gh-pages/ch06/Friction2
- name: Build ch06/Removal
working-directory: ch06
run: |
haxelib run openfl build html5 -final -clean --app-main=Removal
cp -R bin/html5/bin ../gh-pages/ch06/Removal
- name: Build ch06/ShipSim2
working-directory: ch06
run: |
haxelib run openfl build html5 -final -clean --app-main=ShipSim2
cp -R bin/html5/bin ../gh-pages/ch06/ShipSim2
- name: Build ch06/ShipSimFriction
working-directory: ch06
run: |
haxelib run openfl build html5 -final -clean --app-main=ShipSimFriction
cp -R bin/html5/bin ../gh-pages/ch06/ShipSimFriction
- name: Upload Site
if: ${{ github.repository_owner == 'joshtynjala' && github.event_name != 'pull_request' && github.ref_name == 'main' }}
uses: JamesIves/[email protected]
Expand Down
53 changes: 53 additions & 0 deletions ch06/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Chapter 6

Sample code translated to [OpenFL](https://openfl.org/) and [Haxe](https://haxe.org/) from **Chapter 6** of [_Foundation ActionScript 3.0 Animation: Making Things Move_](http://www.apress.com/9781590597910) by Keith Peters (Apress, 2007).

> Ported and shared with permission. Thank you, Keith.
## Samples

Several samples from this chapter are available in the _src_ directory.

- [_Bouncing1.hx_](src/Bouncing1.hx)[Demo](https://joshtynjala.github.io/foundation-animation-making-things-move-haxe-openfl/ch06/Bouncing1/index.html)
- [_Bouncing2.hx_](src/Bouncing2.hx)[Demo](https://joshtynjala.github.io/foundation-animation-making-things-move-haxe-openfl/ch06/Bouncing2/index.html)
- [_Fountain.hx_](src/Fountain.hx)[Demo](https://joshtynjala.github.io/foundation-animation-making-things-move-haxe-openfl/ch06/Fountain/index.html)
- [_Friction1.hx_](src/Friction1.hx)[Demo](https://joshtynjala.github.io/foundation-animation-making-things-move-haxe-openfl/ch06/Friction1/index.html)
- [_Friction2.hx_](src/Friction2.hx)[Demo](https://joshtynjala.github.io/foundation-animation-making-things-move-haxe-openfl/ch06/Friction2/index.html)
- [_Removal.hx_](src/Removal.hx)[Demo](https://joshtynjala.github.io/foundation-animation-making-things-move-haxe-openfl/ch06/Removal/index.html)
- [_ShipSim2.hx_](src/ShipSim2.hx)[Demo](https://joshtynjala.github.io/foundation-animation-making-things-move-haxe-openfl/ch06/ShipSim2/index.html)
- [_ShipSimFriction.hx_](src/ShipSimFriction.hx)[Demo](https://joshtynjala.github.io/foundation-animation-making-things-move-haxe-openfl/ch06/ShipSimFriction/index.html)

## Requirements

- [Haxe](https://haxe.org/download/)
- [OpenFL](https://openfl.org/download/)

## Build & Run

This directory contains an OpenFL project. The _project.xml_ file specifies the project's configuration options, and the Haxe source code appears in the _src_ directory.

To run this project, open a terminal in this directory, and run the following command:

```sh
openfl test html5
```

The _project.xml_ file contains a line for each sample, like this:

```xml
<app main="Bouncing1"/>
```

All except one of the samples are commented out:

```xml
<!-- <app main="Fountain"/> -->
```

To switch between samples, comment out the currently selected sample, and uncomment a different sample. Then, run `openfl test html5` again.

Alternatively, you can switch between samples without modifying _project.xml_ by specifying the `--app-main` command line option with the sample name.

```sh
openfl test html5 --app-main=Fountain
```
22 changes: 22 additions & 0 deletions ch06/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<project>
<meta title="Chapter 6" package="openfl.foundationanimation.ch06"/>
<app file="AnimationCh06"/>

<app main="Bouncing1"/>
<!-- <app main="Bouncing2"/> -->
<!-- <app main="Fountain"/> -->
<!-- <app main="Friction1"/> -->
<!-- <app main="Friction2"/> -->
<!-- <app main="Removal"/> -->
<!-- <app main="ShipSim2"/> -->
<!-- <app main="ShipSimFriction"/> -->

<window allow-high-dpi="true"/>
<window fps="60"/>
<window fps="0" if="html5"/>

<source path="src"/>

<haxelib name="openfl"/>
</project>
23 changes: 23 additions & 0 deletions ch06/src/Ball.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import openfl.display.Sprite;

class Ball extends Sprite {
public var radius:Float;

private var color:UInt;

public var vx:Float = 0;
public var vy:Float = 0;

public function new(radius:Float = 40, color:UInt = 0xff0000) {
super();
this.radius = radius;
this.color = color;
init();
}

public function init():Void {
graphics.beginFill(color);
graphics.drawCircle(0, 0, radius);
graphics.endFill();
}
}
52 changes: 52 additions & 0 deletions ch06/src/Bouncing1.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import openfl.events.Event;
import openfl.display.StageAlign;
import openfl.display.StageScaleMode;
import openfl.display.Sprite;

class Bouncing1 extends Sprite {
private var ball:Ball;
private var vx:Float;
private var vy:Float;

public function new() {
super();
init();
}

private function init():Void {
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;

ball = new Ball();
ball.x = stage.stageWidth / 2;
ball.y = stage.stageHeight / 2;
vx = Math.random() * 10 - 5;
vy = Math.random() * 10 - 5;
addChild(ball);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}

private function onEnterFrame(event:Event):Void {
ball.x += vx;
ball.y += vy;
var left:Float = 0;
var right:Float = stage.stageWidth;
var top:Float = 0;
var bottom:Float = stage.stageHeight;

if (ball.x + ball.radius > right) {
ball.x = right - ball.radius;
vx *= -1;
} else if (ball.x - ball.radius < left) {
ball.x = left + ball.radius;
vx *= -1;
}
if (ball.y + ball.radius > bottom) {
ball.y = bottom - ball.radius;
vy *= -1;
} else if (ball.y - ball.radius < top) {
ball.y = top + ball.radius;
vy *= -1;
}
}
}
55 changes: 55 additions & 0 deletions ch06/src/Bouncing2.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import openfl.events.Event;
import openfl.display.StageAlign;
import openfl.display.StageScaleMode;
import openfl.display.Sprite;

class Bouncing2 extends Sprite {
private var ball:Ball;
private var vx:Float;
private var vy:Float;
private var bounce:Float = -0.7;
private var gravity:Float = .5;

public function new() {
super();
init();
}

private function init():Void {
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;

ball = new Ball();
ball.x = stage.stageWidth / 2;
ball.y = stage.stageHeight / 2;
vx = Math.random() * 10 - 5;
vy = -10;
addChild(ball);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}

private function onEnterFrame(event:Event):Void {
vy += gravity;
ball.x += vx;
ball.y += vy;
var left:Float = 0;
var right:Float = stage.stageWidth;
var top:Float = 0;
var bottom:Float = stage.stageHeight;

if (ball.x + ball.radius > right) {
ball.x = right - ball.radius;
vx *= bounce;
} else if (ball.x - ball.radius < left) {
ball.x = left + ball.radius;
vx *= bounce;
}
if (ball.y + ball.radius > bottom) {
ball.y = bottom - ball.radius;
vy *= bounce;
} else if (ball.y - ball.radius < top) {
ball.y = top + ball.radius;
vy *= bounce;
}
}
}
49 changes: 49 additions & 0 deletions ch06/src/Fountain.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import openfl.display.Sprite;
import openfl.display.StageAlign;
import openfl.display.StageScaleMode;
import openfl.events.Event;

class Fountain extends Sprite {
private var count:Int = 100;
private var balls:Array<Ball>;
private var gravity:Float = 0.5;

public function new() {
super();
init();
}

private function init():Void {
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
balls = new Array<Ball>();
for (i in 0...count) {
var ball:Ball = new Ball(2, Std.int(Math.random() * 0xffffff));
ball.x = stage.stageWidth / 2;
ball.y = stage.stageHeight;
ball.vx = Math.random() * 2 - 1;
ball.vy = Math.random() * -10 - 10;
addChild(ball);
balls.push(ball);
}
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}

private function onEnterFrame(event:Event):Void {
for (i in 0...balls.length) {
var ball:Ball = balls[i];
ball.vy += gravity;
ball.x += ball.vx;
ball.y += ball.vy;
if (ball.x - ball.radius > stage.stageWidth
|| ball.x + ball.radius < 0
|| ball.y - ball.radius > stage.stageHeight
|| ball.y + ball.radius < 0) {
ball.x = stage.stageWidth / 2;
ball.y = stage.stageHeight;
ball.vx = Math.random() * 2 - 1;
ball.vy = Math.random() * -10 - 10;
}
}
}
}
43 changes: 43 additions & 0 deletions ch06/src/Friction1.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import openfl.display.Sprite;
import openfl.display.StageAlign;
import openfl.display.StageScaleMode;
import openfl.events.Event;

class Friction1 extends Sprite {
private var ball:Ball;
private var vx:Float = 0;
private var vy:Float = 0;
private var friction:Float = 0.1;

public function new() {
super();
init();
}

private function init():Void {
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;

ball = new Ball();
ball.x = stage.stageWidth / 2;
ball.y = stage.stageHeight / 2;
vx = Math.random() * 10 - 5;
vy = Math.random() * 10 - 5;
addChild(ball);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}

private function onEnterFrame(event:Event):Void {
var speed:Float = Math.sqrt(vx * vx + vy * vy);
var angle:Float = Math.atan2(vy, vx);
if (speed > friction) {
speed -= friction;
} else {
speed = 0;
}
vx = Math.cos(angle) * speed;
vy = Math.sin(angle) * speed;
ball.x += vx;
ball.y += vy;
}
}
36 changes: 36 additions & 0 deletions ch06/src/Friction2.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import openfl.display.Sprite;
import openfl.display.StageAlign;
import openfl.display.StageScaleMode;
import openfl.events.Event;

class Friction2 extends Sprite {
private var ball:Ball;
private var vx:Float = 0;
private var vy:Float = 0;
private var friction:Float = 0.9;

public function new() {
super();
init();
}

private function init():Void {
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;

ball = new Ball();
ball.x = stage.stageWidth / 2;
ball.y = stage.stageHeight / 2;
vx = Math.random() * 10 - 5;
vy = Math.random() * 10 - 5;
addChild(ball);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}

private function onEnterFrame(event:Event):Void {
vx *= friction;
vy *= friction;
ball.x += vx;
ball.y += vy;
}
}
Loading

0 comments on commit be45cce

Please sign in to comment.